Why can std:set (with a single colon) compile?

C++Visual Studio

C++ Problem Overview

I accidentally wrote

std::set<string> keys;


std:set<string> keys;

but weirdly enough, Visual Studio 2013 still compiles.

Why does this happen?

Actually keys is not only defined, but later used as a set of strings, such as


C++ Solutions

Solution 1 - C++

At block scope, an identifier followed by a single colon introduces a label. Thus, your statement is equivalent to:

set<string> keys;

except that it bears the label std and can be jumped to by the statement goto std;.

For some reason, the name set is known to the compiler---perhaps you did using namespace std;, or using std::set;, or something like that, or perhaps you defined your own set type somewhere.

Solution 2 - C++

In the second case, std is a label. It is the same as spelling default incorrectly in a case statement.


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
QuestionathosView Question on Stackoverflow
Solution 1 - C++Brian BiView Answer on Stackoverflow
Solution 2 - C++cupView Answer on Stackoverflow