Scope resolution operator and const

前端 未结 4 1183
抹茶落季
抹茶落季 2021-01-24 03:27

Let\'s take the following code:

#include  // std::string    
using namespace std;
int main() {
  //const::int i = 42;       -> Error: \"expected         


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-24 04:19

    It compiles because

    using namespace std;
    

    pulls the entire std namespace into the global namespace, so ::string is the same as std::string. Your line in question is actually interpreted as

    const ::string str = "Foo";
    

    However, int is a keyword (and a fundamental type), and not a user-defined name that resides in any namespace. So ::int doesn't make sense.

提交回复
热议问题