When to use “::” for global scope in C++?

后端 未结 4 494
余生分开走
余生分开走 2020-12-18 20:11

Every once in a while, I stumble across some code that I\'m maintaining that challenges the way I think about my code style. Today was one of those days...

I\'m awa

4条回答
  •  萌比男神i
    2020-12-18 20:44

    Readable code is that has the least amount of noise. namespace prefixes normally provide nothing but noise. So baseline is to not have them at all.

    Namespaces were introduced to C++ mainly to handle 3rd party stuff out of one's control. To allow libraries drop prefixing, while clients can use terse names by applying using.

    Just because you can have the same name in many namespaces does not imply it is a good idea to too. If a project uses some environment, platform API, library set, whatever that puts name in global, those names are better be avoided for other purposes. As with or without prefixes they will bear mental overhead.

    Use of :: is rare in well-shaped code, and frequent uses appear in wrapper classes for that same functionality.

提交回复
热议问题