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

  • Home
  • SEARCH
  • 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 8812697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:39:49+00:00 2026-06-14T03:39:49+00:00

I need a template expression that selects the first argument type if the first

  • 0

I need a template expression that selects the first argument type if the first argument is defined, else the second argument type.

select<int, float>::type               // type evaluates to int
select<an_undefined_type, float>::type // type evaluates to float

… and the solution has to work with C++03 and Boost 1.33.1 🙁

My goal is to accept both int and boost::optional<int> as a function template parameter T, so I can do something like:

template<typename T>
void fn(T& t)
{
    int x = std::numeric_limits<select<T::value_type, T>::type>::digits;
}

since boost::optional<int>::value_type is defined.

C++11 solutions are also appreciated.

I don’t see a way to do this with template specialization, since I’m trying to specialize not on types but on concepts. Basically, I would need one specialization that matches the any_integer concept and one specialization that matches the boost::optional<any_integer> concept.

I guess with C++11 I could accomplish this specific goal with:

std::conditional<std::is_integral<T>::value, T, T::value_type>::value

but I don’t have C++11, and I want the more general solution.

  • 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-14T03:39:50+00:00Added an answer on June 14, 2026 at 3:39 am

    I don’t think you can achieve the exact notation you are looking for. However, I think you can use a slightly different notation to achieve what you are semantically after. The problem with your current notation

    int x = std::numeric_limits<select<T::value_type, T>::type>::digits;
    

    is that select<T0, T1> expects two types, i.e., the requirement for the types to be present isn’t on the select<T0, T1> template but on the function calling it. The way I would change this is to use

    int x = std::numeric_limits<select<typename get_value_type<T>::type, T>::type>::digits;
    

    Now all what needs to happen is to have a get_value_type<T> which yields the nested type if present and some type select<T0, T1> is going to ignore if arrives there, e.g., void (or a custom marker type). The get_value_type<T> template should be fairly simple (I saw Dirk Holsopple’s answer but I couldn’t get it work):

    template <typename T>
    struct has_value_type
    {
        typedef char (&true_type)[1];
        typedef char (&false_type)[2];
        template <typename D> static true_type test(typename D::value_type*);
        template <typename D> static false_type test(...);
    
        enum { value = sizeof(test<T>(0)) == 1 };
    };
    
    template <typename T, bool = has_value_type<T>::value >
    struct get_value_type
    {
        typedef T type; // EDIT
    };
    
    template <typename T>
    struct get_value_type<T, true>
    {
        typedef typename T::value_type type;
    };
    

    Obviously, you might want to define your type-traits slightly different so you can use something like

    int x = std::numeric_limits<typename get_type<T>::type>::digits;
    

    This would return the nested type if there is a value_type and a type T otherwise.

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

Sidebar

Related Questions

I've defined a DataTemplate in a ResourceDictionary. The template need some data (for populating
I'm writing an XSLT template that need to output a valid xml file for
I need a url for using that for a template. Now there are two
In C++0x SFINAE rules have been simplified such that any invalid expression or type
I need a regular expression so that if I search for > greater than.
I'm a regex newbie and need a single expression that: matches the an and
For an XSLT template, I need an XPath expression targeting elements which have a
I often find the need to include away and present type visual states that
I need very basic template engine, which is incapsulated in single class. So I
In my Django template I need to return the index of an item in

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.