我希望能够在我调用foo的构造函数时使编译器大声呼叫,而类 不是从_base *派生的。目前的代码只允许foo < _base *>本身。任何 简单的解决方案?通过基类实现C++模板特化
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)
aaah,谢谢。我努力想通过自己潜入助推器,但我偶然发现了很多代码纠缠,所以我不得不撤退。但是,这正是我正在寻找的东西,谢谢。 – 2009-11-02 08:28:25
如果您想要了解更多信息,请参阅Andrei Alexandrescu的“Modern C++ Design:Generic Programming and Design Patterns Applied”一书。 – 2009-11-02 14:59:27
+1这是一个非常好的。并感谢您的书推荐! – 2009-11-03 00:30:00