0

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则此错误消失。

哪里有问题?

4

1 回答 1

7

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

t.template select<3>();
于 2013-01-29T17:28:08.903 回答