在类成员函数内传递模板类作为模板模板参数

Passing template class as template template parameter inside class member function

本文关键字:参数 成员 函数      更新时间:2023-10-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的名称查找会找到注入的类名,而您使用的古代编译器不支持将其用作模板名称。

限定名称。

A< ::C,B> a;