unnamed namespace and friend

Discussion in 'C++' started by marco_segurini, Feb 3, 2005.

  1. Hi,

    I like to know if this code is legal:

    namespace
    {
    void unnamed_ns(){}
    }

    class test
    {
    friend void unnamed_ns();
    };

    int main()
    {
    unnamed_ns();
    }

    because the c++ compiler of vs2003 return me this error

    error C2668: 'unnamed_ns' : ambiguous call to overloaded function
    could be 'void unnamed_ns(void)'
    or 'void `anonymous-namespace'::unnamed_ns(void)'

    while all is fine for Comeau Online

    Thanks a lot.
    Marco.
    marco_segurini, Feb 3, 2005
    #1
    1. Advertising

  2. marco_segurini

    msalters Guest

    marco_segurini wrote:
    > Hi,
    >
    > I like to know if this code is legal:
    >
    > namespace { void unnamed_ns(){} }
    >
    > class test {friend void unnamed_ns(); };
    >
    > int main() {
    > unnamed_ns();
    > }


    Yes. The friend declaration refers to a name that can be found
    in the enclosing scope (in this case, global) because an unnamed
    namespace implies a using directive. (7.3.1.1)

    Regards,
    Michiel Salters
    msalters, Feb 3, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Razmig K
    Replies:
    3
    Views:
    669
    John L Fjellstad
    Sep 5, 2003
  2. marco_segurini
    Replies:
    4
    Views:
    998
    Rob Williscroft
    Jun 16, 2004
  3. Ivan Mascovich

    friend and unnamed namespace

    Ivan Mascovich, Mar 2, 2006, in forum: C++
    Replies:
    9
    Views:
    508
    Victor Bazarov
    Mar 3, 2006
  4. John Ratliff

    unnamed namespace and friend method

    John Ratliff, Feb 6, 2008, in forum: C++
    Replies:
    9
    Views:
    2,435
    Alf P. Steinbach
    Feb 8, 2008
  5. Johannes Schaub (litb)

    Experiment with unnamed namespace and template

    Johannes Schaub (litb), Oct 13, 2010, in forum: C++
    Replies:
    4
    Views:
    490
    Victor Bazarov
    Oct 13, 2010
Loading...

Share This Page