Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8100089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:38:59+00:00 2026-06-05T22:38:59+00:00

Info: I’m currently trying to learn template metaprogramming (by following this book ). One

  • 0

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 e.g. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:39:00+00:00Added an answer on June 5, 2026 at 10:39 pm

    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>

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

info: iOS5, xcode4.3.2, iphone5 Create tab view controller application from xcode template wizard. Following
info: xcode 4.3.2, iOS5, using storyboard. Created project from xcode's Tabbed Application template. Did:
Background info: Me and a couple of friends are building this platform game in
Background info: We are currently 3 web programmers (good, real-life friends, no distrust issues).
Following info I found on another SO question, I have improved my autocomplete to
THE INFO Currently I have two tables I am working with- a POST table
INFO I'd like to use boost::function to pass callback as parameter, like this way:
Background info I am trying to upgrade a custom CMS to support the HTML5
the info file is right,the following is my module file code. when i access
$(.info-icon).hover(function() { var a = this.id; var id = a.substring(10); var $id = infobox-+id;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.