friends namespaces and operators

Discussion in 'C++' started by glen stark, Oct 12, 2004.

  1. glen stark

    glen stark Guest

    Hi.

    I had an interesting experience with a brand x compiler recently. I had
    class defined within a namespace, and in that class I declared the "<<"
    operator as a friend to that class. Then, in the cpp code I implemented
    the operator overloading within a "using namespace" context, only to get
    access errers. To get it to work I had to put a namespaceName:: in
    front of the operator keyword...

    for your edification, here is a sample code:
    ================
    HEADER FILE

    #include <string>
    #include <ostream>

    namespace voxel{
    class PrivateClass{
    public:
    PrivateClass():info("hiya"){};
    friend std::eek:stream& operator<<(std::eek:stream&, const PrivateClass&);
    private:
    std::string info;
    };
    }
    ===================
    CPP FILE

    #include "stdafx.h"
    #include "PrivateClass.h"
    using namespace voxel;
    std::eek:stream& voxel::eek:perator<<(std::eek:stream& os, const PrivateClass& pc)
    {
    os<<pc.info;
    return os;
    }

    int main(int argc, char* argv[])
    {

    PrivateClass pc;
    std::cout<<pc;
    return 0;

    }


    =====end of code

    So my question is: is this a compiler bug, or is this a loophole in the
    standard somehow? If it's a compiler bug, does the standard imply that
    it should behave as i would think, or what? I would very much like any
    clarification you can offer me.
     
    glen stark, Oct 12, 2004
    #1
    1. Advertising

  2. "glen stark" <> wrote in message
    news:416beaa3$...
    > Hi.
    >
    > I had an interesting experience with a brand x compiler recently. I had
    > class defined within a namespace, and in that class I declared the "<<"
    > operator as a friend to that class. Then, in the cpp code I implemented
    > the operator overloading within a "using namespace" context, only to get
    > access errers. To get it to work I had to put a namespaceName:: in
    > front of the operator keyword...
    >
    > for your edification, here is a sample code:
    > ================
    > HEADER FILE
    >
    > #include <string>
    > #include <ostream>
    >
    > namespace voxel{
    > class PrivateClass{
    > public:
    > PrivateClass():info("hiya"){};
    > friend std::eek:stream& operator<<(std::eek:stream&, const PrivateClass&);
    > private:
    > std::string info;
    > };
    > }
    > ===================
    > CPP FILE
    >
    > #include "stdafx.h"
    > #include "PrivateClass.h"
    > using namespace voxel;
    > std::eek:stream& voxel::eek:perator<<(std::eek:stream& os, const PrivateClass& pc)
    > {
    > os<<pc.info;
    > return os;
    > }
    >
    > int main(int argc, char* argv[])
    > {
    >
    > PrivateClass pc;
    > std::cout<<pc;
    > return 0;
    >
    > }
    >
    >
    > =====end of code
    >
    > So my question is: is this a compiler bug, or is this a loophole in the
    > standard somehow?


    Neither, its your understanding of C++ that is wrong.

    > If it's a compiler bug, does the standard imply that
    > it should behave as i would think, or what? I would very much like any
    > clarification you can offer me.


    You seem to be assuming that writing 'using namespace voxel;' should put
    subsequent definitions in the voxel namespace but that is not the case (if
    it were the case then you would have put main in the voxel namespace). Or
    maybe you are thinking that the compiler should somehow make a connection
    between the operator<< in your source file and the operator<< in your header
    file, but as far as the compiler is concerned they are just similar
    functions in different namespaces.

    'Using namespace ...' affects how names are looked up, it does not affect
    which namespace names are defined in. The only way to do that is this

    namespace voxel
    {
    std::eek:stream& operator<<(std::eek:stream& os, const PrivateClass& pc)
    {
    ...
    }
    }

    or this

    std::eek:stream& voxel::eek:perator<<(std::eek:stream& os, const PrivateClass&
    pc)
    {
    ...
    }

    john
     
    John Harrison, Oct 12, 2004
    #2
    1. Advertising

  3. glen stark

    glen stark Guest

    John Harrison wrote:

    > "glen stark" <> wrote in message
    > news:416beaa3$...
    >
    >>Hi.
    >>
    >>I had an interesting experience with a brand x compiler recently. I had
    >>class defined within a namespace, and in that class I declared the "<<"
    >>operator as a friend to that class. Then, in the cpp code I implemented
    >>the operator overloading within a "using namespace" context, only to get
    >>access errers. To get it to work I had to put a namespaceName:: in
    >>front of the operator keyword...
    >>
    >>for your edification, here is a sample code:
    >>================
    >>HEADER FILE
    >>
    >>#include <string>
    >>#include <ostream>
    >>
    >>namespace voxel{
    >>class PrivateClass{
    >>public:
    >> PrivateClass():info("hiya"){};
    >> friend std::eek:stream& operator<<(std::eek:stream&, const PrivateClass&);
    >>private:
    >> std::string info;
    >>};
    >>}
    >>===================
    >>CPP FILE
    >>
    >>#include "stdafx.h"
    >>#include "PrivateClass.h"
    >>using namespace voxel;
    >>std::eek:stream& voxel::eek:perator<<(std::eek:stream& os, const PrivateClass& pc)
    >>{
    >> os<<pc.info;
    >> return os;
    >>}
    >>
    >>int main(int argc, char* argv[])
    >>{
    >>
    >> PrivateClass pc;
    >> std::cout<<pc;
    >> return 0;
    >>
    >>}
    >>
    >>
    >>=====end of code
    >>
    >>So my question is: is this a compiler bug, or is this a loophole in the
    >>standard somehow?

    >
    >
    > Neither, its your understanding of C++ that is wrong.
    >
    >
    >>If it's a compiler bug, does the standard imply that
    >>it should behave as i would think, or what? I would very much like any
    >>clarification you can offer me.

    >
    >
    > You seem to be assuming that writing 'using namespace voxel;' should put
    > subsequent definitions in the voxel namespace but that is not the case (if
    > it were the case then you would have put main in the voxel namespace). Or
    > maybe you are thinking that the compiler should somehow make a connection
    > between the operator<< in your source file and the operator<< in your header
    > file, but as far as the compiler is concerned they are just similar
    > functions in different namespaces.
    >
    > 'Using namespace ...' affects how names are looked up, it does not affect
    > which namespace names are defined in. The only way to do that is this
    >
    > namespace voxel
    > {
    > std::eek:stream& operator<<(std::eek:stream& os, const PrivateClass& pc)
    > {
    > ...
    > }
    > }
    >
    > or this
    >
    > std::eek:stream& voxel::eek:perator<<(std::eek:stream& os, const PrivateClass&
    > pc)
    > {
    > ...
    > }
    >
    > john
    >
    >

    Thanks, that clears it up.

    Glen
     
    glen stark, Oct 12, 2004
    #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. Simon

    Friends and Namespaces

    Simon, Oct 6, 2003, in forum: C++
    Replies:
    3
    Views:
    320
    Jonathan Mcdougall
    Oct 6, 2003
  2. Replies:
    4
    Views:
    409
    Noah Roberts
    Feb 14, 2005
  3. glen stark

    friends, namespaces and operators.

    glen stark, Oct 12, 2004, in forum: C Programming
    Replies:
    3
    Views:
    352
    Martin Ambuhl
    Oct 12, 2004
  4. Replies:
    2
    Views:
    616
  5. Replies:
    0
    Views:
    681
Loading...

Share This Page