我从具有部分专业化的模板继承,并且不能从派生的 ctor 调用模板 ctor。
当下面代码中的部分特化被注释掉时,它编译时不会出现任何错误或警告。
#include <iostream>
typedef enum {supertype, subtype} selector;
template< typename T, selector s>
class Tmpl {
protected:
T* root;
public:
Tmpl( T* t = 0 ) {
root = t;
}
T listHead( ) {
std::cout << "template listHead() called" << std::endl;
}
};
class Descriptor {
public:
Descriptor( const char * s ) {
std::cout << "Descriptor " << s << std::endl;
}
};
// partial specialization - if uncommented, errors
// are reported at the supertypesIterator ctor below.
/*
template<selector s>
class Tmpl<Descriptor, s> {
public:
Descriptor listHead( ) {
switch( s ){
case supertype:
return Descriptor("Supertypes");
case subtype:
return Descriptor("Subtypes");
}
}
};
*/
class supertypesIterator : public Tmpl<Descriptor, supertype> {
public:
supertypesIterator( Descriptor* t = 0 ):Tmpl<Descriptor, supertype>(t) {}
};
main() {
supertypesIterator s;
s.listHead();
}
如果我取消注释专业化,我会收到以下错误:
$g++ trouble.cc
trouble.cc: In constructor ‘supertypesIterator::supertypesIterator(Descriptor*)’:
trouble.cc:43:74: error: no matching function for call to ‘Tmpl<Descriptor, (selector)0u>::Tmpl(Descriptor*&)’
trouble.cc:43:74: note: candidates are:
trouble.cc:27:7: note: Tmpl<Descriptor, (selector)0u>::Tmpl()
trouble.cc:27:7: note: candidate expects 0 arguments, 1 provided
trouble.cc:27:7: note: Tmpl<Descriptor, (selector)0u>::Tmpl(const Tmpl<Descriptor, (selector)0u>&)
trouble.cc:27:7: note: no known conversion for argument 1 from ‘Descriptor*’ to ‘const Tmpl<Descriptor, (selector)0u>&’
我需要做什么才能从supertypesIterator
ctor 中初始化基类?
我正在使用 g++ version 4.7.1
,尽管我还需要它才能跨平台工作。