How to ensure that the template parameter is a subtype of a desired type?

前端 未结 3 1467
粉色の甜心
粉色の甜心 2020-11-30 07:49

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 d
相关标签:
3条回答
  • 2020-11-30 08:17

    From "Modern C++ Design", chapter 2.7 ("Detecting Convertibility and Inheritance at Compile Time"): you can use a sizeof trick:

    typedef char Small;
    class Big { char dummy[2]; };
    
    Small Test(Base1);
    Big Test(...);
    
    const bool isSubclassOfBase1 = sizeof(Test(Derived1())) == sizeof(Small);
    

    It exploits the fact that sizeof(...) can figure out the type that the expression evaluates to and since the return values have different sizes, the == check evaluates to true or false. If Derived1 is indeed the base of Base1, Small Test(Base1); overload is chosen and isSubclassOfBase1 will be true.

    Extending from that, you can encapsulate a check and do a static assertion to make it fail at compile time:

    #include <boost/static_assert.hpp>
    
    class A {};
    class B: public A {};
    class C {};
    
    template<class Base, class Derived>
    struct SubclassCheck
    {
        typedef char Yes;
        class No { char dummy[2]; };
    
        static Yes Test(Base);
        static No Test(...);
        enum {
            Value = (sizeof(Test(*(Derived*)NULL)) == sizeof(Yes))
        };
    };
    
    #define CHECK_DERIVES(b,d)\
        BOOST_STATIC_ASSERT((SubclassCheck<b,d>::Value));
    
    int
    main()
    {
        CHECK_DERIVES(A, B);
        // CHECK_DERIVES(A, C); // fails
    }
    

    You can use any other static assertion implementation, not necessarily Boost's.

    0 讨论(0)
  • 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 讨论(0)
  • 2020-11-30 08:26

    Yes, this is taken care of automatically in that if the parameter doesn't support the things you do with it, a compilation error will result. Manually checking if a type is a subtype of another type can only (AFAIK) be done at runtime, which is far later than compile time. I don't know what you mean by that error being detected late, compile time is as early as it gets. Also, if everyone checked the type of their template parameters, the STL couldn't work with pointers as well as actual class-based iterators, and wouldn't be nearly as flexible.

    As for letting your user know the requirements, simply provide them in the documentation.

    0 讨论(0)
提交回复
热议问题