我有一个项目需要同时使用 GCC-4.4.7 和 GCC-4.9.0 进行编译。
我们正在使用将模板化类作为模板模板参数传递给另一个类的代码。虽然代码在 GCC-4.9.0 上编译良好,但在 GCC-4.4.7 上却失败了。
这里是错误的重现:
#include <iostream>
using namespace std;
struct E
{
int a;
E(int b) : a(b) {}
};
template<template<int B> class C, int D> struct A
{
void print()
{
E e(D);
cout << e.a << endl;
}
int a;
};
template<int B> struct C
{
const static int a = B;
void print()
{
A<C, B> a;
a.print();
}
};
int main() {
C<3> c;
c.print();
return 0;
}
关于编译:
[swarup@localhost ~]$ g++-4.9 Test.cpp -o Test
[swarup@localhost ~]$
[swarup@localhost ~]$ g++-4.4 Test.cpp -o Test
Test.cpp:43: error: type/value mismatch at argument 1 in template parameter list for ‘template<template<int B> class C, int D> struct A’
Test.cpp:43: error: expected a class template, got ‘C<B>’
Test.cpp:43: error: invalid type in declaration before ‘;’ token
Test.cpp:44: error: request for member ‘print’ in ‘a’, which is of non-class type ‘int’
如何改正错误并正确使其与GCC-4.4.7编译?
注意:仅 C++98 标准,代码非常古老。
最佳答案
C
的名称查找查找注入(inject)的类名,而您使用的旧编译器不支持将其用作模板名称。
限定名称。
A< ::C,B> a;
关于c++ - 在类成员函数中将模板类作为模板模板参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38521502/