Language Subtlety or Compiler bug?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ned Harding

    #1

    Language Subtlety or Compiler bug?

    In VC7.1 the following code outputs:
    00000000
    Success!

    It would seem that the Y::operator<< is hiding the global operator<<
    when it is outputting an object from another namespace but not when it
    is outputting an object in the global namespace.

    I can't help but think one of these 2 behaviors is a bug in VC7.1, but
    which one?

    Thanks,

    Ned.




    =============== =============== ===========
    #include <iostream>

    namespace X
    {
    class Abc1
    {
    };

    }
    std::ostream& operator<< (std::ostream& strm, const X::Abc1 *node)
    {
    return strm << "Success!";
    }

    X::Abc1 * GetDoc() { return NULL; }

    class Abc2
    {
    };

    Abc2 * GetDoc2() { return NULL; }

    std::ostream& operator<< (std::ostream& strm, const Abc2 *node)
    {
    return strm << "Success!";
    }

    namespace Y
    {
    std::ostream & operator<<(std: :ostream& a, const char& b)
    {
    return a << "This should not run";
    }

    void DoTest()
    {
    std::cout << GetDoc() << "\n";
    std::cout << GetDoc2() << "\n";
    }
    }



    int main(int argc, char* argv[])
    {
    Y::DoTest();
    return 0;
    }
  • Howard

    #2
    Re: Language Subtlety or Compiler bug?


    "Ned Harding" <nharding@exten dthereach.com> wrote in message
    news:34c6c406.0 408310925.41a44 c87@posting.goo gle.com...[color=blue]
    > In VC7.1 the following code outputs:
    > 00000000
    > Success!
    >
    > It would seem that the Y::operator<< is hiding the global operator<<
    > when it is outputting an object from another namespace but not when it
    > is outputting an object in the global namespace.
    >[/color]

    I don't see the operator in the Y namespace coming into play at all here.
    What makes you think it is? You're not getting "This should not run" as
    your output. Plus, the operator in Y uses a reference to a char, not a
    pointer - of *any* type!
    [color=blue]
    > =============== =============== ===========
    > #include <iostream>
    >
    > namespace X
    > {
    > class Abc1
    > {
    > };
    >
    > }
    > std::ostream& operator<< (std::ostream& strm, const X::Abc1 *node)
    > {
    > return strm << "Success!";
    > }
    >
    > X::Abc1 * GetDoc() { return NULL; }
    >
    > class Abc2
    > {
    > };
    >
    > Abc2 * GetDoc2() { return NULL; }
    >
    > std::ostream& operator<< (std::ostream& strm, const Abc2 *node)
    > {
    > return strm << "Success!";
    > }
    >
    > namespace Y
    > {
    > std::ostream & operator<<(std: :ostream& a, const char& b)
    > {
    > return a << "This should not run";
    > }
    >
    > void DoTest()
    > {
    > std::cout << GetDoc() << "\n";
    > std::cout << GetDoc2() << "\n";
    > }
    > }
    >
    >
    >
    > int main(int argc, char* argv[])
    > {
    > Y::DoTest();
    > return 0;
    > }[/color]

    It looks to me like your operator in the global namespace is correctly
    getting called for the case where you've passing it an Abc2*. That's normal
    overloading (correct?). In the case where you pass an Abc1*, however, your
    code apparently isn't seeing the overloaded operator that uses an Abc1*, but
    instead is calling the << normally used for a void* pointer.

    I'm no expert on why that is, but if I had to guess (which I do :-)), I'd
    say it's because there is nothing specifying that you want to use the
    operator << that's in the X namespace. I'm not even sure how you would do
    that. Perhaps using X:<< ?

    -Howard


    Comment

    • Howard

      #3
      Re: Language Subtlety or Compiler bug?


      "Howard" <alicebt@hotmai l.com> wrote in message
      news:sC3Zc.5377 88$Gx4.147573@b gtnsc04-> I'm no expert on why that is, but if
      I had to guess (which I do :-)), I'd[color=blue]
      > say it's because there is nothing specifying that you want to use the
      > operator << that's in the X namespace. I'm not even sure how you would do
      > that. Perhaps using X:<< ?
      >[/color]

      I meant to say "X::<<" there. (But as I said, it was just a guess. :-))
      [color=blue]
      > -Howard
      >
      >[/color]


      Comment

      • Victor Bazarov

        #4
        Re: Language Subtlety or Compiler bug?

        Howard wrote:[color=blue]
        > "Howard" <alicebt@hotmai l.com> wrote in message
        > news:sC3Zc.5377 88$Gx4.147573@b gtnsc04-> I'm no expert on why that is, but if
        > I had to guess (which I do :-)), I'd
        >[color=green]
        >>say it's because there is nothing specifying that you want to use the
        >>operator << that's in the X namespace. I'm not even sure how you would do
        >>that. Perhaps using X:<< ?
        >>[/color]
        >
        >
        > I meant to say "X::<<" there. (But as I said, it was just a guess. :-))[/color]

        I think that :: is allowed only with the name. << is not a name, so it
        probably should be X::operator<<. But I am just adding a guess on top
        of your guess...

        Victor

        Comment

        • Old Wolf

          #5
          Re: Language Subtlety or Compiler bug?

          nharding@extend thereach.com (Ned Harding) wrote:[color=blue]
          > In VC7.1 the following code outputs:
          > 00000000
          > Success!
          >
          > It would seem that the Y::operator<< is hiding the global operator<<
          > when it is outputting an object from another namespace but not when it
          > is outputting an object in the global namespace.[/color]

          Actually the behaviour is correct.
          Also the Y::operator<< is irrelevant, the code will behave the same
          without it.

          [Abbreviated, equivalent code]:[color=blue]
          > #include <iostream>
          >
          > namespace X { class Abc1 {} };
          >
          > std::ostream& operator<< (std::ostream& strm, const X::Abc1 *node)
          > { return strm << "Success!"; }
          >
          > class Abc2 { };
          >
          > std::ostream& operator<< (std::ostream& strm, const Abc2 *node)
          > { return strm << "Success!"; }
          >
          > namespace Y {
          > void DoTest() {
          > std::cout << (X::Abc1 *)0 << "\n";
          > std::cout << (Abc2 *)0 << "\n";
          > }
          > }
          >
          > int main() {
          > Y::DoTest();
          > }[/color]

          The first example looks for:
          operator<< (std::ostream &, X::Abc1 *)
          in the namespaces 'std' and 'X'. It finds neither.
          It doesn't look in the global namespace because none of its
          parameter types are in the global namespace.

          However there is an implicit conversion (X::Abc1 *) --> (void *)
          so it finds std::operator<< (std::ostream &, void *).

          The second example looks for:
          operator<< (std::ostream &, ::Abc2 *)
          in the namespace 'std' and the global namespace. It finds
          your std::ostream& operator<< (std::ostream& strm, const Abc2 *node)
          from the global namespace.
          [color=blue]
          > namespace Y {
          > std::ostream & operator<<(std: :ostream& a, const char& b)
          > };[/color]

          Why did you include that function? It has nothing to do with
          anything else in this program because you never go
          std::cout << (something with char type or convertible to char).

          Comment

          • Ned Harding

            #6
            Re: Language Subtlety or Compiler bug?

            oldwolf@inspire .net.nz (Old Wolf) wrote in message[color=blue]
            > Why did you include that function? It has nothing to do with
            > anything else in this program because you never go
            > std::cout << (something with char type or convertible to char).[/color]

            That's the weird part. If you take out Y::operator << (which I agree
            never gets called) then I get

            Success!
            Success!

            I'm not sure why y::operator << is hiding anything, but if it hides
            one of the ::operator<<, why doesn't it hide both?

            ned.

            Comment

            • Howard

              #7
              Re: Language Subtlety or Compiler bug?


              "Ned Harding" <nharding@exten dthereach.com> wrote in message
              news:34c6c406.0 409010658.1567a b88@posting.goo gle.com...[color=blue]
              > oldwolf@inspire .net.nz (Old Wolf) wrote in message[color=green]
              > > Why did you include that function? It has nothing to do with
              > > anything else in this program because you never go
              > > std::cout << (something with char type or convertible to char).[/color]
              >
              > That's the weird part. If you take out Y::operator << (which I agree
              > never gets called) then I get
              >
              > Success!
              > Success!
              >
              > I'm not sure why y::operator << is hiding anything, but if it hides
              > one of the ::operator<<, why doesn't it hide both?
              >
              > ned.[/color]

              Try moving the operator that takes the X::Abc1* parameter INSIDE the X
              namespace. Then you won't have any problem.

              BTW, I have no idea how it's possible to "hide" the operator like you've
              described, which I've verified with all kinds of variations of that Y::
              operator. But even though it's never called, it does indeed prevent VC7
              from "seeing" the global namespace operator that takes a parameter type from
              another namespace. But I tend not to sweat stuff like that, once I've found
              a solution! :-)

              -Howard




              Comment

              • Old Wolf

                #8
                Re: Language Subtlety or Compiler bug?

                nharding@extend thereach.com (Ned Harding) wrote:[color=blue]
                > oldwolf@inspire .net.nz (Old Wolf) wrote in message[color=green]
                > > Why did you include that function? It has nothing to do with
                > > anything else in this program because you never go
                > > std::cout << (something with char type or convertible to char).[/color]
                >
                > That's the weird part. If you take out Y::operator << (which I agree
                > never gets called) then I get
                >
                > Success!
                > Success![/color]

                I don't (gcc 3.4.1). Perhaps it is a compiler bug in MSVC.
                (On BCC 5.5.1, both versions give Success! Success!, I suppose it
                looks in the global namespace when it should not.. BCC is not
                famous for its lack of bugs)
                [color=blue]
                > I'm not sure why y::operator << is hiding anything, but if it hides
                > one of the ::operator<<, why doesn't it hide both?[/color]

                Comment

                Working...