我有这个简单的代码:
template<template <class> class Generator>
class TestHelper {};
template<class Writer>
class Test
{
typedef TestHelper< Test > Helper;
};
Run Code Online (Sandbox Code Playgroud)
它在最新的g ++版本上运行良好,但是,在4.4或4.5中,我收到此错误:
test.cpp:7: error: type/value mismatch at argument 1 in template parameter list for 'template<template<class> class Generator> class TestHelper'
test.cpp:7: error: expected a class template, got 'Test<Writer>'
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
Ben*_*igt 12
这是因为在类的主体内部Test<Writer>
,Test
没有提供模板参数的命名会自动采用相同的参数(例如Writer
).
例如,这允许您将复制构造函数编写为:
Test(const Test&);
Run Code Online (Sandbox Code Playgroud)
代替
Test::Test(const Test<Writer>&);
Run Code Online (Sandbox Code Playgroud)
您可以通过Test
使用其命名空间进行限定来克服此问题,例如
typedef TestHelper< ::Test > Helper;
Run Code Online (Sandbox Code Playgroud)
注意:正如Tomalek建议的那样,原始用法在C++ 0x中有效.以下是标准(强调我的)的相关段落,来自第14.6.1节([temp.local]
):
与普通(非模板)类一样,类模板具有注入类名(第9节).inject-class-name可以用作模板名称或类型名称.当它与template-argument-list一起使用时,作为模板模板参数的模板参数,或作为友元类模板声明的详细类型说明符中的最终标识符,它引用类模板本身.否则,它等同于template-name,后跟<>中包含的类模板的template-parameters.