I have a template class, what I want to do are the following
(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.
There are ways to restrict the types you can use inside a template you write by using specific typedefs inside your template. This will ensure that the compilation of the template specialisation for a type that does not include that particular typedef will fail, so you can selectively support/not support certain types.
For example, given a specialization Stack<int>, “int” is a template argument. Instantiation: This is when the compiler generates a regular class, method, or function by substituting each of the template's parameters with a concrete type.
A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.
A template non-type parameter is a template parameter where the type of the parameter is predefined and is substituted for a constexpr value passed in as an argument. A non-type parameter can be any of the following types: An integral type. An enumeration type.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With