Namespace & template puzzle

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James W. Walker

    Namespace & template puzzle

    Can someone please explain to me why the following code gives a compile
    error when instantiating the sort algorithm?

    #include <vector>

    namespace
    {
    struct Foo {
    int m;
    };
    }

    static bool operator<( const Foo& inOne, const Foo& inTwo )
    {
    return inOne.m < inTwo.m;
    }

    static void test()
    {
    std::vector<Foo > vec;
    std::sort( vec.begin(), vec.end() );
    }

    It's as if it doesn't understand that I have provided operator< for
    Foo. But if I just say

    Foo x,y; if (x < y) {... }

    instead of sorting, there is no problem. If I move the operator< into
    the namespace, then it compiles. So, I know how to fix it, but I'd
    like to understand it.
  • Bob Hairgrove

    #2
    Re: Namespace &amp; template puzzle

    On Sat, 25 Oct 2003 18:19:49 GMT, "James W. Walker"
    <osxNOSPAM@jwwa lker.com.invali d> wrote:
    [color=blue]
    >Can someone please explain to me why the following code gives a compile
    >error when instantiating the sort algorithm?
    >
    >#include <vector>
    >
    >namespace
    >{
    > struct Foo {
    > int m;
    > };
    >}
    >
    >static bool operator<( const Foo& inOne, const Foo& inTwo )
    >{
    > return inOne.m < inTwo.m;
    >}
    >
    >static void test()
    >{
    > std::vector<Foo > vec;
    > std::sort( vec.begin(), vec.end() );
    >}
    >
    >It's as if it doesn't understand that I have provided operator< for
    >Foo. But if I just say
    >
    >Foo x,y; if (x < y) {... }
    >
    >instead of sorting, there is no problem. If I move the operator< into
    >the namespace, then it compiles. So, I know how to fix it, but I'd
    >like to understand it.[/color]

    By putting Foo() into an anonymous namespace, it is hidden from
    everything outside that namespace.


    --
    Bob Hairgrove
    NoSpamPlease@Ho me.com

    Comment

    • James W. Walker

      #3
      Re: Namespace &amp; template puzzle

      In article <3f9ad689.11546 683@news.webshu ttle.ch>, Bob Hairgrove
      <wouldnt_you_li ke@to_know.com> wrote:
      [color=blue]
      > By putting Foo() into an anonymous namespace, it is hidden from
      > everything outside that namespace.[/color]

      I don't see how that is consistent with what happened. My definition
      of operator< for Foo was outside the namespace, and I didn't get a
      compile error on that.

      Comment

      • Oplec

        #4
        Re: Namespace &amp; template puzzle

        James W. Walker wrote:
        [color=blue]
        > I don't see how that is consistent with what happened. My definition
        > of operator< for Foo was outside the namespace, and I didn't get a
        > compile error on that.[/color]

        I'm new to C++, but from what I have read about anonymous namespaces,
        whatever is included in an anonymous namespace is only accessable from
        within the single translation unit that it is defined in. Maybe that has
        something to do with it.

        Oplec.

        Comment

        • Stephen M. Webb

          #5
          Re: Namespace &amp; template puzzle

          "James W. Walker" <osxNOSPAM@jwwa lker.com.invali d> wrote in message news:<251020031 119477341%osxNO SPAM@jwwalker.c om.invalid>...[color=blue]
          > Can someone please explain to me why the following code gives a compile
          > error when instantiating the sort algorithm?[/color]

          I believe this is an effect of Koenig lookup rules.

          --
          Stephen M. Webb

          Comment

          Working...