2017-03-01 10 views
2

私のようなクラスのスコープ外で静的変数を定義しよう:矛盾する宣言が

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で行うのですか?

答えて

1

それはバグだかどうかは分かりませんが、あなたはこのようにそれを行うことができます。

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; 
}