Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » C++Goto page 1, 2  Next

non-const reference to temporary

 
Jump to:  
 
WalterHoward@gmail.com
PostPosted: Sat Aug 16, 2008 2:10 am    Post subject: non-const reference to temporary
       
I have a disagreement with this "error". I'm constantly running into
this with g++, perhaps, hoping at some point the latest version has
changed this. For example, here's a function.

void WriteEverythingToFile( File& file, const std::string& text)
{
file.write(text);
}

The "File" can't be const in this case, for example, because in this
class, the write operation alters some data members.

Now say I just want to quickly write some stuff to a file and I don't
care about the file object persisting or leaving any extraneous
temporary variables lying around.

WriteEverythingToFile(File("AFileName.txt"), std::string("This stuff
goes into the file"));

g++ will tell me this is an error because I'm passing a non const
reference to a temporary (anonymous variable with no name).

First of all this isn't an error, except by someones overly broad
definition of error. Other people say "why would you pass a non-const
reference to something which will be modified, but you don't care
about the modification?".

Exactly. I don't care about the modification. I just want the function
to do the job I request.

g++ doesn't complain about this type of construct:
File("afilename").write("this is some text"); which has all the same
criticisms, "Why are you creating an non-const object, if you don't
save it to a variable?". Exactly, I don't want to save it, I just want
to write to the file and be done with it, no extraneous variables
hanging around, no greasy kid stuff.

And please, don't say, "why don't you do it this way". I know how to
work around this. The problem is, I shouldn't have to.

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Mathias Gaunard
PostPosted: Sat Aug 16, 2008 11:52 pm    Post subject: Re: non-const reference to temporary
       
On 16 août, 04:10, "WalterHow...@gmail.com" <WalterHow...@gmail.com>
wrote:

Quote:
WriteEverythingToFile(File("AFileName.txt"), std::string("This stuff
goes into the file"));

g++ will tell me this is an error because I'm passing a non const
reference to a temporary (anonymous variable with no name).

And it is.


Quote:
First of all this isn't an error, except by someones overly broad
definition of error.

The C++ standard clearly says it is not allowed.
So it is an error.

There are several reasons why it was chosen to be so. Go back in time
and look into how C++ was designed.


Quote:
g++ doesn't complain about this type of construct:
File("afilename").write("this is some text"); which has all the same
criticisms, "Why are you creating an non-const object, if you don't
save it to a variable?". Exactly, I don't want to save it, I just want
to write to the file and be done with it, no extraneous variables
hanging around, no greasy kid stuff.

While binding an rvalue to a non-const reference isn't allowed, an
rvalue does not have to be const.
This is therefore perfectly valid.

(Note that binding an rvalue to a const-reference may cause a copy,
and converts the rvalue into an lvalue)


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Guest
PostPosted: Sun Aug 17, 2008 3:18 am    Post subject: Re: non-const reference to temporary
       
On Aug 15, 7:10 pm, "WalterHow...@gmail.com" <WalterHow...@gmail.com>
wrote:
Quote:
I have a disagreement with this "error". I'm constantly running into
this with g++, perhaps, hoping at some point the latest version has
changed this. For example, here's a function.

void WriteEverythingToFile( File& file, const std::string& text)
{
file.write(text);

}

The "File" can't be const in this case, for example, because in this
class, the write operation alters some data members.

Now say I just want to quickly write some stuff to a file and I don't
care about the file object persisting or leaving any extraneous
temporary variables lying around.

WriteEverythingToFile(File("AFileName.txt"), std::string("This stuff
goes into the file"));

g++ will tell me this is an error because I'm passing a non const
reference to a temporary (anonymous variable with no name).

First of all this isn't an error, except by someones overly broad
definition of error. Other people say "why would you pass a non-const
reference to something which will be modified, but you don't care
about the modification?".

Exactly. I don't care about the modification. I just want the function
to do the job I request.

g++ doesn't complain about this type of construct:
File("afilename").write("this is some text"); which has all the same
criticisms, "Why are you creating an non-const object, if you don't
save it to a variable?". Exactly, I don't want to save it, I just want
to write to the file and be done with it, no extraneous variables
hanging around, no greasy kid stuff.

And please, don't say, "why don't you do it this way". I know how to
work around this. The problem is, I shouldn't have to.

Now, yelling like this and being purposefully argumentative is
probably never a good thing. It's going to make people less likely to
respond.

It is an error by the C++ standard. The C++ clearly states that
unnamed objects, a.k.a. temporaries, may be bound to const references
and may not be bound to non-const references.

Is this a desirable rule of the language? I imagine it prevents a lot
of bufs, but as you've noted, it can be quiet inconvenient at times.
As an example, read up on the history of auto_ptr and how it
repeatedly hit this issue in its design.

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
red floyd
PostPosted: Sun Aug 17, 2008 3:21 am    Post subject: Re: non-const reference to temporary
       
WalterHoward@gmail.com wrote:
Quote:
I have a disagreement with this "error". I'm constantly running into
this with g++, perhaps, hoping at some point the latest version has
changed this. For example, here's a function.

void WriteEverythingToFile( File& file, const std::string& text)
{
file.write(text);
}

The "File" can't be const in this case, for example, because in this
class, the write operation alters some data members.

Now say I just want to quickly write some stuff to a file and I don't
care about the file object persisting or leaving any extraneous
temporary variables lying around.

WriteEverythingToFile(File("AFileName.txt"), std::string("This stuff
goes into the file"));

g++ will tell me this is an error because I'm passing a non const
reference to a temporary (anonymous variable with no name).

First of all this isn't an error, except by someones overly broad
definition of error.

No. It's an error because the STANDARD -- ISO/IEC 14882:2003 -- says
it's an error, not because some guy on the g++ team though ti should be.

Deal with it.

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
David Abrahams
PostPosted: Sun Aug 17, 2008 3:21 am    Post subject: Re: non-const reference to temporary
       
on Fri Aug 15 2008, "WalterHoward-AT-gmail.com" <WalterHoward-AT-gmail.com> wrote:

Quote:
I have a disagreement with this "error". I'm constantly running into
this with g++, perhaps, hoping at some point the latest version has
changed this.

g++ 4.3 (and the next C++ standard) has rvalue references, so, yes, it's
been changed.

--
Dave Abrahams
BoostPro Computing
LINK

[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
blargg
PostPosted: Sun Aug 17, 2008 3:22 am    Post subject: Re: non-const reference to temporary
       
In article
<d73726c4-60de-4702-9fb3-5bc87031c9fa@v16g2000prc.googlegroups.com>,
"WalterHoward@gmail.com" <WalterHoward@gmail.com> wrote:

Quote:
[...]
void WriteEverythingToFile( File& file, const std::string& text)
[...]
WriteEverythingToFile(File("AFileName.txt"), std::string("This stuff
goes into the file"));

g++ will tell me this is an error because I'm passing a non const
reference to a temporary (anonymous variable with no name).
[...]
And please, don't say, "why don't you do it this way". I know how to
work around this. The problem is, I shouldn't have to.

template<typename T>
inline T& nonconst( T const& t ) { return const_cast<T&> (t); }

WriteEverythingToFile( nonconst(File("AFileName.txt")),
"This stuff goes into the file" );

Just don't complain about the reduced compile-time checking.

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Chris Morley
PostPosted: Sun Aug 17, 2008 3:24 am    Post subject: Re: non-const reference to temporary
       
Quote:
WriteEverythingToFile(File("AFileName.txt"), std::string("This stuff
goes into the file"));

g++ will tell me this is an error because I'm passing a non const
reference to a temporary (anonymous variable with no name).

No I wouldn't have though it was an error either. What is your File
constructor like?

Does this compile & run?

Chris

---- 8< -------- 8< -------- 8< -------- 8< -------- 8< -------- 8< ----

#include <string>
#include "stdio.h"
#include "conio.h"

class File {
public:
// File( const char* fn ) : filename(fn) {} //ok too
File( const std::string& fn ) : filename(fn) {}
~File() { display(); }

void write( const std::string& stuff ) { buf += stuff; }

void display();

private:
std::string filename;
std::string buf;
};

void File::display()
{
printf( "Filename: %s\n", filename.c_str() );
printf( "Contents:\n%s\n", buf.c_str() );
printf( "Display end...\n\n" );
}

void WriteEverythingToFile( File& file, const std::string& text)
{
file.write(text);
}

int main(int argc, char* argv[])
{
WriteEverythingToFile(File("AFileName.txt"), std::string("This stuff goes
into the file"));
File("afilename").write("this is some text");

_getch();
return 0;
}



--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Dave Harris
PostPosted: Sun Aug 17, 2008 3:27 am    Post subject: Re: non-const reference to temporary
       
WalterHoward@gmail.com () wrote (abridged):
Quote:
g++ will tell me this is an error because I'm passing a non const
reference to a temporary (anonymous variable with no name).

First of all this isn't an error, except by someones overly broad
definition of error. Other people say "why would you pass a
non-const reference to something which will be modified, but
you don't care about the modification?".

Exactly. I don't care about the modification. I just want the
function to do the job I request.

I agree it shouldn't be an error. The reason it is treated as one is that
the construct is, in general, error-prone, because of implicit
conversions. Code like:

void inc( long &x ) { ++x; }

void test() {
int y = 0;
inc( y );
std::cout << y;
}

if it compiled, would output 0 not 1, because the int is converted to a
temporary long and the increment lost.

The problem here is the implicit conversion, not the reference to
temporary. Unfortunately, rather than try to ban non-const references to
temporaries that resulted from implicit conversions, the standard bans
non-const references to all temporaries, which is over-kill.

The proposed solution, apparently, is a new kind of reference, spelt "&&",
that can bind to temporaries. I think if your function has the signature:
void WriteEverythingToFile( File&& file, const std::string& text);

it will work in C++0x. But I'm not an expert on the new stuff, and it
doesn't help you now, nor will it unless you can rewrite the function you
need to call. The situation is less than ideal. You can see, though, that
there are real problems lurking here which are non-trivial to solve, and
the quote you attribute to "other people" doesn't do the problems
justice.

-- Dave Harris, Nottingham, UK.

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Stuart Golodetz
PostPosted: Sun Aug 17, 2008 2:46 pm    Post subject: Re: non-const reference to temporary
       
WalterHoward@gmail.com wrote:
Quote:
I have a disagreement with this "error". I'm constantly running into
this with g++, perhaps, hoping at some point the latest version has
changed this. For example, here's a function.

void WriteEverythingToFile( File& file, const std::string& text)
{
file.write(text);
}

The "File" can't be const in this case, for example, because in this
class, the write operation alters some data members.

Now say I just want to quickly write some stuff to a file and I don't
care about the file object persisting or leaving any extraneous
temporary variables lying around.

WriteEverythingToFile(File("AFileName.txt"), std::string("This stuff
goes into the file"));

g++ will tell me this is an error because I'm passing a non const
reference to a temporary (anonymous variable with no name).

First of all this isn't an error, except by someones overly broad
definition of error. Other people say "why would you pass a non-const
reference to something which will be modified, but you don't care
about the modification?".

Exactly. I don't care about the modification. I just want the function
to do the job I request.

g++ doesn't complain about this type of construct:
File("afilename").write("this is some text"); which has all the same
criticisms, "Why are you creating an non-const object, if you don't
save it to a variable?". Exactly, I don't want to save it, I just want
to write to the file and be done with it, no extraneous variables
hanging around, no greasy kid stuff.

And please, don't say, "why don't you do it this way". I know how to
work around this. The problem is, I shouldn't have to.

See:

LINK

Specifically the bit entitled "Binding temporaries to references".

Regards,
Stu

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Howard Hinnant
PostPosted: Sun Aug 17, 2008 3:28 pm    Post subject: Re: non-const reference to temporary
       
On Aug 15, 10:10 pm, "WalterHow...@gmail.com" <WalterHow...@gmail.com>
wrote:
Quote:
I have a disagreement with this "error". I'm constantly running into
this with g++, perhaps, hoping at some point the latest version has
changed this. For example, here's a function.

void WriteEverythingToFile( File& file, const std::string& text)
{
file.write(text);

}

The "File" can't be const in this case, for example, because in this
class, the write operation alters some data members.

Now say I just want to quickly write some stuff to a file and I don't
care about the file object persisting or leaving any extraneous
temporary variables lying around.

WriteEverythingToFile(File("AFileName.txt"), std::string("This stuff
goes into the file"));

g++ will tell me this is an error because I'm passing a non const
reference to a temporary (anonymous variable with no name).

First of all this isn't an error, except by someones overly broad
definition of error. Other people say "why would you pass a non-const
reference to something which will be modified, but you don't care
about the modification?".

Exactly. I don't care about the modification. I just want the function
to do the job I request.

g++ doesn't complain about this type of construct:
File("afilename").write("this is some text"); which has all the same
criticisms, "Why are you creating an non-const object, if you don't
save it to a variable?". Exactly, I don't want to save it, I just want
to write to the file and be done with it, no extraneous variables
hanging around, no greasy kid stuff.

And please, don't say, "why don't you do it this way". I know how to
work around this. The problem is, I shouldn't have to.

There will be a new reference type in the next C++ standard (C++0X,
where X is hexadecimal Sad) which allows you to do exactly what you
want. Search for "rvalue reference". If the author of
WriteEverythingToFile writes:

void WriteEverythingToFile( File&& file, const std::string& text)
{
file.write(text);
}

then rvalue File's will bind (note the extra '&'). You can experiment
with this new language feature today in a few compilers such as gcc
4.3 (with C++0X features turned on as a command line flag to the
compiler).

-Howard


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

Page 1 of 2 .:. Goto page 1, 2  Next

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

banery bodyguard Transport opony matador Zachowania nabywców - seria: Marketing bez tajemni