comparison of two auto variables

自古美人都是妖i 提交于 2020-01-06 23:46:50

问题


#include <iostream>

using namespace std;

int main()
{
    auto a{1};
    auto b{1};
    if (a==b)
    {
        cout << "equal";
    }
    return 0;
}

Why does the above C++ code return an error in g++ compiler with c++11 standard, instead of printing "equal" as output?

test.cpp:9:14: error: no match for ‘operator==’ (operand types are ‘std::initializer_list’ and ‘std::initializer_list’) if (a==b) ^


回答1:


What do you think:

auto a{1};

is to compiler?

If you think it's supposed to be integer you are wrong. Compilers are lazy in determining what things are, and it's initializer_list (just as your error stated) And std::initializer_list doesn't have == operator defined (just as your compiler stated)



来源:https://stackoverflow.com/questions/38099768/comparison-of-two-auto-variables

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