Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

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

I accidentally wrote

std::set<string> keys;

as:

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

if(keys.find(keystr)==keys.end()){
    keys.insert(keystr);
    toret.push_back(tempv);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...