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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:03:36+00:00 2026-06-14T17:03:36+00:00

Consider this class template: template <typename T1, typename T2, bool B> class SomeClass {

  • 0

Consider this class template:

template <typename T1, typename T2, bool B>
class SomeClass { };

Now, I’d like to provide two implementations based on B==true and B==false. That is, I’d like to say something like:

template <ANYTHING, ANYTHING, true> class SomeClass {
// First implementation
};

template <ANYTHING, ANYTHING, false> class SomeClass {
// Second implementation
};

How can this be done in C++(11)?

  • 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-14T17:03:38+00:00Added an answer on June 14, 2026 at 5:03 pm

    With partial specialization:

    // primary
    template<typename X, typename Bool>
    struct Foo;
    
    template<typename X>
    struct Foo<X, std::true_type> {};
    
    template<typename X>
    struct Foo<X, std::false_type> {};
    
    // use
    Foo<X, std::true_type> x;
    

    I use a type-wrapper for bool, but you can also do that with
    non-type template parameters:

    // primary
    template<typename, bool>
    struct Foo;
    
    template<typename X>
    struct Foo<X, true> {};
    
    template<typename X>
    struct Foo<X, false> {};
    
    // use
    Foo<X, true> x;
    

    Sometimes you can compute the value used for partial specialization
    with meta-programming in the default argument:

    // primary
    template<typename X, typename is_integral_ = std::is_integral<X>::type>
    struct Foo;
    

    This makes the configuration variable overridable by user choice.

    struct my {};
    Foo<my, std::true_type> x;
    

    To prevent that, dispatch through inheritance:

    // primary, where Foo_impl is any of the above
    template<typename X>
    struct Foo : public Foo_impl<X> {};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider: template <typename T> class Base { public: static const bool ZEROFILL = true;
Consider something like... template<typename T> class Vector { ... bool operator==( const Vector<float> &rhs
Consider this code: template<typename T> class Base { template<typename U> friend void f(void *ptr)
Consider this code: template <typename T> class String { public: ... String(T* initStr) {
Consider this overly simple test: class foo { public: foo(int i); template< typename T
Please, consider the code below: template<typename T> bool function1(T some_var) { return true; }
Consider this template: template< typename T, typename RefT = T& > class foo {
Consider this one template<typename T> struct A { enum class X { V =
Consider a template class like: template<typename ReturnType, ReturnType Fn()> class Proxy { void run()
Consider the following code : template<typename T> class Base { Base(); Base(const Base<T>& rhs);

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.