Bug 14618 - Document copy constructor constraints in binding rvalues to references
Summary: Document copy constructor constraints in binding rvalues to references
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.0
: P2 normal
Target Milestone: 3.4.0
Assignee: Giovanni Bajo
URL:
Keywords: documentation
: 14774 (view as bug list)
Depends on:
Blocks:
 
Reported: 2004-03-17 09:38 UTC by Peter Schmid
Modified: 2004-07-13 17:02 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2004-03-17 22:54:16


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Peter Schmid 2004-03-17 09:38:31 UTC
The following code s.C, reduced from the original file libs/thread/test/
test_thread.cpp included in current boost repository, is rejected by gcc 3.4, 
but accepted by gcc 3.3. I have not investigated but given the error messages I 
guess this problem is also responsible for the failure of the libs/python/test/
embedding.cpp test. This looks like a regression to me. 
 
source code s.C  
 
namespace boost { 
class noncopyable 
{ 
 protected: 
    noncopyable() {} 
    ~noncopyable() {} 
 private: 
    noncopyable( const noncopyable& ); 
    const noncopyable& operator=( const noncopyable& ); 
}; 
 
 
class thread : private noncopyable 
{ 
public: 
    bool operator==(const thread& other) const; 
}; 
} // namespace boost 
 
 
void comparison_thread(boost::thread* parent) 
{ 
    boost::thread thrd; 
    if (thrd == boost::thread()) ; 
} 
 
g++ -v -W -Wall -c s.C  
Reading specs from /usr/local/lib/gcc/i686-pc-linux-gnu/3.4.0/specs 
Configured with: ../gcc/configure --enable-threads=posix --enable-languages=c,c
++,f77,objc --enable-__cxa_atexit --enable-libstdcxx-debug 
Thread model: posix 
gcc version 3.4.0 20040313 (prerelease) 
 /usr/local/libexec/gcc/i686-pc-linux-gnu/3.4.0/cc1plus -quiet -v -D_GNU_SOURCE 
s.C -quiet -dumpbase s.C -mtune=pentiumpro -auxbase s -W -Wall -version -o /
tmp/ccRINl8i.s 
ignoring nonexistent directory "NONE/include" 
ignoring nonexistent directory "/usr/local/lib/gcc/
i686-pc-linux-gnu/3.4.0/../../../../i686-pc-linux-gnu/include" 
#include "..." search starts here: 
#include <...> search starts here: 
 /usr/local/lib/gcc/i686-pc-linux-gnu/3.4.0/../../../../include/c++/3.4.0 
 /usr/local/lib/gcc/i686-pc-linux-gnu/3.4.0/../../../../include/c++/3.4.0/
i686-pc-linux-gnu 
 /usr/local/lib/gcc/i686-pc-linux-gnu/3.4.0/../../../../include/c++/3.4.0/
backward 
 /usr/local/include 
 /usr/local/lib/gcc/i686-pc-linux-gnu/3.4.0/include 
 /usr/include 
End of search list. 
GNU C++ version 3.4.0 20040313 (prerelease) (i686-pc-linux-gnu) 
	compiled by GNU C version 3.4.0 20040313 (prerelease). 
