Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #13884 > unrolled thread

taking address of temporary object

Started by"V.Subramanian, India" <subramanian100in@yahoo.com>
First post2012-02-10 05:44 -0800
Last post2012-02-10 17:51 +0000
Articles 4 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  taking address of temporary object "V.Subramanian, India" <subramanian100in@yahoo.com> - 2012-02-10 05:44 -0800
    Re: taking address of temporary object Victor Bazarov <v.bazarov@comcast.invalid> - 2012-02-10 08:53 -0500
    Re: taking address of temporary object "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-02-10 15:08 +0100
      Re: taking address of temporary object Gareth Owen <gwowen@gmail.com> - 2012-02-10 17:51 +0000

#13884 — taking address of temporary object

From"V.Subramanian, India" <subramanian100in@yahoo.com>
Date2012-02-10 05:44 -0800
Subjecttaking address of temporary object
Message-ID<93e89895-796d-48bb-abd6-3f560410b006@tj4g2000pbc.googlegroups.com>
The following post is for learning purpose only.
I am using g++ 3.4.3
Consider the following program x.cpp

#include <cstdlib>
#include <iostream>
#include <ostream>

using namespace std;

class Test {
public:
	explicit Test(int arg = 0);
	Test& operator+=(const Test& rhs);
	int value() const;
private:
	int x;
};

inline Test::Test(int arg) : x(arg)
{
}

inline Test& Test::operator+=(const Test& rhs)
{
	x += rhs.value();
	return *this;
}

inline int Test::value() const
{
	return x;
}

inline const Test operator+(const Test& lhs,
                                        const Test& rhs)
{
	Test temp(lhs);
	return temp += rhs;
}

int main()
{
	Test x(100);
	Test y(200);
	const Test* p = &(x + y);

	int a = 10;
	int b = 20;
	const int* i = &(a + b);

	return EXIT_SUCCESS;
}

When I compile this program x.cpp as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
I get the following warning and error.

x.cpp: In function `int main()':
x.cpp:42: warning: taking address of temporary
x.cpp:46: error: non-lvalue in unary `&'
x.cpp:42: warning: unused variable 'p'
x.cpp:46: warning: unused variable 'i'

Question:
In the above code, why does the compiler give warning for
taking the address of temporary-class-object namely
(x + y) whereas it generates error for taking the address of
temporary-built-in-object namely (a + b) ? I expected error
message for taking &(x + y) similar to that of &(a + b).

Is ithis behaviour specific to g++3.4.3 ? Or the standard
itself requires so ?

Please explain.

Thanks
V.Subramanian

[toc] | [next] | [standalone]


#13885

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2012-02-10 08:53 -0500
Message-ID<jh37hl$tip$1@dont-email.me>
In reply to#13884
On 2/10/2012 8:44 AM, V.Subramanian, India wrote:
> The following post is for learning purpose only.
> I am using g++ 3.4.3
> Consider the following program x.cpp
>
> #include<cstdlib>
> #include<iostream>
> #include<ostream>
>
> using namespace std;
>
> class Test {
> public:
> 	explicit Test(int arg = 0);
> 	Test&  operator+=(const Test&  rhs);
> 	int value() const;
> private:
> 	int x;
> };
>
> inline Test::Test(int arg) : x(arg)
> {
> }
>
> inline Test&  Test::operator+=(const Test&  rhs)
> {
> 	x += rhs.value();
> 	return *this;
> }
>
> inline int Test::value() const
> {
> 	return x;
> }
>
> inline const Test operator+(const Test&  lhs,
>                                          const Test&  rhs)
> {
> 	Test temp(lhs);
> 	return temp += rhs;
> }
>
> int main()
> {
> 	Test x(100);
> 	Test y(200);
> 	const Test* p =&(x + y);
>
> 	int a = 10;
> 	int b = 20;
> 	const int* i =&(a + b);
>
> 	return EXIT_SUCCESS;
> }
>
> When I compile this program x.cpp as
> g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
> I get the following warning and error.
>
> x.cpp: In function `int main()':
> x.cpp:42: warning: taking address of temporary
> x.cpp:46: error: non-lvalue in unary `&'
> x.cpp:42: warning: unused variable 'p'
> x.cpp:46: warning: unused variable 'i'
>
> Question:
> In the above code, why does the compiler give warning for
> taking the address of temporary-class-object namely
> (x + y) whereas it generates error for taking the address of
> temporary-built-in-object namely (a + b) ? I expected error
> message for taking&(x + y) similar to that of&(a + b).
>
> Is ithis behaviour specific to g++3.4.3 ? Or the standard
> itself requires so ?
>
> Please explain.

