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

89

6

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?

user1410910

Posted 2012-10-09T19:32:27.717

Reputation: 570

1@Xeo: He said in the question he has using namespace std;. – Fred Larson – 2012-10-09T19:36:25.867

@Fred: Derp! That part wasn't marked as code, so I just overlooked it. :) I'd still like to point to this question, however.

– Xeo – 2012-10-09T19:36:57.593

23Same reason that http://www.stackoverflow.com compiles... – Kerrek SB – 2012-10-09T19:40:49.290

19Yet another reason not to use using namespace std; :) – David Rodríguez - dribeas – 2012-10-09T19:41:47.153

@KerrekSB That happened to a friend of mine while browsing reddit! The link turned out to be this :D

– GradGuy – 2012-10-09T19:44:42.583

Answers

90

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.

K-ballo

Posted 2012-10-09T19:32:27.717

Reputation: 67 853

Is this a legal goto label? – John Dibling – 2012-10-09T19:36:22.557

@JohnDibling: Sure, why not? – Lily Ballard – 2012-10-09T19:36:41.557

Reserved by the Standard? – John Dibling – 2012-10-09T19:36:53.987

4AFAIK labels has their own name space, so they never conflict with anything else, other than keywords or macros, of course. – rodrigo – 2012-10-09T19:38:49.010

11C++03 §6.1/1 says "Labels have their own name space and do not interfere with other identifiers." – Adam Rosenfield – 2012-10-09T19:40:07.177

6.1/1: "Labels have their own name space and do not interfere with other identifiers" – John Dibling – 2012-10-09T19:43:29.400

OK, I'm a little slower. :) – John Dibling – 2012-10-09T19:44:04.367

32

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

Fred Larson

Posted 2012-10-09T19:32:27.717

Reputation: 46 284

12

Its a label which is followed by the string

Rahul Tripathi

Posted 2012-10-09T19:32:27.717

Reputation: 125 063

8

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

Polymorphism

Posted 2012-10-09T19:32:27.717

Reputation: 234

4Grammar-wise, it's actually a (label-)statement, not an expression. – GManNickG – 2012-10-09T20:08:10.747

Oh yes you are right i mean they are separated.. – Polymorphism – 2012-10-09T20:51:43.180

1

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

Ali

Posted 2012-10-09T19:32:27.717

Reputation: 37 519