GGC heuristics: --param ggc-min-expand=64 --param ggc-min-heapsize=64274 
s.C: In copy constructor `boost::thread::thread(const boost::thread&)': 
s.C:8: error: `boost::noncopyable::noncopyable(const boost::noncopyable&)' is 
private 
s.C:24: error: within this context 
s.C: In function `void comparison_thread(boost::thread*)': 
s.C:22: warning: unused parameter 'parent'
Comment 1 Wolfgang Bangerth 2004-03-17 13:52:13 UTC
I think that the error is actually valid here. Consider 
------------------- 
class noncopyable  
{  
  protected:  
    noncopyable() {}  
    ~noncopyable() {}  
  private:  
    noncopyable( const noncopyable& );  
    const noncopyable& operator=( const noncopyable& );  
}; 
  
struct D : private noncopyable {};  
 
int foo(const D &); 
  
int i = foo (D()); 
------------------- 
 
You generate a temporary in the call to foo(), and the standard prescribes 
that when binding the temporary to the const reference, the copy constructor 
of D must be accessible, even if it is unused. Thus, the error 
 
g/x> /home/bangerth/bin/gcc-3.4-pre/bin/c++ -c x.cc 
x.cc: In copy constructor `D::D(const D&)': 
x.cc:7: error: `noncopyable::noncopyable(const noncopyable&)' is private 
x.cc:4: error: within this context 
 
seems sensible to me. However, the error messages do not refer to the 
line where we attempt to perform this copy (or where the compiler wants 
to check that the copy constructor is accessible). 
 
Nathan, Gaby, would you agree? 
 
W. 
Comment 2 Giovanni Bajo 2004-03-17 14:37:46 UTC
FWIW, I agree. We ran into this very problem also in PR 14528, it seems like 
GCC 3.4.0 is the first compiler to enforce this rule. Not even EDG does this.

Standard paragraphs that apply:

- [basic.lval]/6 says that "D()" is a rvalue.

- [dcl.init.ref]/5, bullet 2 ("Otherwise, the reference shall be to a non-
volatile const type"), sub-bullet 1: 

"If the initializer expression is a rvalue, the refence 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) 
[...]
— 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 [...]

***The constructor that would be used to make the copy shall be callable 
whether or not the copy is actually done.***"

The last sentence (with my stress) is the one that matters. Either GCC 
internally elides the copy or not, it does not matter, but the copy constructor 
has to be callable.


- [class.temporary]/2 talks about this another time: "Temporaries of class type 
are created in various contexts: binding an rvalue to a reference, [...]. Even 
when the creation of the temporary object is avoided, all the semantic 
restrictions must be respected as if the temporary object was created. 
[Example: even if the copy constructor is not called, all the semantic 
restrictions, such as accessibility (clause 11), shall be satisfied.]".
Comment 3 Gabriel Dos Reis 2004-03-17 15:08:14 UTC
Subject: Re:  Cannot compile test_thread.cpp from boost

"bangerth at dealii dot org" <gcc-bugzilla@gcc.gnu.org> writes:

| You generate a temporary in the call to foo(), and the standard prescribes 
| that when binding the temporary to the const reference, the copy constructor 
| of D must be accessible, even if it is unused. Thus, the error 
|  
| g/x> /home/bangerth/bin/gcc-3.4-pre/bin/c++ -c x.cc 
| x.cc: In copy constructor `D::D(const D&)': 
| x.cc:7: error: `noncopyable::noncopyable(const noncopyable&)' is private 
| x.cc:4: error: within this context 
|  
| seems sensible to me. However, the error messages do not refer to the 
| line where we attempt to perform this copy (or where the compiler wants 
| to check that the copy constructor is accessible). 
|  
| Nathan, Gaby, would you agree? 

Your analysis is correct.  Maybe we would like to put a documentation
for this in the "non bugs" section we used to have.  Any taker?
(that is becoming an FAQ).

-- Gaby
Comment 4 Gabriel Dos Reis 2004-03-17 15:11:06 UTC
Subject: Re:  Cannot compile test_thread.cpp from boost

"giovannibajo at libero dot it" <gcc-bugzilla@gcc.gnu.org> writes:

| FWIW, I agree. We ran into this very problem also in PR 14528, it seems like 
| GCC 3.4.0 is the first compiler to enforce this rule. Not even EDG does this.

Giovanni, please would you like submitring an FAQ  patch that documents
this?  It would save you to repeat yourself in the future -- I
conjecture we would have more reports on this issue.

-- Gaby
Comment 5 Giovanni Bajo 2004-03-17 22:54:15 UTC
(In reply to comment #4)

> Giovanni, please would you like submitring an FAQ  patch that documents
> this?  

Yes. I will put something also in changes.html.
Keeping this bug as a reminder for the documentation.
Comment 6 Gabriel Dos Reis 2004-03-17 23:22:16 UTC
Subject: Re:  Document copy constructor constraints in binding rvalues to references

"giovannibajo at libero dot it" <gcc-bugzilla@gcc.gnu.org> writes:

| > Giovanni, please would you like submitring an FAQ  patch that documents
| > this?  
| 
| Yes. I will put something also in changes.html.
| Keeping this bug as a reminder for the documentation.

Thanks!

-- Gaby
Comment 8 Drea Pinski 2004-03-29 18:24:51 UTC
*** Bug 14774 has been marked as a duplicate of this bug. ***