Thread: namespace :: syntax

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    namespace :: syntax

    what does it mean when there is 2 colons before an identifier but no namespace there..

    such as

    Code:
    ::std::vector < ::std::stack<int> >;
    and in this case the colons are before a namespace itself! , I am confused

    does this code have the same meaning as

    Code:
    std::vector < std::stack<int> >;
    ?

    google isn't helping me with this one (as its hard to describe in a search)

    THANKS!
    Last edited by rodrigorules; 05-08-2010 at 06:11 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The std namespace which is within global namespace?

    Probably to disambiguate situations like:
    Code:
    #include <vector>
    #include <stack>
    
    namespace x
    {
        namespace std {
            template <class > class vector {};
            template <class > class stack {};
        }
    }
    
    using namespace x;
    
    ::std::vector < ::std::stack<int> > p; //try compiling without
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    But without the colons wouldn't it still see std as global since the std we defined is WITHIN another namespace?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, but the using declaration imports namespace x::std also to the global namespace, so just std::vector might refer both to ::std::vector (the standard one which is not nested in another namespace) and x::std::vector.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    thanks

    Thank you, anon. You sure know how to recognize different namespaces from quite a long way away.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I believe the use of the scope resolution operator to indicate global namespace has recently been discouraged by Microsoft for their most recent compilers. I'm not exactly sure of all the reasoning behind it. I still use it out of pure habit.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by anon View Post
    Yes, but the using declaration imports namespace x::std also to the global namespace
    While it is often be described like that, the meaning specified in the standard is a little different to "importing to the global namespace". A using namespace directive means that names within that namespace become candidates for matching names in code.

    For example, a "using namespace std;" directive subsequently causes the compiler to treat std::cout as a candidate to match any subsequent usage of the name cout ..... if <iostream> has been #include'd, of course. If another using namespace directive is also active, and that namespace also contains something named cout, it is also a candidate for matching. Hence the compiler reports ambiguity.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Zipping files
    By CompiledMonkey in forum C Programming
    Replies: 19
    Last Post: 03-06-2003, 12:23 PM