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

后端 未结 5 1628
渐次进展
渐次进展 2020-12-14 13:53

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

相关标签:
5条回答
  • 2020-12-14 14:08

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

    0 讨论(0)
  • 2020-12-14 14:22

    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.

    0 讨论(0)
  • 2020-12-14 14:22

    Its a label which is followed by the string

    0 讨论(0)
  • 2020-12-14 14:27

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

    0 讨论(0)
  • 2020-12-14 14:33
    (expression)std: (end of expression); (another expression)string x = y;
    
    0 讨论(0)
提交回复
热议问题