473,756 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Problem "taking address of temporary"

On Nov 18, 5:50 pm, Pete Becker <p...@versatile coding.comwrote :
On 2008-11-18 10:52:48 -0500, Andy Gibbs <andyg1...@hotm ail.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.c c", line 34: Warning, badargtypel2w: String literal
converted to char* in formal argument str in call to Test::Test
(char*).
"addrtemp.c c", line 37: Warning, badargtypel2w: String literal
converted to char* in formal argument str in call to Test::Test
(char*).
"addrtemp.c c", 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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 18 '08 #1
4 7121
James Kanze wrote:
On Nov 18, 5:50 pm, Pete Becker <p...@versatile coding.comwrote :
>On 2008-11-18 10:52:48 -0500, Andy Gibbs <andyg1...@hotm ail.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*********@gm ail.comsaid:
On Nov 18, 5:50 pm, Pete Becker <p...@versatile coding.comwrote :
>On 2008-11-18 10:52:48 -0500, Andy Gibbs <andyg1...@hotm ail.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...@com Acast.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 objektorientier ter Datenverarbeitu ng
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
3510
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: "/advantage/hlri_tools/sol/SUNWspro/prod/include/CC/Cstd/./vector", line 318: Warning: should not initialize a non-const reference with a
5
16829
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 and no files are deleted in the folder. The peculiarity of this issue is that the files/subfolders cannot be seen through the windows explorer either. I can only access/delete them through a command shell. Any ideas?
3
6129
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 Internet Files folder. I'm only received 1 file when I know there is more. Any suggestions are very appreciated. Thanks
2
9096
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 empty directory, and have the x2 site QA'd. I then switch over by creating a new empty directory for the next time, and in IIS, pointing the x2 site to the new directory and finally pointing the "x" site to the newly-deployed directory. This...
4
2346
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 ASP.NET. Recently, I group of user have problems with it because the values of the sessions were not stored correctly (the application save the username in a login page, then other page try to get it and the result was always ""). We restart the web...
3
2269
by: Juha Nieminen | last post by:
Consider this code: void foo(int& i) { i += 10; } int main() { int a = 1;
1
2165
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 to /bin. However when we do this we get a configuration error. When we copy the DLLs to "/Temporary ASP.NET Files/..." everything works. Does anyone know what could be the reason that the DLLs can be loaded from the temporary files but not from...
2
2053
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 temporary stuff on a separate physical drive. Is there a registry entry that defines the place for that folder?
2
2372
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 ); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ....g++ tells me: "invalid use of non-lvalue array"
0
9462
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9287
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10046
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6542
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.