2013-10-29 157 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是一個編譯時間常數。我應該改變什麼?

回答