Is _ (single underscore) a valid C++ variable name?

前端 未结 6 1796
挽巷
挽巷 2021-01-07 19:06

With gcc 4.7.2 this compiles just fine for me:

int main()
{
  int _ = 1;
  return 0;
}

Can I expect this to compile in general? I\'ve read

6条回答
  •  死守一世寂寞
    2021-01-07 19:55

    Yeah it's a valid prefix. Running

    #include 
    
    using namespace std;
    
    int main()
    {
    char* _ = "Hello World";
    cout << _ << endl; 
    return 0;
    }
    

    prints out "Hello World" as expected. It's not a very helpful varible name but it is valid

提交回复
热议问题