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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:29:45+00:00 2026-06-11T19:29:45+00:00

At compile time in C++11 in a template function that takes 2 template parameters,

  • 0

At compile time in C++11 in a template function that takes 2 template parameters, both of which must be unsigned integer types, I’d like to have a local variable have the type of whichever of the two template parameters has more bits. In C++03 I might write something like:

template<bool, class T, class U>
struct pick_first;

template<class T, class U>
struct pick_first<true, T, U> {
    typedef T type;
};

template<class T, class U>
struct pick_first<false, T, U> {
    typedef U type;
};

template<class T, class U>
struct pick_bigger {
    typedef typename pick_first<(sizeof(T) >= sizeof(U)), T, U>::type type;
};

// usage
template<class uintX_t, class uintY_t>
void foo() {
    typename pick_bigger<uintX_t, uintY_t>::type mylocal = 0;
    // insert doing stuff with mylocal here
}

Can I leverage any of the new C++11 features to make this simpler? I know I could use variadic templates to make it work with more than just pairs of types, and instead of using pick_first I could write lots of specializations to make it work with the new int_leastX_t and int_fastX_t types. But I’m curious if there’s just a plain better approach to this. Maybe somehow leveraging auto/constexpr/decltype?

  • 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-11T19:29:46+00:00Added an answer on June 11, 2026 at 7:29 pm

    Your pick_first is just std::conditional in C++11, so you could write

    template<class T, class U>
    struct wider {
        using type = typename std::conditional<sizeof(T) >= sizeof(U), T, U>::type; // I'm using the C++11 type alias feature 1) to educate people about them and 2) because I like them better than typedefs.
    };
    

    If you just want a type suitable for holding the result of some expression involving both types and don’t necessarily need exactly one of the two types then std::common_type, or perhaps auto, is the best solution:

    template<class uintX_t, class uintY_t>
    void foo() {
        typename std::common_type<uintX_t, uintY_t>::type mylocal = 0;
        // insert doing stuff with mylocal here
    }
    
    // or
    template<class uintX_t, class uintY_t>
    void foo(uintX_t x, uintY_t y) {
        auto mylocal = x + y;
    }
    

    and your implementation of pick_bigger is missing a typename in there: typedef typename pick_first<(sizeof(T) >= sizeof(U)), T, U>::type type;

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

Sidebar

Related Questions

I have a template function and wish to ensure at compile time that it
I have a few classes that define sequences whose values must be available both
I have a question regarding a call to a function that takes quite a
I have a matrix class with the size determined by template parameters. template <unsigned
In some C++ code, I am encountering a compile-time error that seems like it
We get compile time error when integer is divided by zero whereas in case
If dynamic resolves to object at compile time, and all .NET types extend object,
I'd like to obscure strings at compile time. I know it can be done
The following code throws an compile-time error like Cannot convert type 'string' to 'int'
I have a superclass which is defined in terms of a few internal types

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.