使用模板对象的模板函数中无法识别的内容

Something unrecognize in template function working with a template object

本文关键字:识别 函数 对象      更新时间:2023-10-16

Gcc (4.7.2) 在编译此代码时抛出一个小错误:

#include <iostream>
template<typename T>
struct test
{
    template<int n>
    int select() const
    {
        return n;
    }
};
template<typename T>
struct test_wrapper
{
    void print() const
    {
        std::cout << t.select<3>() << std::endl; // L.18
    }
    test<T> t;
};
int main()
{}

错误是:

test3.cpp: In member function 'void test_wrapper<T>::print() const':
test3.cpp:18:34: error: expected primary-expression before ')' token

如果我通过专用类型更改test<T> t,例如test<void> t,此错误会消失。

问题出在哪里?

模板化构造中调用模板方法时,需要使用 template 关键字:

t.template select<3>();