当我使用不是从 _base* 派生的类调用 foo 的构造函数时,我希望能够让编译器大喊大叫。当前代码仅允许 foo<_base*> 本身。任何简单的解决方案?
class _base
{
public:
// ...
};
class _derived: public _base
{
public:
// ...
};
template <typename T>
class foo
{
public:
foo () { void TEMPLATE_ERROR; }
};
template <> foo<_base*>::foo ()
{
// this is the only constructor
}
主代码:
foo<_base*> a; // should work
foo<_derived*> b; // should work (but doesnt)
foo<int*> c; // should not work (and infact doesnt)