What would 'std:;' do in c++?

断了今生、忘了曾经 提交于 2019-12-13 11:33:38

问题


I was recently modifying some code, and found a pre-existing bug on one line within a function:

std:;string x = y;

This code still compiles and has been working as expected.

The string definition works because this file is using namespace std;, so the std:: was unnecessary in the first place.

The question is, why is std:; compiling and what, if anything, is it doing?


回答1:


std: its a label, usable as a target for goto.

As pointed by @Adam Rosenfield in a comment, it is a legal label name.

C++03 §6.1/1:

Labels have their own name space and do not interfere with other identifiers.




回答2:


It's a label, followed by an empty statement, followed by the declaration of a string x.




回答3:


Its a label which is followed by the string




回答4:


(expression)std: (end of expression); (another expression)string x = y;



回答5:


The compiler tells you what is going on:

#include <iostream>
using namespace std;
int main() {
  std:;cout << "Hello!" << std::endl;
}

Both gcc and clang give a pretty clear warning:

std.cpp:4:3: warning: unused label 'std' [-Wunused-label]
  std:;cout << "Hello!" << std::endl;
  ^~~~
1 warning generated.

The take away from this story: always compile your code with warnings enabled (e.g. -Wall).



来源:https://stackoverflow.com/questions/12807115/what-would-std-do-in-c

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