Mistake in C++ Reference Guide
Posted on April 28th, 2007 at 14:36 by fr3@K

I recently came across a mistake in The rvalue Reference Proposal published in Informit’s C++ Reference Guide.

Let’s first take a look at the example code snips in section Overloading and Overload Resolution. Please note the last two lines, where the error lies:


void f(const A& a); // #1 lvalue reference
void f(A&& a);    // #2 rvalue reference


struct A {};
A f();
const A cf();
A a;
const A ca;
f(a);        // calls #1
f(ca);       // calls #1
f(source());    // calls #2
f(const_source()); // calls #1

With this example alone, I was unable to tell what source() or const_source() was. After some Googling, I dug up another example in a paper published by the C++ Standards Committee, and it closely resembles the example from Informit:


void foo(const A& t);  // #1
void foo(A&& t);       // #2


struct A {};
A source();
const A const_source();
...
A a;
const A ca;
foo(a);               // binds to #1
foo(ca);              // binds to #1
foo(source());        // binds to #2
foo(const_source());  // binds to #1

Alright, this example from C++ Standard Committee’s paper made sense. The source() and const_source() were two generator functions (functions those return values but take no argument). Their return values were used as arguments to two of the overloaded foo()s.

del.icio.us:Mistake in C++ Reference Guide digg:Mistake in C++ Reference Guide spurl:Mistake in C++ Reference Guide newsvine:Mistake in C++ Reference Guide furl:Mistake in C++ Reference Guide Y!:Mistake in C++ Reference Guide 黑米共享書籤:Mistake in C++ Reference Guide 推推王:Mistake in C++ Reference Guide
Previous Post
« Compiz, Not So Cool «
Next Post
» 週三公司早餐報名 »

4 Comments »

Comment #2778

void foo(A&& t); // #2

What is A&&? You cannot have a reference to a reference. It is disallowed in C++.

Adrian

Comment by Adrian — May 2, 2007 @ 1:12


Comment #2780

Adrian,

The A&& thing is a proposal (i.e. not in the stadard, yet) called rvalue reference, which you would’ve been familiar with (or at least read about) if you followed links in this post.

It’s true that one shouldn’t code something like A&& under current C++ standard, and it’s likely true those code won’t compile (if not interpreted as something else by faulty compilers), neither. But apparently, you have no clue what it was.

Please be less assertive in wording next time, otherwise I would expect you to know what you are talking about, or at least followed links in my post.

Comment by fr3@K — May 2, 2007 @ 10:05


Comment #2786

My bad. I had other things on my mind so I wasn’t reading it carefully. I apologise for my assertiveness.

Comment by Adrian — May 2, 2007 @ 19:44


Comment #2787

Peace :-)

Comment by fr3@K — May 2, 2007 @ 21:42


Comments RSS TrackBack URI

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>