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 8343323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:01:23+00:00 2026-06-09T06:01:23+00:00

I have a templated math function which takes two values, does some math to

  • 0

I have a templated math function which takes two values, does some math to them, and returns a value of the same type.

template <typename T>
T math_function(T a, T b) {
  LongT x = MATH_OP1(a,b);
  return MATH_OP2(x,a);
}

I want to store intermediate values (in x) in a type which is basically the long version of T (above, called LongT). So, if T is float, I want x to be a double; and if T is an int, I want x to be a long int.

Is there some way to accomplish this? I tried enable_if, but it seems that I would really need an enable_if_else.

I’d prefer to have the compiler figure out what to use for LongT on its own. I’d rather not have to specify it when I call the function.

  • 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-09T06:01:24+00:00Added an answer on June 9, 2026 at 6:01 am

    You can define a type mapping that will yield the needed type:

    template <typename T> struct long_type;
    template <> struct long_type<int> {
       typedef long type;
    };
    template <> struct long_type<float> {
       typedef double type;
    };
    

    And then use that metafunction:

    template <typename T>
    T math_function(T a, T b) {
      typename long_type<T>::type x = MATH_OP1(a,b);
      return static_cast<T>(MATH_OP2(x,a));
    }
    

    With this particular implementation, your template will fail to compile for any type other than the ones for which you have provided the long_type trait. You might want to provide a generic version that will just map to the itself, so that if the input is long long int that is what is used (assuming no larger type in your architecture).

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

Sidebar

Related Questions

I have a templated class which contains a static function which does not depend
Let's say I have some templated class depending on type T . T could
I have a templated function fct that uses some complex data structure based on
I have a templated function that operates on a template-type variable, and if the
I have a templated class Rect with conversion constructor which allows conversion between Rect
I have the following templated function... template< class T > T *create_object( lua_State *L
I have a templated class holding a value. Is it possible to add a
I have a templated function for returning the number of digits in a number:
I have a templated class that is dependent on two template parameters to calculate
Assume I have templated dropdown list in my gridview (which is bounded to all

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.