2013-01-10 37 views
1

考虑下面的代码:Constexpr建设和静态成员不工作

#include <iostream> 
#include <type_traits> 

template<typename Type> 
class Test 
{ 
    public: 
     constexpr Test(const Type val) : _value(val) {} 
     constexpr Type get() const {return _value;} 
     static void test() 
     { 
      static constexpr Test<int> x(42); 
      std::integral_constant<int, x.get()> i; 
      std::cout<<i<<std::endl; 
     } 
    protected: 
     Type _value; 
}; 

int main(int argc, char *argv[]) 
{ 
    Test<double>::test(); 
    return 0; 
} 

在G ++ 4.7.1,它会返回错误:

main.cpp: In static member function ‘static void Test<Type>::test()’: 
main.cpp:13:48: error: invalid use of ‘Test<Type>::get<int>’ to form a pointer-to-member-function 
main.cpp:13:48: note: a qualified-id is required 
main.cpp:13:48: error: could not convert template argument ‘x.Test<Type>::get<int>()’ to ‘int’ 
main.cpp:13:51: error: invalid type in declaration before ‘;’ token 

我不明白的问题:是编译器错误还是真正的问题? 如何解决它?

回答

1

它看起来像一个GCC bug,铛3.2 compiles without any error

约MSVC
+2

什么?哦,等等,'constexpr',哈! – chris

+0

好的,我只是把这个bug添加到g ++ bugtracker中:http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55931 – Vincent

+0

@Vincent谢谢,虽然在GCC有点失望,[第二个bug在3天](http://stackoverflow.com/questions/14192936/template-non-type-arguments/14193677#comment19676817_14193677) –