Effective C++: Item 26

B

Bart Vandewoestyne

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
 
V

Victor Bazarov

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
 
B

Bart Vandewoestyne

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
 
V

Victor Bazarov

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
 
P

ptyxs

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
 
N

Nobody

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".
 
R

Richard Delorme

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.
 
J

Jorgen Grahn

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
 
P

ptyxs

Le 21/09/2012 14:05, Nobody a écrit :
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
 
P

ptyxs

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
 
B

Bart Vandewoestyne

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
 
N

Nobody

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).
 
B

Balog Pal

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top