lundi 1 février 2021

auto type deduction with uniform initialisation syntax c++11 vs c++17

I was trying to test auto type deduction. Both Scott Meyers (Effective modern C++) and Bjarne Stroustrup's C++ Programming Language mention that doing

auto val {10};

will deduce val to be of type "initialisation list".

I read that this was changed in C++17 so that if there is only one element in the list, then auto will deduce to a type of that element instead.

However, I tested this with recent gcc (v10) and clang (V11) compilers by explicitly specifying the C++11 standard and I didn't see the behaviour expected

auto A {1.0};
std::cout << typeid(A).name();

prints "d" to screen

whereas

auto A={1.0};
std::cout << typeid(A).name();

prints "St16initializer_listIdE" to screen.

This is the same regardless of whether I specity

gcc -std=c++11

or

gcc -std=c++17

and similarly for clang.

I understand that it was changed in C++17, but why then do I not see the "old" behaviour? Or am I misunderstanding?

Thanks

Aucun commentaire:

Enregistrer un commentaire