I think the answer lies in the fact that a temporary object of a class 
type (especially one that has a c-tor) usually resides in memory anyway 
(and the compiler knows that), so taking its address is not a totally 
impossible operation, whereas a temporary of a built-in type can easily 
reside in the CPU register which does not have an address.

A class object that has any non-static member functions must have the 
unique "this" pointer when the control is inside that function, which 
means that even a temporary has to reside in memory.

V
-- 
I do not respond to top-posted replies, please don't ask

[toc] | [prev] | [next] | [standalone]


#13886

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2012-02-10 15:08 +0100
Message-ID<jh38ck$2uj$1@dont-email.me>
In reply to#13884
On 10.02.2012 14:44, V.Subramanian, India wrote:
> The following post is for learning purpose only.
> I am using g++ 3.4.3
> Consider the following program x.cpp
>
> #include<cstdlib>
> #include<iostream>
> #include<ostream>
>
> using namespace std;
>
> class Test {
> public:
> 	explicit Test(int arg = 0);
> 	Test&  operator+=(const Test&  rhs);
> 	int value() const;
> private:
> 	int x;
> };
>
> inline Test::Test(int arg) : x(arg)
> {
> }
>
> inline Test&  Test::operator+=(const Test&  rhs)
> {
> 	x += rhs.value();
> 	return *this;
> }
>
> inline int Test::value() const
> {
> 	return x;
> }
>
> inline const Test operator+(const Test&  lhs,
>                                          const Test&  rhs)
> {
> 	Test temp(lhs);
> 	return temp += rhs;
> }
>
> int main()
> {
> 	Test x(100);
> 	Test y(200);
> 	const Test* p =&(x + y);
>
> 	int a = 10;
> 	int b = 20;
> 	const int* i =&(a + b);
>
> 	return EXIT_SUCCESS;
> }

Please convert tabs to spaces before posting.


> When I compile this program x.cpp as
> g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
> I get the following warning and error.
>
> x.cpp: In function `int main()':
> x.cpp:42: warning: taking address of temporary
> x.cpp:46: error: non-lvalue in unary `&'
> x.cpp:42: warning: unused variable 'p'
> x.cpp:46: warning: unused variable 'i'

Hm, it should not compile at all. Lemme test.

<test>
[d:\dev\test]
 > g++ -std=c++98 -pedantic -Wall -Wextra foo.cpp
foo.cpp: In function 'int main()':
foo.cpp:42:25: error: taking address of temporary [-fpermissive]
foo.cpp:46:24: error: lvalue required as unary '&' operand
foo.cpp:42:14: warning: unused variable 'p' [-Wunused-variable]
foo.cpp:46:13: warning: unused variable 'i' [-Wunused-variable]

[d:\dev\test]
 > _
</test>

Solution: upgrade to more recent version of the compiler.


>
> Question:
> In the above code, why does the compiler give warning for
> taking the address of temporary-class-object namely
> (x + y) whereas it generates error for taking the address of
> temporary-built-in-object namely (a + b) ? I expected error
> message for taking&(x + y) similar to that of&(a + b).

I think it must be a very old version of g++ you're using.

The Holy Standard does not permit using the built-in address operator to 
take the address of a temporary.


> Is ithis behaviour specific to g++3.4.3 ? Or the standard
> itself requires so ?

Specific.


Cheers & hth.,

- Alf

[toc] | [prev] | [next] | [standalone]


#13887

FromGareth Owen <gwowen@gmail.com>
Date2012-02-10 17:51 +0000
Message-ID<87zkcqtw41.fsf@gmail.com>
In reply to#13886
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> writes:

> The Holy Standard does not permit using the built-in address operator
> to take the address of a temporary.

Presumably you can call an over-ridden unary & operator?
(which isn't to say that to do so isn't a horrible design decision)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web