Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Effective C++: Item 26

239 views
Skip to first unread message

Bart Vandewoestyne

unread,
Sep 20, 2012, 11:02:00 AM9/20/12
to
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/BartVandewoestyne/cpp/blob/master/books/Effective_C%2B%2B/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

Victor Bazarov

unread,
Sep 20, 2012, 2:40:23 PM9/20/12
to
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

Bart Vandewoestyne

unread,
Sep 20, 2012, 3:45:33 PM9/20/12
to
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

Victor Bazarov

unread,
Sep 20, 2012, 6:49:30 PM9/20/12
to
I say the g++ compiler is wrong. At least three other compilers (Comeau
online, VC++ 2010, VC++ 2012) report an error...

ptyxs

unread,
Sep 21, 2012, 7:14:56 AM9/21/12
to Bart Vandewoestyne
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


Nobody

unread,
Sep 21, 2012, 8:05:14 AM9/21/12
to
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".

Richard Delorme

unread,
Sep 21, 2012, 8:19:51 AM9/21/12
to
Le 20/09/2012 17:02, Bart Vandewoestyne a écrit :
Try to compile your code with the -pedantic options.

--
Richard

Scott Lurndal

unread,
Sep 21, 2012, 9:55:13 AM9/21/12
to
Indeed:

$ g++ -pedantic -o /tmp/a /tmp/a.cpp
/tmp/a.cpp: In function 'int main()':
/tmp/a.cpp:39: error: conversion from 'B' to 'const A' is ambiguous
/tmp/a.cpp:22: note: candidates are: B::operator A() const
/tmp/a.cpp:13: note: A::A(const B&)
$

Bart Vandewoestyne

unread,
Sep 21, 2012, 10:47:49 AM9/21/12
to
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

unread,
Sep 21, 2012, 10:51:29 AM9/21/12
to
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

Jorgen Grahn

unread,
Sep 22, 2012, 4:19:49 AM9/22/12
to
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 .

ptyxs

unread,
Sep 24, 2012, 10:59:45 AM9/24/12
to Nobody
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

unread,
Sep 24, 2012, 11:02:09 AM9/24/12
to Bart Vandewoestyne

Bart Vandewoestyne

unread,
Sep 25, 2012, 5:04:26 AM9/25/12
to
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

Nobody

unread,
Sep 26, 2012, 6:23:02 PM9/26/12
to
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).

Balog Pal

unread,
Oct 5, 2012, 6:01:04 PM10/5/12
to
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.

0 new messages