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

后端 未结 4 509
余生分开走
余生分开走 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条回答
  •  忘掉有多难
    2020-12-18 20:41

    I use it quite infrequently; only when some ambiguity needs resolving for whatever reason. This is pretty subjective, though.

    There may be some occasions (say, inside a template) where you're worried about ADL causing ambiguities only in certain cases:

    template 
    void foo(T t)
    {
       ::bar(t);  // :: just in case there's a `bar` in `T`'s namespace
    }
    

提交回复
热议问题