Using auto with initializer list

邮差的信 提交于 2019-12-11 13:54:26

问题


I have question regarding interaction between auto and initializer list. Example code:

#include <iostream>

int main()
{
auto a{ 1 };
auto b = { 1 };
auto c = 1;

std::cout << typeid(a).name() << std::endl;
std::cout << typeid(b).name() << std::endl;
std::cout << typeid(c).name() << std::endl;

return 0;
}

Gives output:

int
class std::initializer_list<int>
int

Which is kind of confusing. I'm posting this question as a followup to this. What should happen? I've did some research and it seems that auto c = 1; is illegal now, and it seems that it works because compilers allow this as a backwards compatibility patch. Does this apply also to auto a{1}?

来源:https://stackoverflow.com/questions/38104782/using-auto-with-initializer-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!