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

C++StdColon

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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

Solution 3 - C++

Its a label which is followed by the string

Solution 4 - C++

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

Solution 5 - C++

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).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1410910View Question on Stackoverflow
Solution 1 - C++K-balloView Answer on Stackoverflow
Solution 2 - C++Fred LarsonView Answer on Stackoverflow
Solution 3 - C++Rahul TripathiView Answer on Stackoverflow
Solution 4 - C++PolymorphismView Answer on Stackoverflow
Solution 5 - C++AliView Answer on Stackoverflow