0
次はなぜコンパイルに失敗しますか? コンパイラは入力からの戻り値の型を推定できませんか?自動入力引数からの戻り値の型を推定する
表示されている型変換エラーに完全に従わないでください。
auto func(auto &x)
{
return x[0];
}
int main()
{
vector<int> v = { 1, 2, 3};
(void)func(v);
}
$ g++ -std=c++1z auto.cc
auto.cc: In instantiation of ‘auto func(auto:1&) [with auto:1 = std::vector<int>]’:
auto.cc:15:16: required from here
auto.cc:8:14: error: could not convert ‘(& x)->std::vector<_Tp, _Alloc>::operator[]<int, std::allocator<int> >(0ul)’ from ‘__gnu_cxx::__alloc_traits<std::allocator<int> >::value_type {aka int}’ to ‘std::vector<int>’
return x[0];
今後の[C + 1z](https://en.wikipedia.org/wiki/C%2B%2B17)a.k.a. C++ 17ドラフト標準では提案されていませんか?私はGCCがその提案を超えて多くの機能を追加したとは思わない。 –
ラムダパラメータの型を宣言するために 'auto'を使うことは、C++ 14でサポートされていることに注意してください。 – EricWF
@BasileStarynkevitch:私はそれが(まだ)標準C++ではないコンセプトTSの一部だと思います。 –