私は、テンプレート型推論が失敗しながらブレース初期化子与え、auto
は、std::initializer_list
の種類を推測します、ということを理解:括弧で囲まれたイニシャライザで、自動とテンプレートの種類の差異が異なるのはなぜですか?
auto var = { 1, 2, 3 }; // type deduced as std::initializer_list<int>
template<class T> void f(T parameter);
f({ 1, 2, 3 }); // doesn't compile; type deduction fails
これはC++ 11標準で指定された場合、私も知っている:14.8。 2.5/5箇条書き5:は、型推論の挙動の違いが存在する理由を私は知っているか理解していない何
[It's a non-deduced context if the program has] A function parameter for which the associated argument is an initializer list (8.5.4) but the parameter does not have std::initializer_list or reference to possibly cv-qualified std::initializer_list type. [ Example:
template void g(T);
g({1,2,3}); // error: no argument deduced for T
—end example ]
がです。 C++ 14 CDの仕様はC++ 11と同じですので、おそらく標準化委員会はC++ 11の動作を欠陥として見なさないでしょう。
なぜauto
は、括弧で囲まれた初期化子の型を推測しますが、テンプレートは許可されていませんか? "これは理由があるかもしれない"という推測的な説明は面白いですが、私は特にがを知っている人からの説明に興味があります。
あなたが考え出したことについて最近質問された別のSOの質問がありました。なぜそれがあるのかもしれない。 – chris
それを見つけました。なぜ討論が1つのコメントであるのかという唯一の真実ですが、あなたは見てみることができます:http://stackoverflow.com/questions/17496268/type-inference-with-rvalue-initializer-list – chris
[Scott Meyers](http: /scottmeyers.blogspot.cz/2013/07/when-decltype-meets-auto.html): "なぜ、' auto'とテンプレートの型減算が同じでないのか分かりません。 * "。 –