This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: non-const reference to temporary (was: non-const copy ctor)


Andrew Haley writes:
 > Christian Fröbel writes:
 >  > Hi there,
 >  > 
 >  > Christian Fröbel wrote:
 >  > > I'm having problems with a non-const copy constructor.
 >  > 
 >  > I'd like to correct myself. This problem dosn't seem to be related to the copy 
 >  > constructor at all. The problem is that I'm not allowed to make a non-const 
 >  > reference to a temporary object. For example code like this doesn't compile:
 >  > 
 >  > class C {};
 >  > 
 >  > void foo (C& c) {}
 >  > 
 >  > int main (void)
 >  > {
 >  > 	foo (C()); // <== problem
 >  > 	return 0;
 >  > }
 >  > 
 >  > The error message I get here is:
 >  > conversion.cpp:48: error: invalid initialization of non-const reference of 
 >  > type øC&ù from a temporary of type øCù
 >  > conversion.cpp:42: error: in passing argument 1 of øvoid foo(C&)ù
 >  > 
 >  > I read §5.5 in Stroustrup's C++ Programming Language regadring that topic but 
 >  > couldn't find anything of real use there.
 > 
 > I can.  "The initializer of a const T& need not be an lvalue..."  In
 > other words, although he doesn't explicitly say so, the initializer of
 > a const T& has to be an lvalue.

Sorry, a non-const T&.

Andrew.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]