473,322 Members | 1,232 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Re: Problem "taking address of temporary"

On Nov 18, 5:50 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-11-18 10:52:48 -0500, Andy Gibbs <andyg1...@hotmail.co.uksaid:
The problem comes in the line "Function(&Test("test2"));"
with the warning "taking address of temporary". I am using
GCC 4.1.2. However, the code runs as expected with the
following output:
test1:
ctor
str=test1
dtor
test2:
ctor
str=test2
dtor
test3
null
The code above demonstrates that the Test object is not
destroyed until after Function is called, so I cannot see
why the compiler complains about this.
The compiler warns about it because the code takes the address
of a temporary, although there's nothing inherently wrong with
doing that.
Has this changed in the latest draft. According to my copy of
the standard (version 1998---out of date, I know), "The
operand [of the unary & operator] shall be an lvalue or a
qualified-id". His expression was &Test("test2"); IMHO, the
compiler generated a warning because it was being laxist.

If I compile his code with Sun CC, I get:
"addrtemp.cc", line 34: Warning, badargtypel2w: String literal
converted to char* in formal argument str in call to Test::Test
(char*).
"addrtemp.cc", line 37: Warning, badargtypel2w: String literal
converted to char* in formal argument str in call to Test::Test
(char*).
"addrtemp.cc", line 37: Error, wantvarname: The "&" operator can
only be applied to a variable or other l-value.
Which is what I'd expect from a good compiler. Curiously
enough, g++ only generates says:
addrtemp.cc: In function 'int main(int, char**)':
addrtemp.cc:37: warning: taking address of temporary
even with -std=c++98 -pedantic (I'd call this a bug), and VC++
doesn't say anything.
If you misuse the result you can get in trouble. Apparently
the writers of your compiler thnk that you can't be trusted to
use that address without screwing up.
Apparently, the writers of his compiler don't care about the
standard. (Nothing new there.) And it's the members of the
stadnards committee who think you can't be trusted. (Or just
wanted to remain compatible with C in this respect.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 18 '08 #1
4 7069
James Kanze wrote:
On Nov 18, 5:50 pm, Pete Becker <p...@versatilecoding.comwrote:
>On 2008-11-18 10:52:48 -0500, Andy Gibbs <andyg1...@hotmail.co.uksaid:
>>The problem comes in the line "Function(&Test("test2"));"
with the warning "taking address of temporary". I am using
GCC 4.1.2. However, the code runs as expected with the
following output:
>>test1:
ctor
str=test1
dtor
test2:
ctor
str=test2
dtor
test3
null
>>The code above demonstrates that the Test object is not
destroyed until after Function is called, so I cannot see
why the compiler complains about this.
>The compiler warns about it because the code takes the address
of a temporary, although there's nothing inherently wrong with
doing that.

Has this changed in the latest draft. According to my copy of
the standard (version 1998---out of date, I know), "The
operand [of the unary & operator] shall be an lvalue or a
qualified-id". His expression was &Test("test2"); IMHO, the
compiler generated a warning because it was being laxist.
[..]
There is an inherent controversy around this. Example:

struct Foo {
Foo(int) {}
Foo& that() { return *this; }
};

int main() {
Foo(42); // expression that yields an rvalue
Foo(42).that(); // expression that is an lvalue
}

How would you explain that? :-) You can't avoid being "laxist" with the
language that contains such pearls.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 18 '08 #2
Victor Bazarov wrote:
>
There is an inherent controversy around this. Example:

struct Foo {
Foo(int) {}
Foo& that() { return *this; }
};

int main() {
Foo(42); // expression that yields an rvalue
Foo(42).that(); // expression that is an lvalue
}

How would you explain that? :-) You can't avoid being "laxist" with the
language that contains such pearls.
...
There's an inherent controversy around everything, if you dig deep
enough. Your example is not really different from, say

const int* addr(const int& i) { return &i; }

int main() {
const int* p = addr(42);
}

You think this is a "pearl"? I'd say this is just a necessary evil one
has to remember about.

