Effective C++: Item 26

Discussion in 'C++' started by Bart Vandewoestyne, Sep 20, 2012.

  1. I am trying to reproduce the ambiguity problem from the first example in Item 26 from Scott Meyers' book 'Effective C++'. My code is online at

    https://github.com/BartVandewoestyn.../Effective_C++/item26/potential_ambiguity.cpp

    In the book, no definition of A's copy constructor nor B's operator A() is given, so i defined it myself... hopefully correct somehow...

    Compiling this code with g++ 4.6.2 and the -Wall option succeeds without errors and when I run it, i get:

    A(const B&)
    f(const A&)

    so apparently A's copy constructor gets called and there's no ambiguity problem.

    I was suspecting am ambiguity problem at compile time... what am I doing/interpreting wrong?

    Regards,
    Bart
    Bart Vandewoestyne, Sep 20, 2012
    #1
    1. Advertising

  2. On 9/20/2012 11:02 AM, Bart Vandewoestyne wrote:
    > I am trying to reproduce the ambiguity problem from the first example in Item 26 from Scott Meyers' book 'Effective C++'. My code is online at
    >
    > https://github.com/BartVandewoestyn.../Effective_C++/item26/potential_ambiguity.cpp
    >
    > In the book, no definition of A's copy constructor nor B's operator A() is given, so i defined it myself... hopefully correct somehow...
    >
    > Compiling this code with g++ 4.6.2 and the -Wall option succeeds without errors and when I run it, i get:
    >
    > A(const B&)
    > f(const A&)
    >
    > so apparently A's copy constructor gets called and there's no ambiguity problem.
    >
    > I was suspecting am ambiguity problem at compile time... what am I doing/interpreting wrong?


    Not sure about the compiler you used. Comeau online gives an error
    exactly where you expected it.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 20, 2012
    #2
    1. Advertising

  3. On Thursday, September 20, 2012 8:41:14 PM UTC+2, Victor Bazarov wrote:
    >
    > Not sure about the compiler you used. Comeau online gives an error
    > exactly where you expected it.


    I now tested with g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 and everything compiles _without_ any ambiguity error...

    Who's wrong here? The book or the compiler?

    Regards,
    Bart
    Bart Vandewoestyne, Sep 20, 2012
    #3
  4. On 9/20/2012 3:45 PM, Bart Vandewoestyne wrote:
    > On Thursday, September 20, 2012 8:41:14 PM UTC+2, Victor Bazarov wrote:
    >>
    >> Not sure about the compiler you used. Comeau online gives an error
    >> exactly where you expected it.

    >
    > I now tested with g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 and everything compiles _without_ any ambiguity error...
    >
    > Who's wrong here? The book or the compiler?


    I say the g++ compiler is wrong. At least three other compilers (Comeau
    online, VC++ 2010, VC++ 2012) report an error...

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 20, 2012
    #4
  5. Bart Vandewoestyne

    ptyxs Guest

    Le 20/09/2012 17:02, Bart Vandewoestyne a écrit :
    > I am trying to reproduce the ambiguity problem from the first example in Item 26 from Scott Meyers' book 'Effective C++'. My code is online at
    >
    > https://github.com/BartVandewoestyn.../Effective_C++/item26/potential_ambiguity.cpp
    >
    > In the book, no definition of A's copy constructor nor B's operator A()is given, so i defined it myself... hopefully correct somehow...
    >
    > Compiling this code with g++ 4.6.2 and the -Wall option succeeds without errors and when I run it, i get:
    >
    > A(const B&)
    > f(const A&)
    >
    > so apparently A's copy constructor gets called and there's no ambiguityproblem.
    >
    > I was suspecting am ambiguity problem at compile time... what am I doing/interpreting wrong?
    >
    > Regards,
    > Bart
    >


    In my Effective C++ THIRD EDITION, Item 26 is entitled : "Postpone
    variable definitions as long as possible".

    Please tell what edition you are using and what is the title of the item
    you are discussing.
    Thanks.
    Ptyxs
    ptyxs, Sep 21, 2012
    #5
  6. Bart Vandewoestyne

    Nobody Guest

    On Fri, 21 Sep 2012 13:14:56 +0200, ptyxs wrote:

    > In my Effective C++ THIRD EDITION, Item 26 is entitled : "Postpone
    > variable definitions as long as possible".
    >
    > Please tell what edition you are using and what is the title of the item
    > you are discussing.


    I'm fairly sure that it's the second edition, where item 26 is titled
    "Guard against potential ambiguity".
    Nobody, Sep 21, 2012
    #6
  7. Le 20/09/2012 17:02, Bart Vandewoestyne a écrit :
    > I am trying to reproduce the ambiguity problem from the first example in Item 26 from Scott Meyers' book 'Effective C++'. My code is online at
    >
    > https://github.com/BartVandewoestyn.../Effective_C++/item26/potential_ambiguity.cpp
    >
    > In the book, no definition of A's copy constructor nor B's operator A() is given, so i defined it myself... hopefully correct somehow...
    >
    > Compiling this code with g++ 4.6.2 and the -Wall option succeeds without errors and when I run it, i get:
    >
    > A(const B&)
    > f(const A&)
    >
    > so apparently A's copy constructor gets called and there's no ambiguity problem.
    >
    > I was suspecting am ambiguity problem at compile time... what am I doing/interpreting wrong?


    Try to compile your code with the -pedantic options.

    --
    Richard
    Richard Delorme, Sep 21, 2012
    #7
  8. On Friday, September 21, 2012 2:05:03 PM UTC+2, Nobody wrote:
    >
    > I'm fairly sure that it's the second edition, where item 26 is titled
    > "Guard against potential ambiguity".


    Indeed.

    Regards,
    Bart
    Bart Vandewoestyne, Sep 21, 2012
    #8
  9. On Friday, September 21, 2012 2:19:52 PM UTC+2, Richard Delorme wrote:
    >
    > Try to compile your code with the -pedantic options.


    Indeed! That gives me the ambiguity error!

    Thanks!
    Bart
    Bart Vandewoestyne, Sep 21, 2012
    #9
  10. Bart Vandewoestyne

    Jorgen Grahn Guest

    On Fri, 2012-09-21, Bart Vandewoestyne wrote:
    > On Friday, September 21, 2012 2:19:52 PM UTC+2, Richard Delorme wrote:
    >>
    >> Try to compile your code with the -pedantic options.

    >
    > Indeed! That gives me the ambiguity error!


    I've said it before, but I recommend always using -pedantic (and
    several other warning options) with g++. Lots of things which end up
    as questions here (often much more trivial than this) would have been
    caught that way.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Sep 22, 2012
    #10
  11. Bart Vandewoestyne

    ptyxs Guest

    Le 21/09/2012 14:05, Nobody a écrit :
    > On Fri, 21 Sep 2012 13:14:56 +0200, ptyxs wrote:
    >
    >> In my Effective C++ THIRD EDITION, Item 26 is entitled : "Postpone
    >> variable definitions as long as possible".
    >>
    >> Please tell what edition you are using and what is the title of the item
    >> you are discussing.

    >
    > I'm fairly sure that it's the second edition, where item 26 is titled
    > "Guard against potential ambiguity".
    >


    One interesting thing to note is that in the THIRD EDITION, pp.278-279,
    Appendix B, Item Mapping Between Second and Third Edition shows that
    there is no mapping from second edition item 26 to any third edition item.

    Would that suggest that the author, as an afterthought, considered that
    second edition item 26 was somehow inaccurate or useless ??

    Ptyxs
    ptyxs, Sep 24, 2012
    #11
  12. Bart Vandewoestyne

    ptyxs Guest

    > On Friday, September 21, 2012 2:05:03 PM UTC+2, Nobody wrote:
    >>
    >> I'm fairly sure that it's the second edition, where item 26 is titled
    >> "Guard against potential ambiguity".

    >
    > Indeed.
    >
    > Regards,
    > Bart
    >



    One interesting thing to note is that in the THIRD EDITION, pp.278-279,
    Appendix B, Item Mapping Between Second and Third Edition shows that
    there is no mapping from second edition item 26 to any third edition item.

    Would that suggest that the author, as an afterthought, considered that
    second edition item 26 was somehow inaccurate or useless ??

    Ptyxs
    ptyxs, Sep 24, 2012
    #12
  13. On Saturday, September 22, 2012 10:19:50 AM UTC+2, Jorgen Grahn wrote:
    >
    > I've said it before, but I recommend always using -pedantic (and
    > several other warning options) with g++. Lots of things which end up
    > as questions here (often much more trivial than this) would have been
    > caught that way.


    It's nice to know that the ambiguity error pops up in pedantic mode. I have emailed Scott Meyers about this, and now both him and me are actually puzzled why it *only* pops up in pedantic mode... Shouldn't that ambiguity error pop up without the -pedantic option too?

    Regards,
    Bart
    Bart Vandewoestyne, Sep 25, 2012
    #13
  14. Bart Vandewoestyne

    Nobody Guest

    On Tue, 25 Sep 2012 02:04:26 -0700, Bart Vandewoestyne wrote:

    > It's nice to know that the ambiguity error pops up in pedantic mode. I
    > have emailed Scott Meyers about this, and now both him and me are
    > actually puzzled why it *only* pops up in pedantic mode... Shouldn't
    > that ambiguity error pop up without the -pedantic option too?


    Without -pedantic, gcc extensions are still enabled so long as they do not
    affect the interpretation of valid code.

    The -std= option should ensure that all valid code is interpreted as
    specified by the standard. The -pedantic option is required if you
    also want invalid code to be handled as specified by the standard (i.e.
    generating any required diagnostics).
    Nobody, Sep 26, 2012
    #14
  15. Bart Vandewoestyne

    Balog Pal Guest

    On 9/24/2012 5:02 PM, ptyxs wrote:
    > One interesting thing to note is that in the THIRD EDITION, pp.278-279,
    > Appendix B, Item Mapping Between Second and Third Edition shows that
    > there is no mapping from second edition item 26 to any third edition item.
    >
    > Would that suggest that the author, as an afterthought, considered that
    > second edition item 26 was somehow inaccurate or useless ??


    No, just less important than the other items that made into the 3rd ed.
    Balog Pal, Oct 5, 2012
    #15
    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. Don Kim
    Replies:
    9
    Views:
    552
    Gregg
    May 23, 2004
  2. Matthias
    Replies:
    25
    Views:
    992
    Thorsten Ottosen
    Feb 1, 2005
  3. John
    Replies:
    4
    Views:
    640
    Ioannis Vranos
    Apr 27, 2005
  4. Replies:
    1
    Views:
    422
    benben
    Aug 7, 2005
  5. Replies:
    4
    Views:
    423
Loading...

Share This Page