c++ - 在类成员函数中将模板类作为模板模板参数传递

标签 c++ c++98 template-templates

我有一个项目需要同时使用 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/

相关文章:

c++ - 再次 double 四舍五入

c++ - 使用 enable_if 和 SFINAE 时,函数参数类型推导(标准容器,例如 vector )失败

C++ 部分模板 模板特化

c++ - 运行时在 boost::mpl::vector 中找到第一个匹配项

C++、ANTLR 和 VECTORS

c++ - 是否有像boost库中的boost::regex_search那样通过将数组作为任意参数来搜索单词数组的函数?

用于灰度到 ARGB 转换的 C++ SSE2 或 AVX2 内在函数

c++ - std::vector capacity/size/reserve 可以用于手动管理 vector 内存分配吗?

c++ - 用户定义的推导指南是否涉及模板模板参数作为指南标准的模板

c++ - 该程序中段错误背后的原因是什么?