c++ - Why do auto and template type deduction differ for braced initializers? -
i understand that, given braced initializer, auto
deduce type of std::initializer_list
, while template type deduction fail:
auto var = { 1, 2, 3 }; // type deduced std::initializer_list<int> template<class t> void f(t parameter); f({ 1, 2, 3 }); // doesn't compile; type deduction fails
i know specified in c++11 standard: 14.8.2.5/5 bullet 5:
[it's non-deduced context if program has] function parameter associated argument initializer list (8.5.4) parameter not have std::initializer_list or reference possibly cv-qualified std::initializer_list type. [ example:
template void g(t);
g({1,2,3}); // error: no argument deduced t
—end example ]
what don't know or understand why difference in type deduction behavior exists. specification in c++14 cd same in c++11, presumably standardization committee doesn't view c++11 behavior defect.
does know why auto
deduces type braced initializer, templates not permitted to? while speculative explanations of form "this reason" interesting, i'm interested in explanations people know why standard written way was.
there 2 important reasons templates not deduction (the 2 remember in discussion guy in charge)
concerns future language extensions (there multiple meanings invent - if wanted introduce perfect forwarding braced init list function arguments?)
the braces can validly initialize function parameter dependent
template<typename t> void assign(t &d, const t& s);
int main() { vector<int> v; assign(v, { 1, 2, 3 }); }
if t
deduced @ right side initializer_list<int>
@ left side vector<int>
, fail work because of contradictional argument deduction.
the deduction auto
initializer_list<t>
controversial. there exist proposal c++-after-14 remove (and ban initialization { }
or {a, b}
, , make {a}
deduce type of a
).
Comments
Post a Comment