K.F*_*K.F 7 c++ cout compiler-warnings
我在这里有一个C++学习演示:
char c = 'M';
short s = 10;
long l = 1002;
char * cptr = &c;
short * sptr = &s;
long * lptr = &l;
cout << "cptr:\t" << static_cast<void*>(cptr) << '\n';
cout << "cptr++:\t" << static_cast<void*>(++cptr) << '\n';
cout << "sptr:\t" << sptr << '\n';
cout << "sptr++:\t" << ++sptr << '\n';
cout << "lptr:\t" << lptr << '\n';
cout << "lptr++:\t" << ++lptr << '\n';
cout << c << '\t' << static_cast<void*>(cptr) << '\t' << static_cast<void*>(++cptr) << '\n';
cout << s << '\t' << sptr << '\t' << ++sptr << '\n';
cout<< l << '\t' << lptr << '\t'<< ++lptr << '\n';
Run Code Online (Sandbox Code Playgroud)
编译器警告:
任何人都可以向我解释这个吗?怎么解决?
在C++ 17之前,对<<链的操作数的评估是无序的,因此代码导致了未定义的行为.
编译器警告表明您没有在C++ 17模式下进行编译.要解决它,您可以:
<<链分成多个cout <<语句,其中没有x和++x在同一语句中.注意:到目前为止,所有版本的g ++似乎都有漏洞并且没有正确实现这些排序要求,请参阅此主题以获取更多示例.警告可以看作是指示编译器错误; 它们不仅仅是虚假的警告.