0
auto type deduction
On pages 49-50 of Scott Meyers' Effective C++, he says that both auto x = {27}; auto y{27}; should deduce type to be std initializer list, but when I run the code below, the second one seems to be integer instead. Am I missing something? https://code.sololearn.com/ceTJ8qEd9yC3
5 odpowiedzi
+ 4
Edward Finkelstein
I'm here as I was exploring your profile and found the question interesting.
The behaviour of
auto y{27};
was changed in N3922
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3922.html
From what I understand,
auto var = ...init....;
Deduces type from the initializer.
So when you write,
auto x = {27};
The type will be deduced from '{27}', i.e. std::initializer_list<int>
But
auto var{val};
Where the initializer list has only ONE value 'val', the type is deduced from the value
So in case of
auto y{27};
The type will be deduced from '27', i.e. int
See this for more info
https://en.cppreference.com/w/cpp/language/template_argument_deduction#Other_contexts
Related:
https://en.cppreference.com/w/cpp/language/auto
+ 2
@XXX Yeah, I ended up seeing the same thing after asking in another forum, and this too https://timsong-cpp.github.io/cppwp/n4659/diff.cpp14.dcl.dcl
Thanks for the post, I appreciate it.
+ 1
Sorry, I'm not that much into digging and memorizing the standard, it looks like law handbook to me LOL.
But someone will probably tell you about that.
I personally think your understanding was correct. The assignment operator `=` there kinda tell me that it was an assignment rather than a construction.
Let's hear out what our friends got to say ...
0
It *could be* a misunderstanding, but I can't really tell since I don't have a copy of the book at hand.
0
Ipang but is there something in the C++ standard that says something about it.