是从template<int, int>组成template<int>的一种方法。
我尝试了以下代码,但它不能编译:
#include <iostream>
template<int N, int M>
struct A { enum E { n = N, m = M }; };
template<template<int> class C>
struct B : public C<8> { };
int main(int argc, const char *argv[])
{
typedef B< A<4> > T;
T b;
std::cout << T::n << std::endl;
std::cout << T::m << std::endl;
return 0;
}错误:
test3.cxx: In function ‘int main(int, const char**)’:
test3.cxx:10:23: error: wrong number of template arguments (1, should be 2)
test3.cxx:3:12: error: provided for ‘template<int N, int M> struct A’
test3.cxx:10:25: error: template argument 1 is invalid
test3.cxx:10:28: error: invalid type in declaration before ‘;’ token
test3.cxx:13:22: error: ‘T’ is not a class or namespace
test3.cxx:14:22: error: ‘T’ is not a class or namespace发布于 2012-01-07 05:26:11
至少在C++03中,模板模板参数必须具有所需的参数数量。
要实现这一点,一种方法是创建一个新模板,该模板具有委托给原始模板的正确的属性:
template<int M>
struct Hack : A<4, M> { };
...
typedef B<Hack> T;发布于 2012-01-07 05:25:15
下面的代码打印出4和8,我希望我正确地推断了你的意思。template-template中的参数数量与您传入的模板数量不匹配。
#include <iostream>
template<int N, int M>
struct A { enum E { n = N, m = M }; };
template<template<int, int> class C, int Num>
struct B : public C<Num, 8> { };
int main(int argc, const char *argv[])
{
typedef B< A, 4 > T;
T b;
std::cout << T::n << std::endl;
std::cout << T::m << std::endl;
return 0;
}https://stackoverflow.com/questions/8764653
复制相似问题