namespace and operator==

Discussion in 'C++' started by Xavier Decoret, Jul 24, 2003.

  1. The following code does not compoile with gcc-3.2.3

    namespace dummy
    {
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Interface of Foo
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    class Foo
    {
    public:

    friend bool operator==(const Foo& f0,const Foo& f1);
    private:
    int a_;
    };
    }
    //************************************************************
    // Implementation of Doo
    //************************************************************
    bool
    operator==(const dummy::Foo& f0,const dummy::Foo& f1)
    {
    return f0.a_ == f1.a_;
    }


    After a while, I figured that operator== is actually declared in
    namespace dummy:: so the compiler error is legitimate. Therefore I can
    fix the implementation with:

    bool
    dummy::eek:perator==(const dummy::Foo& f0,const dummy::Foo& f1)
    {
    std::cout<<"in dummy::eek:perator=="<<std::endl;
    return f0.a_ == f1.a_;
    }

    But then I am quite surprise that the following does compile:
    int
    main(int,char**)
    {
    dummy::Foo f0;
    dummy::Foo f1;
    return f0 == f1;
    }

    Because it should not find the operator== since namespace dummy is not
    opened. But I am sure that it is the one called (thanks to the cout).

    Then I tried to make operator== be in global namespace with
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Interface of Foo
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    friend bool ::eek:perator==(const Foo& f0,const Foo& f1);

    //************************************************************
    // Implementation of Doo
    //************************************************************
    bool
    operator==(const dummy::Foo& f0,const dummy::Foo& f1)
    {
    std::cout<<"in ::eek:perator=="<<std::endl;
    return f0.a_ == f1.a_;
    }

    But then it refuses to compile with:
    main.C:11: `bool operator==(const dummy::Foo&, const dummy::Foo&)'
    should have
    been declared inside `::'

    So my question is: is my concern legal? Do I really want to have
    operator== in namespace dummy? Is it legal that gcc finds that there is
    an dummy::eek:perator== even if the namespace is not opened?

    Thanks for insights.


    --
    +-------------------------------------------------+
    | Xavier Décoret - Post Doct |
    | Graphics Lab (LCS) - MIT |
    | mailto: |
    | home : http://www.graphics.lcs.mit.edu/~decoret|
    +-------------------------------------------------+
    Xavier Decoret, Jul 24, 2003
    #1
    1. Advertising

  2. "Xavier Decoret" <> wrote...
    > [...]
    > So my question is: is my concern legal?


    It's legal. Whether it's valid or not is a different question.
    In order to answer it, one needs to know what your concern is.

    > Do I really want to have
    > operator== in namespace dummy?


    I don't know. Do you?

    > Is it legal that gcc finds that there is
    > an dummy::eek:perator== even if the namespace is not opened?


    Yes, it is. See 3.4.2/1. The call to operator== is a function
    call. Namespaces of the operands are considered in the name
    lookup along with all visible scopes.

    Victor
    Victor Bazarov, Jul 24, 2003
    #2
    1. Advertising

  3. Xavier Decoret wrote:
    > The following code does not compoile with gcc-3.2.3
    >
    > namespace dummy
    > {
    > //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    > // Interface of Foo
    > //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    > class Foo
    > {
    > public:
    >
    > friend bool operator==(const Foo& f0,const Foo& f1);
    > private:
    > int a_;
    > };
    > }
    > //************************************************************
    > // Implementation of Doo
    > //************************************************************
    > bool
    > operator==(const dummy::Foo& f0,const dummy::Foo& f1)
    > {
    > return f0.a_ == f1.a_;
    > }
    >
    >
    > After a while, I figured that operator== is actually declared in
    > namespace dummy:: so the compiler error is legitimate. Therefore I can
    > fix the implementation with:


    It is not really declared yet. But yes, the above 'friend' declaration
    refers to the 'operator ==' from namespace 'dummy'.

    > bool
    > dummy::eek:perator==(const dummy::Foo& f0,const dummy::Foo& f1)
    > {
    > std::cout<<"in dummy::eek:perator=="<<std::endl;
    > return f0.a_ == f1.a_;
    > }
    >
    > But then I am quite surprise that the following does compile:
    > int
    > main(int,char**)
    > {
    > dummy::Foo f0;
    > dummy::Foo f1;
    > return f0 == f1;
    > }
    >
    > Because it should not find the operator== since namespace dummy is not
    > opened. But I am sure that it is the one called (thanks to the cout).


    The correct 'operator ==' is found by argument-dependent name lookup.
    There's no need to "open" any namespaces here, whatever that means.

    > Then I tried to make operator== be in global namespace with
    > //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    > // Interface of Foo
    > //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    > friend bool ::eek:perator==(const Foo& f0,const Foo& f1);
    >
    > //************************************************************
    > // Implementation of Doo
    > //************************************************************
    > bool
    > operator==(const dummy::Foo& f0,const dummy::Foo& f1)
    > {
    > std::cout<<"in ::eek:perator=="<<std::endl;
    > return f0.a_ == f1.a_;
    > }
    >
    > But then it refuses to compile with:
    > main.C:11: `bool operator==(const dummy::Foo&, const dummy::Foo&)'
    > should have
    > been declared inside `::'


    If I'm not mistaken, you cannot refer to an unknown name in a friend
    declaration, unless this name is supposed to belong to immediate
    enclosing namespace scope. In other words, you friend declaration can
    introduce any names that will (or do already) belong to namespace
    'dummy', but if you want to "befriend" anything from global scope, that
    thing must be already declared in advance. For example, this will compile

    namespace dummy { class Foo; };
    bool operator==(const dummy::Foo& f0,const dummy::Foo& f1);

    namespace dummy
    {
    class Foo
    {
    public:
    friend bool ::eek:perator==(const Foo& f0,const Foo& f1);
    private:
    int a_;
    };
    }

    bool operator==(const dummy::Foo& f0,const dummy::Foo& f1)
    {
    return f0.a_ == f1.a_;
    }

    int main()
    {
    dummy::Foo f0;
    dummy::Foo f1;
    return f0 == f1;
    }

    >
    > So my question is: is my concern legal?


    What concern is that?

    > Do I really want to have operator== in namespace dummy?


    Well, you should really ask yourself. It is perfectly legal to have it
    there.

    > Is it legal that gcc finds that there is
    > an dummy::eek:perator== even if the namespace is not opened?


    Yes, it is. Read about argument-dependent name lookup. Sometimes it is
    also referred to as Koenig lookup.

    --
    Best regards,
    Andrey Tarasevich
    Brainbench C and C++ Programming MVP
    Andrey Tarasevich, Jul 24, 2003
    #3
    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. Èý¹â
    Replies:
    1
    Views:
    565
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,127
  3. Anonymous
    Replies:
    3
    Views:
    529
    Ron Natalie
    Aug 18, 2003
  4. Jason Heyes
    Replies:
    1
    Views:
    448
    Woebegone
    Nov 19, 2004
  5. mrstephengross
    Replies:
    3
    Views:
    396
    James Kanze
    May 10, 2007
Loading...

Share This Page