--
Best regards,
Andrey Tarasevich
Nov 18 '08 #3
On 2008-11-18 12:24:01 -0500, James Kanze <ja*********@gmail.comsaid:
On Nov 18, 5:50 pm, Pete Becker <p...@versatilecoding.comwrote:
>On 2008-11-18 10:52:48 -0500, Andy Gibbs <andyg1...@hotmail.co.uksaid:
>>The problem comes in the line "Function(&Test("test2"));"
with the warning "taking address of temporary". I am using
GCC 4.1.2. However, the code runs as expected with the
following output:
>>test1:
ctor
str=test1
dtor
test2:
ctor
str=test2
dtor
test3
null
>>The code above demonstrates that the Test object is not
destroyed until after Function is called, so I cannot see
why the compiler complains about this.
>The compiler warns about it because the code takes the address
of a temporary, although there's nothing inherently wrong with
doing that.

Has this changed in the latest draft. According to my copy of
the standard (version 1998---out of date, I know), "The
operand [of the unary & operator] shall be an lvalue or a
qualified-id". His expression was &Test("test2"); IMHO, the
compiler generated a warning because it was being laxist.
No, it hasn't changed. I was being lax. Thanks for pointing it out.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 18 '08 #4
On Nov 18, 6:53*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
James Kanze wrote:
[...]
The compiler warns about it because the code takes the
address of a temporary, although there's nothing inherently
wrong with doing that.
Has this changed in the latest draft. *According to my copy of
the standard (version 1998---out of date, I know), "The
operand [of the unary & operator] shall be an lvalue or a
qualified-id". *His expression was &Test("test2"); IMHO, the
compiler generated a warning because it was being laxist.
[..]
There is an inherent controversy around this. *Example:
* * struct Foo {
* * * * Foo(int) {}
* * * * Foo& that() { return *this; }
* * };
* * int main() {
* * * *Foo(42); * * * *// expression that yields an rvalue
* * * *Foo(42).that(); // expression that is an lvalue
* * }
How would you explain that? :-)
I wouldn't try to:-). But the language standard defines a
concept of lvalue and rvalue, or at least insists that they
exist, that certain expressions return lvalues, others rvalues,
and that certain operators require lvalues, others rvalues (and
that there is an lvalue to rvalue conversion---so that in
something like i=j, where both i and j have type int, there is
an implicit conversion involved). The language says that the
expression Test("test1") is an rvalue, and it says that unary &
requires an lvalue, and so be it.
You can't avoid being "laxist" with the language that contains
such pearls.
Such "pearls" are probably inevitable as soon as you try to
maintain the distinction between lvalues and rvalues, and also
have real objects. (At one point, I actually suggested that we
drop the concept of lvalue/rvalue completely, and simply state
that all temporaries were considered const. This would have
allowed things like &3, with type int const, but after all, we
allow this indirectly already, if you bind 3 to a const
reference, and then take its address.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 18 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: qazmlp | last post by:
// Test.C Line-300: namespace Line-301: { Line-302: std::vector<std::string> vecaNS ; Line-303: } The 'SUN Forte 7 C++ Compiler' reports the following warning for the above code:...
5
by: Rosa | last post by:
Hi, I'm trying to clear the TIF on Windows XP programmatically with the below code. This code works fine on any folder but the TIF. For some reason the atEnd() statements always defaults to true...
3
by: Jim | last post by:
Is it possible to read the Temporary Internet Files folder using C#? I'm messing with FileIO (newbie here) and everything seems to work fine until I try to read the list of files in this Temporary...
2
by: John Saunders | last post by:
I deploy web applications in what may be an odd manner. For every web site "x", I have an "x2" web site which points to an empty directory. I can then use Copy Project in VS.NET to deploy to the...
4
by: Nicolás Castagnet | last post by:
Hi, I write this post because I notice a strange behavior related with "Temporary Internet Files" and maybe some of you can help me to understand it. I am working in a web application with...
3
by: Juha Nieminen | last post by:
Consider this code: void foo(int& i) { i += 10; } int main() { int a = 1;
1
by: Boris | last post by:
We have some .NET 1.1 DLLs which we want to use in a ASP.NET 1.1 web page (actually one is a real .NET DLL in Managed C++ while the others are native Windows DLLs). First we copied all of the DLLs...
2
by: Ralf Kaiser | last post by:
Hi, is it possible to define another place where the "Temporary ASP.NET Files" are stored? I do not want to have them on my system partition because i have a separate partition for all the...
2
by: anon.asdf | last post by:
Hello! 1) =============================== When trying to define an array of std::string ... func( (std::string ) { std::string("ab"), std::string("cd"), std::string("ef") } , 3 ); ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.