1

それは...ですか

template <typename T>
void foo(T) {}

template <>
void foo(int) {}

明示的な特殊化または関数のオーバーロード、および明示的な初期化コンパイラについては、次のコードを見たいですか?

template <typename T>
void foo(T) {}

template <>
void foo<int>(int) {}

私は、標準がこれらの両方を受け入れると思います:

ISO/IEC 14882:2011

14.7.3 Explicit specialization [temp.expl.spec]

1 ...

can be declared by a declaration introduced by template<>; that is:
explicit-specialization:
template < > declaration
4

1 に答える 1

2

どちらでもかまいません。専攻では

template <>
void foo(int) {}

コンパイラはT = int、関数の引数からテンプレートの引数を推測します。14.7.3p10 から:

末尾のテンプレート引数は、関数の引数の型から推測できる場合、明示的な関数テンプレートの特殊化を指定するtemplate-idで未指定のままにすることができます。

与えられた例には、クラス テンプレートからの推定が含まれていますが、直接使用される型からの推定にも同様に適用できます。

template<class T> class Array { /∗ ... ∗/ };
template<class T> void sort(Array<T>& v);
// explicit specialization for sort(Array<int>&)
// with deduced template-argument of type int
template<> void sort(Array<int>&);
于 2012-11-23T20:17:34.873 に答える