Why does everybody use unanchored namespace declarations (i.e. std:: not ::std::)?

前端 未结 8 1848
悲&欢浪女
悲&欢浪女 2020-11-30 03:01

It seems to me that using unanchored namespaces is just asking for trouble later when someone puts in a new namespace that happens to have the same name as a root level name

8条回答
  •  野性不改
    2020-11-30 03:51

    why do people always say std::

    Not always. I use string and using ::std::string. So nothing bad if there will be fred::std::string, because I still use std::string. In small cpp-files it could be even using namespace ::std. Sample:

    #include 
    using ::std::string;
    //using namespace ::std; // good for small cpp-files
    
    int main()
    {
      string s = "sometext"; // no leading ::std
    }
    

    you should just never name a namespace std

    Yes, you shouldn't give a name std to custom namespace.

提交回复
热议问题