以下代码无法编译......
namespace {
template<typename T, template<typename> class D>
struct Base {
Base(const T& _t) : t(_t) { }
T t;
};
template<typename T>
struct Derived : Base<T, Derived> {
Derived(const T& _t) : Base<T, Derived>(_t) { }
};
}
int main(int argc, char* argv[]) {
Derived<int> d(1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该行有一个编译错误 - Derived(const T& _t) : Base<T, Derived>(_t) { }
错误C3200''anonymous-namespace':: Derived':模板参数'D'的模板参数无效,需要一个类模板
如果我提供任何其他具有模板参数而不是Derived本身的类,则此方法有效
template<typename T>
struct Other {
};
template<typename T>
struct Derived : Base<T, Other> {
Derived(const T& _t) …Run Code Online (Sandbox Code Playgroud) 假设你有代码
template <template<class> class BaseType>
class EST16
: public BaseType<int>
{
public:
EST16(double d)
{
}
};
template <class T>
class SCEST
{
T y;
};
typedef EST16<SCEST> EST16_SC;
class Child
: public EST16_SC
{
public:
Child()
: EST16_SC(1.0)
{
}
};
class NotWorkingChild
: public EST16<SCEST>
{
public:
NotWorkingChild()
: EST16<SCEST>(1.0)
{
}
};
TEST(TemplateTest, TestInstantiate)
{
Child child;
NotWorkingChild notWorkingChild;
}
Run Code Online (Sandbox Code Playgroud)
Child和NotWorkingChild仅因typedef而异.在GCC中都编译,在Visual Studio中NotWorkingChild的构造函数产生以下错误:
2>..\..\..\src\itenav\test\SCKFErrorStateTest.cpp(43) : error C3200: 'SCEST<T>' : invalid template argument for template parameter 'BaseType', expected a …Run Code Online (Sandbox Code Playgroud)