Jan*_*cek 8 c++ templates metaprogramming template-meta-programming
C++允许模板化的模板参数,如下所示:
template <template <bool> class T>
struct something1 {};
Run Code Online (Sandbox Code Playgroud)
Bool类型可以用typedef替换(因此不需要在声明中出现原始类型名称):
typedef bool bool_t;
template <template <bool_t> class T>
struct something2 {};
Run Code Online (Sandbox Code Playgroud)
这很好用,但如果我尝试定义这样的嵌套结构:
template <typename Type>
struct enclosing
{
typedef bool bool_t;
typedef Type type_t;
template <template <bool_t> class T>
struct something3 {};
template <template <type_t> class T>
struct something4 {};
};
Run Code Online (Sandbox Code Playgroud)
然后以下代码无法编译:
template <bool Value>
struct param {};
typedef something1<param> x1; // ok
typedef something2<param> x2; // ok
typedef enclosing<bool>::something3<param> x3; // ok
typedef enclosing<bool>::something4<param> x4; // error
Run Code Online (Sandbox Code Playgroud)
这是符合标准的行为,还是我做错了什么?我正在使用MSVS 2008.
编辑:
我在微软支持论坛上发布了一个错误报告:
错误报告