I wondering is there any way to set restrictions on template class?
Specify that every type substituted in template must have specific ancestor (realize some interface).template < class B > //and开发者_如何学运维 every B must be a child of abstract C
class A {
public:
B * obj;
int f() {
return B::x + this->obj->f();
}
};
Like => in haskell
func :: (Ord a, Show b) => a -> b -> c
A future version of C++ will support this natively using concepts (which didn't make it into C++11).
One way to approach the problem is to use specialisation on a dummy template parameter:
class C {};
template <class B, class dummy=void>
class A;
template <class B>
class A<B, typename enable_if<is_base_and_derived<C, B> >::type>
{
// class definition here
};
struct D : C {};
A<D> d; // fine
A<int> n; // compile error - undefined class A<B>
I've put stand-alone definitions of enable_if
and is_base_and_derived
here.
The following works in VC10 using static_assert. I've just seen this used and haven't really dug around much into what static_assert actually does - perhaps someone else can answer that.
#include <type_traits>
class Base
{
};
class Derived : public Base
{
};
class SomeRandomClass
{
};
template<typename T>
class A
{
static_assert(std::tr1::is_base_of<Base, T>::value, "T not derived from Base");
};
int _tmain(int argc, _TCHAR* argv[])
{
argc; argv;
//
// This will compile
A<Derived> a;
//
// This will throw a compilation error
A<SomeRandomClass> b;
return 0;
}
The compiler output being:
1>d:\temp\aaa\aaa\aaa.cpp(25): error C2338: T not derived from Base
1> d:\temp\aaa\aaa\aaa.cpp(41) : see reference to class template instantiation 'A<T>' being compiled
1> with
1> [
1> T=SomeRandomClass
1> ]
You can use BOOST_STATIC_ASSERT
or a similar library to assert your restrictions on the template parameter.
For example:
#include <limits>
#include <boost/static_assert.hpp>
template <class UnsignedInt>
class myclass
{
private:
BOOST_STATIC_ASSERT((std::numeric_limits<UnsignedInt>::digits >= 16)
&& std::numeric_limits<UnsignedInt>::is_specialized
&& std::numeric_limits<UnsignedInt>::is_integer
&& !std::numeric_limits<UnsignedInt>::is_signed);
public:
/* details here */
};
EDIT: For your example, you can write
template < class B >
class A {
BOOST_STATIC_ASSERT(boost::is_base_of<C, B>);
public:
B * obj;
int f() {
return B::x + this->obj->f();
}
};
You could use a trick like this (if you don't want to use Boost):
class Base
{
public:
static const int TEMPLATE_REQUIRES_BASE_CLASS = 0;
};
class Correct : public Base
{
};
class Incorrect
{
};
template <typename T>
class TMPL
{
static const int TEMPLATE_REQUIRES_BASE_CLASS = T::TEMPLATE_REQUIRES_BASE_CLASS;
T *m_t;
};
void main()
{
TMPL<Correct> one; // OK
TMPL<Incorrect> two; // Will not compile
}
The first line will compile. The second will not compile and will give the following error:
test.cpp
test.cpp(18) : error C2039: 'TEMPLATE_REQUIRES_BASE_CLASS' : is not a member of 'Incorrect'
test.cpp(12) : see declaration of 'Incorrect'
test.cpp(25) : see reference to class template instantiation 'TMPL<T>' being compiled
with
[
T=Incorrect
]
test.cpp(18) : error C2065: 'TEMPLATE_REQUIRES_BASE_CLASS' : undeclared identifier
test.cpp(18) : error C2057: expected constant expression
Templates are sort of duck typing in C++.
If your class supports everything that the template uses, then it can be used as template argument, otherwise it cannot.
If in your template you have something like
C *instance;
void foo(T *t)
{
instance = t;
}
then you're enforcing that T is derived from C (or at least assignement-compatible for pointers)
精彩评论