I know for C++11 way of initializing a vector using auto, actually an std::initializer_list is initialized instead of a vector. However, given below piece of code:
#include <iostream>
#include <vector>
using namespace std;
int main() {
auto x = {1, 2};
cout << typeid(x).name() << endl;
auto z = (1, 2);
cout << z << ", type: " << typeid(z).name() << endl;
return 0;
}
I don't understand:
- Why the type of
xreturned isSt16initializer_listIiEand the type of 'z' returned is 'i', using gcc-10 compiler. Shouldn't we just returnstd::initializer_listand 'int'? - There is a warning on
z:warning: left operand of comma operator has no effect [-Wunused-value]. Then the 2nd half of result is:2, type: i. How does c++11 interpret()-initialized type? Why is only the last element passed intozand thuszis still of typeint?
Aucun commentaire:
Enregistrer un commentaire