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

前端 未结 8 1855
悲&欢浪女
悲&欢浪女 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 04:03

    This is so much artificial:

    namespace fred {
    namespace std {  // A standard fred component
    
    class string { // Something rather unlike the ::std::string
       // ...
    };
    
    } // namespace std
    } // namespace fred
    

    This Fred would not be able to explain why he did that to the whole team.

    To answer your question: people omit first "::" because it is a habit, which saves time and because it is easier to expect that when namespace is used anywhere, then such name is absolute, not relative.

提交回复
热议问题