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

The Archive Base Latest Questions

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

I want a template to select from two types based on some condition. E.g.

  • 0

I want a template to select from two types based on some condition. E.g.

struct Base {};

template <typename T1, typename T2>
struct test
{
    // e.g. here it should select T1/T2 that is_base_of<Base>
    typename select_base<T1, T2>::type m_ValueOfBaseType;
};

Of course to pass condition to the select_base (to make it generic) would be useful, but hard-coded solution is easier and good as well.

Here’s a sample solution that I tried but it always selects T1: http://ideone.com/EnVT8

The question is how to implement the select_base template.

  • 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:49:08+00:00Added an answer on June 5, 2026 at 10:49 pm

    C++14 (and onwards):

    template <typename T, typename U>
    struct select_base:
        std::conditional_t<std::is_base_of<T, Base>::value, T, U> {};
    

    In the same vein, you can instead use this:

    template<typename T, typename U>
    using select_base = std::conditional_t<std::is_base_of_v<T,Base>, T, U>;
    

    The difference between these two approaches can be observed when you use them. For example, in the first case if you have to use ::type whereas in the second, you dont. And if any dependent type involves in the usage of the first approach, you have to use typename as well, to assist the compiler. The second approach is free of all such noises and thus superior to the rest of the approaches in this answer.

    Also, please note that you can write similar type-alias in C++11 as well.


    C++11:

    template <typename T, typename U>
    struct select_base:
        std::conditional<std::is_base_of<T, Base>::value, T, U>::type {};
    //                 ^                                       ^~~~~~
    

    C++98:

    Conditional is easy enough:

    template <typename bool, typename T, typename U>
    struct conditional { typedef T type; };
    
    template <typename T, typename U>
    struct conditional<false, T, U> { typedef U type; };
    

    is_base_of is slightly more complicated, an implementation is available in Boost that I will not reproduce here.

    Afterwards, see C++11.

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

Sidebar

Related Questions

Simple question, I just want to select the text from the <Template> tag. Here's
I'm using DataTemplateSelector with the WPFToolkit DataGrid. I want to select the editing template
I want to render same template on one page which should display the value
I want to do something like template <typename T> void foo(const T& t) {
I want to use a custom template file which shoud use the base layout
I'm using the JDBC template and want to read from a database using prepared
here is my below xml under that table due based on CTD_CTD_PKG_ID(some time under
So i want to create: select * from Post where Post.is_chosen = true order_by
I am creating a custom control. I want the template for this control to
I want to have a template class that looks something like what I have

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.