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

should this compile?

12 views
Skip to first unread message

usene...@lehrerfamily.com

unread,
Mar 24, 2009, 6:17:53 PM3/24/09
to
The following code fails to compile on my platform. Testing Comeau
online when they use the same EDG parser we use it fails. More recent
versions of their compiler successfully compile the code.

struct foo {
foo();
private:
foo(const foo &rhs);
};

void func1(const foo &);

void func2() {
func1( foo() );
}


The question is, does "func1(foo());" require access to foo's copy
constructor? If it does, it may elide the call, but the copy
constructor must still be accessible.

A co-worker was able to dig the following out of the standard which he
says proves the code should NOT compile. Do you agree? My reading of
8.5.3 is that a temporary is created by copying the RValue produced by
foo():


No, it should not compile.
5.2.2
4)
"When a function is called, each parameter (8.3.5) shall be
initialized (8.5, 12.8, 12.1) with its corresponding argument."

8.5.3
... ellided ...
3)
"A reference to type “cv1 T1” is initialized by an expression of type
“cv2 T2” as follows:"
... elidded ...
"— If the initializer expression is an rvalue, with T2 a class type,
and “cv1 T1” is reference-compatible
with “cv2 T2,” the reference is bound in one of the following ways
(the choice is implementation-defined):"

-- The reference is bound to the object represented by the rvalue (see
3.10) or to a sub-object within that object.
— A temporary of type “cv1 T2” [sic] is created, and a constructor is
called to copy the entire rvalue object into the temporary. The
reference is bound to the temporary or to a sub-object within the
temporary.93)
The constructor that would be used to make the copy shall be callable
whether or not the copy is actually done."

thanks for your help.

-Joshua


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

litb

unread,
Mar 25, 2009, 5:25:25 AM3/25/09
to
On 24 Mrz., 23:17, usenet_...@lehrerfamily.com wrote:
> The following code fails to compile on my platform. Testing Comeau
> online when they use the same EDG parser we use it fails. More recent
> versions of their compiler successfully compile the code.
>
> struct foo {
> foo();
> private:
> foo(const foo &rhs);
>
> };
>
> void func1(const foo &);
>
> void func2() {
> func1( foo() );
>
> }
>

I'm quite sure that will compile with C++1x, but it will fail with
current C++03 rules, because of the paragraph you quoted. In C++1x,
the rules say that the created temporary (which is a rvalue) is bound
without a copy to the const reference. Actually, the comeau online
compiler has a switch to enable C++1x extensions. One of those
extensions they implemented precisely is this bind-to-rvalues-without-
requiring-copy-ctor. If you disable that extension, you will see it
being directed by their online compiler too.

Reference: http://www.comeaucomputing.com/iso/cwg_defects.html#391 and
http://www.comeaucomputing.com/439features.html

0 new messages