How to ensure that the template parameter is a subtype of a desired type?
I have a template class, what I want to do are the following
- Make sure that an object is instantiated only if the template parameter passed is a subtype of a desired type
- 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.
Solution 1:
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
Solution 2:
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.