【问题标题】:Passing template class as template template parameter inside class member function在类成员函数中将模板类作为模板模板参数传递
【发布时间】:2016-07-22 08:17:16
【问题描述】:

我有一个项目需要同时使用 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++ c++98 template-templates


    【解决方案1】:

    C 的名称查找找到注入的类名称,而您使用的古老编译器不支持将其用作模板名称。

    限定名称。

    A< ::C,B> a;
    

    【讨论】:

    • 哇!!我不知道!该死的编译器一定是陈旧的。谢谢你,你是一个救生员
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 2013-11-06
    • 2013-11-08
    • 2015-04-02
    相关资源
    最近更新 更多