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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:44:48+00:00 2026-05-24T15:44:48+00:00

I have a template class, what I want to do are the following Make

  • 0

I have a template class, what I want to do are the following

  1. Make sure that an object is instantiated only if the template parameter passed is a subtype of a desired type
  2. Communicate to the user of the code upfront what is it that the template parameter must satisfy

(1) is sort of taken care automatically in the sense if the template parameter passed does not support some feature that the class uses the code will not compile. But this error may be detected fairly late. I want the checks to be as early as possible. What I also want to accomplish is that it should be obvious rightaway that template parameter that is passed has to be derived from a base type that I provide.

First, is this misguided ? and if not how shall I do this ? (the simplest way please, C++ is still new to me)

Thanks stackoverflow, you have really speeded up my C++ learning rate.

  • 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-05-24T15:44:50+00:00Added an answer on May 24, 2026 at 3:44 pm

    Given some complete type MyBase, the following will yield a compile-time error if T is not derived from MyBase:

    #include <boost/type_traits/is_base_of.hpp>
    #include <boost/static_assert.hpp>
    
    template<typename T>
    class Foo {
        BOOST_STATIC_ASSERT_MSG(
            (boost::is_base_of<MyBase, T>::value),
            "T must be a descendant of MyBase"
        );
        // Foo implementation as normal
    };
    

    If you’re using a C++03 compiler with TR1, you can use std::tr1::is_base_of instead of boost::is_base_of; if you’re using a C++11 compiler, you can use std::is_base_of instead of boost::is_base_of and the static_assert keyword instead of the BOOST_STATIC_ASSERT_MSG macro:

    #include <type_traits>
    
    template<typename T>
    class Foo {
        static_assert(
            std::is_base_of<MyBase, T>::value, 
            "T must be a descendant of MyBase"
        );
        // Foo implementation as normal
    };
    

    N.b. this will yield true_type for privately and ambiguously-derived types, so this is insufficient if what you really need is to treat T as-a MyBase (in most contexts).

    Doc links:
    Boost.StaticAssert
    Boost.TypeTraits

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

Sidebar

Related Questions

I want to have a template class that looks something like what I have
I have some code (that is working), but I just want to make sure
I have a templated class that I want to avoid copying (because of the
I have a template class method like that: template<class T> static tmpClass<T>* MakeInstance(T value)
I have a template class with a variadic template member function that I am
I have a file that contains the following: #include <map> class A {}; void
Suppose I have the following class: template <typename T> class MyClass { public: void
I have a class similar to the following: class SomeClass { public: template<typename... Args>
I have a template class and I want to add instances of this class
If I want to make a template class, and depending on the typeid of

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.