2013-10-29 150 views
1

我试图做这样的事情:功能模板参数编译错误

#include <iostream> 
#include <array> 
using namespace std; 

template <size_t A> 
class Test { 
    public: 
     typedef array<int, A> TestType; 
}; 

template <size_t A> 
void foo(Test<A>::TestType t) { 
    cout << "test\n"; 
} 

int main() { 
    Test<5>::TestType q; 

    foo(q); 
    return 0; 
} 

但富不编译。在GCC我得到

prog.cpp:12:19: error: variable or field ‘foo’ declared void 
void foo(Test<A>::TestType t) { 
       ^
prog.cpp:12:28: error: expected ‘)’ before ‘t’ 
void foo(Test<A>::TestType t) { 

而在Visual Studio 2010中,我得到

error C2975: 'Test' : invalid template argument for 'A', expected compile-time constant expression 

我不明白我在做什么错误的,因为A是一个编译时间常数。我应该改变什么?

回答