简体   繁体   中英

Return type of template function

Info:

I'm currently trying to learn template metaprogramming (by following this book ). One useful example they give is for dimensional analysis. I implemented it as in the book and everything was fine; see here .

My problem is however, I want to use the dimensional analysis framework with mixed types. By this I mean you could have say a scalar with dimensions of mass multiplying a vector with dimensions of acceleration to give a vector force. As it stands in the the link they only work with the same type T for input and output of all operations.

I have a 3-vector class which has all the necessary operations for multiplying with/dividing by scalars, etc. so I would like to do something like

quantity<double,mass> m(1.0);
quantity<vect,acceleration> a(vect(0.0,0.0,-9.81));
quantity<vect,force> f = m*a;

First attempt:

To acheive this I tried exending the examples form the book to handle two different types as inputs to operator* and operator/ however I hit a wall when it comes to the return type.

I know here the return type of double * vect is vect but if they are the other way around vect * double it is still a vect . Worse; in principle the return type could be anything. So I want a way to extend the operator* to something like

template<class T1, class T2, class Dim1, class Dim2>
quantity<X, typename boost::mpl::transform<Dim1,Dim2,boost::mpl::plus<_1,_2> >::type>
operator*(const quantity<T1,Dim1>& q1, const quantity<T2,Dim2>& q2)
{
    return quantity<X,
                    typename boost::mpl::transform<Dim1,Dim2,boost::mpl::plus<_1,_2> >::type>
                   (q1.value()*q2.value());
}

where X is the return type of q1.value()*q2.value() and is deduced at compile time. I tried simply adding another template class T3 to the signature and having it return T3 but it seems it cannot deduce what T3 should be.

Second attempt:

Next I tried using decltype as follows

template<class T1, class T2>
struct return_type
{
    auto mult_f(const T1& a, const T2& b)->decltype(a*b){return a*b;}
    typedef decltype(mult_f) type;
};

template<class T1, class T2, class Dim1, class Dim2>
quantity<typename return_type<T1,T2>::type, typename boost::mpl::transform<Dim1,Dim2,boost::mpl::plus<_1,_2> >::type>
operator*(const quantity<T1,Dim1>& q1, const quantity<T2,Dim2>& q2)
{
    return quantity<typename return_type<T1,T2>::type,
                    typename boost::mpl::transform<Dim1,Dim2,boost::mpl::plus<_1,_2> >::type>
                   (q1.value()*q2.value());
}

However this throws a multitude of incomprehensible compiler errors.

Question:

My question is then, am I using decltype in the correct way but missing some syntax eg a typename specifier somewhere? Or; is it even possible to do this this way and if not how is it possible to compute the return type of a function?

Thanks.

Ok, so first the type return_type<T1,T2>::type is not what you seem to be expecting but is the type of a method, namely T3 (return_type::*)(const T1&, const T2&) with T3 being the type you are expecting. If you want to use an intermediate class, you can use:

template <typename T1, typename T2>
struct return_type
{
  typedef decltype(std::declval<T1>()*std::declval<T2>()) type;
};

But you could also use directly decltype(T1()*T2()) to get the type of the product.

Edit : I edited the code with the suggestion of ildjarn, so no need for default-constructible types. Just don't forget to include <utility>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM