假设我有课程:
class Base{};
class A: public Base{
int i;
};
class B:public Base{
bool b;
};
现在我要定义一个模板化的类:
template < typename T1, typename T2 >
class BasePair{
T1 first;
T2 second;
};
但是我想定义它,只有类Base的后代可以用作模板参数。
我该怎么做?
答案 0 :(得分:41)
C ++ 11引入了<type_traits>
template <typename T1, typename T2>
class BasePair{
static_assert(std::is_base_of<Base, T1>::value, "T1 must derive from Base");
static_assert(std::is_base_of<Base, T2>::value, "T2 must derive from Base");
T1 first;
T2 second;
};
答案 1 :(得分:12)
更确切地说:
class B {};
class D1 : public B {};
class D2 : public B {};
class U {};
template <class X, class Y> class P {
X x;
Y y;
public:
P() {
(void)static_cast<B*>((X*)0);
(void)static_cast<B*>((Y*)0);
}
};
int main() {
P<D1, D2> ok;
P<U, U> nok; //error
}
答案 2 :(得分:9)
C ++还没有直接允许这个。您可以通过在类中使用STATIC_ASSERT
和type checking间接实现它:
template < typename T1, typename T2 >
class BasePair{
BOOST_STATIC_ASSERT(boost::is_base_of<Base, T1>);
BOOST_STATIC_ASSERT(boost::is_base_of<Base, T2>);
T1 first;
T2 second;
};
答案 3 :(得分:2)
这是一个很好的问题!在通过这个link进行研究的同时,我想出了以下内容,但与之提供的解决方案并没有太大的不同。每天学习一些东西......检查!
#include <iostream>
#include <string>
#include <boost/static_assert.hpp>
using namespace std;
template<typename D, typename B>
class IsDerivedFrom
{
class No { };
class Yes { No no[3]; };
static Yes Test(B*); // declared, but not defined
static No Test(...); // declared, but not defined
public:
enum { IsDerived = sizeof(Test(static_cast<D*>(0))) == sizeof(Yes) };
};
class Base
{
public:
virtual ~Base() {};
};
class A : public Base
{
int i;
};
class B : public Base
{
bool b;
};
class C
{
string z;
};
template <class T1, class T2>
class BasePair
{
public:
BasePair(T1 first, T2 second)
:m_first(first),
m_second(second)
{
typedef IsDerivedFrom<T1, Base> testFirst;
typedef IsDerivedFrom<T2, Base> testSecond;
// Compile time check do...
BOOST_STATIC_ASSERT(testFirst::IsDerived == true);
BOOST_STATIC_ASSERT(testSecond::IsDerived == true);
// For runtime check do..
if (!testFirst::IsDerived)
cout << "\tFirst is NOT Derived!\n";
if (!testSecond::IsDerived)
cout << "\tSecond is NOT derived!\n";
}
private:
T1 m_first;
T2 m_second;
};
int main(int argc, char *argv[])
{
A a;
B b;
C c;
cout << "Creating GOOD pair\n";
BasePair<A, B> good(a, b);
cout << "Creating BAD pair\n";
BasePair<C, B> bad(c, b);
return 1;
}
答案 4 :(得分:0)
首先,修复声明
template < class T1, class T2 >
class BasePair{
T1 first;
T2 second;
};
然后,您可以在基类中声明一些私有函数Foo();并告诉Base类将BasePair作为朋友;然后在朋友构造函数中你只需要调用这个函数。这样,当有人试图将其他类用作模板参数时,您将收到编译时错误。
答案 5 :(得分:0)
class B
{
};
class D : public B
{
};
class U
{
};
template <class X, class Y> class P
{
X x;
Y y;
public:
P()
{
(void)static_cast<X*>((Y*)0);
}
};
答案 6 :(得分:0)
在未知(雅虎)建议的答案中,不需要实际上将X和Y型变量作为成员。这些行在构造函数中就足够了:
static_cast<B*>((X*)0);
static_cast<B*>((Y*)0);