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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:50:05+00:00 2026-06-10T04:50:05+00:00

Can I specify exactly what kind of arguments a template can receive? For example,

  • 0

Can I specify exactly what kind of arguments a template can receive? For example, I’d like to create a template that can only be instantiated with classes that are or extend class A. In Java, generics support this with:

class B<T extends A> { }

Can something similar be achieved with templates in C++?

template <typename T (?)> class B { }
  • 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-10T04:50:06+00:00Added an answer on June 10, 2026 at 4:50 am

    There are two ways to do this.

    First, through a hidden dummy template parameter that uses std::enable_if with a std::is_base_of<A, T>::value as condition. If the latter expression evaulates to false, then the nested type does not exist in std::enable_if. If you were using this on overloaded functions, SFINAE then means “substitution failure is not an error”, and the overload in question would be removed from the set of viable functions. HOwever in this situation, there is no other class template to match your call, and then you do get a compile-time error.

    SFINAE is a very subtle mechanism and easy to get wrong. E.g. if you have multiple class specializations with different SFINAE conditions, you have to make sure that they are all non-overlapping, or else you get an ambiguity.

    Second, you can do a simple static_assert with a std::is_base_of<A,T>::value inside the body of the class. The advantage of this method is that you also specify a more readable error message compared to the SFINAE method. A disadvantage is that you always get an error, and you cannot silently suppress this particular template and select another one. But overall I think this method is recommended in your case.

    #include<type_traits>
    
    class A {};
    class C: public A {};
    class D {};
    
    // first alternative: SFINAE on hidden template parameter
    template
    <
        typename T, 
        typename /* dummy */ = typename std::enable_if< 
            std::is_base_of<A, T>::value
        >::type
    >
    class B
    {
    };
    
    // second alternative: static_assert inside class
    template
    <
        typename T
    >
    class E
    {
        static_assert(std::is_base_of<A, T>::value, "A should be a base of T");
    };
    
    int main()
    {
        B<A> b1;
        B<C> c1;
        //B<D> d1; // uncomment this line to get a compile-time error
    
        E<A> b2;
        E<C> c2;
        //E<D> d2; // uncomment this line to get a compile-time error
    
        return 0;
    }
    

    As was pointed out in the comments, you can use either a decent C++11 compiler (VC++ 2010 or later, gcc 4.5 or later) or the Boost or TR1 libraries to get the <type_traits> functionality. Note however that the std::is_base_of<A, A>::value evaluates to true, but the old boost::is_base_of<A, A>::value used to evalute to false.

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

Sidebar

Related Questions

I'd like to specify an SVG linear gradient in a way that exactly duplicates
We can specify a derived from constraint on generic type parameters like this: class
In code one can specify globally accessible constants/enums/etc once that can then be reused
I know that in Java you can specify the return type of the DoInBackground
For example: Cardinality and optionality are orthogonal properties of a relationship. You can specify
I have the following classes: Defect - represents a type of data that can
I´m searching some providers or frameworks or something that can deliver a map like
Normally we can specify a target in the link or use javascript window.open to
I know you can specify a different layout file for orientation by creating a
in a java webstart file (jnlp) you can specify the nativelib tag to load

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.