与模板类的静态变量冲突的声明

问题描述:

我尝试定义类范围之外的静态变量,如:与模板类的静态变量冲突的声明

template<typename T> 
struct Foo { 
    void set(int i) { 
    } 
    static constexpr decltype(&Foo<T>::set) i = &Foo<T>::set; 
}; 

template<typename T> 
constexpr decltype(&Foo<T>::set) Foo<T>::i; 

Live example.

,但我得到以下错误(所有GCC> = 4.7):

conflicting declaration 'constexpr decltype (& Foo<T>::set(int)) Foo<T>::i' 
note: previous declaration as 'constexpr decltype (& Foo<T>::set(int)) Foo<T>::i' 

所有clang版本(clang> = 3.2)对我的代码没有任何问题。

这个问题似乎是函数的参考。它的工作原理没有使用模板类。

我的问题:

  • 它是一个错误吗?
  • 如何在gcc中做到这一点?

我不知道这是否是一个错误或没有,但你可以做这样的:

template<typename T> 
struct Foo { 
    void set(int i) { 
    } 

    typedef decltype(&Foo<T>::set) function_type; 
    static constexpr function_type i = &Foo<T>::set; 
}; 

template<typename T> 
constexpr typename Foo<T>::function_type Foo<T>::i; 

int main() 
{ 
    Foo<int> f; 
}