以下のコードでは、引数{1,2}を持つインスタンスbのメンバ関数Fを呼び出すと、B::F(std::initializer_list<int>)
がコンパイルされ、呼び出されます。しかし、私はブレース-INIT-リストから1つの要素を削除し、コンパイラがbegin(int)
を探している、とないbegin(initializer_list<int>)
1つの要素を持つbraced-init-listの型が要素自体の型に切り替わるのはなぜですか?
9 : error: no matching function for call to 'begin(int)' using type = decltype(std::begin(std::declval<T>()));
を取得し、私は理解していない使用している場合
私はこれをhttps://godbolt.org/g/tMyYQsでプレイしていますが、clangとg ++の両方で同じ種類のエラーが発生します。私は何が欠けていますか?
#include <type_traits>
#include <iterator>
template< bool B, class T = void >
using enable_if_t = typename std::enable_if<B,T>::type;
template <typename T>
struct mytrait {
using type = decltype(std::begin(std::declval<T>()));
};
template <typename T>
class A {
public:
template <typename TA, typename =
enable_if_t<std::is_same<T, typename mytrait<TA>::type>::value>>
A(TA &&){}
};
class B
{
public:
void F(A<int>);
void F(std::initializer_list<int>);
};
int main()
{
B b;
b.F({1,2}); // compiles fine
#if 0
b.F({1}); // causes mytrait<int>::type to be examined,
// not mytrait<std::initializer_list<int>>::type
#endif
}
"均一な初期化"のため:-) –