473,785 Members | 2,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Temporaries, non-const yet r-value?!

Firstly, we all know that temporaries are non-const, which I shall
demonstrate with the following code:

struct Blah
{
int monkey;

void SetMonkey(int const supplied)
{
monkey = supplied;
}
};
Blah SomeFunc()
{
return Blah();
}
int main()
{
Blah().SetMonke y(5);

SomeFunc().monk ey = 5;
}
But, at the same time, temporaries are "r-value"'s. The following is
illegal:

int Blah()
{
return 5;
}

int main
{
Blah() = 6;
}
Also, in the previous code, "Blah().mon key = 5" is illegal.
It's not legal to bind a temporary to a non-const reference, because it is
an "r-value".
You're allowed to use "const_cast " to cast away the constness if the
original object was in fact non-const...

struct Blah
{
int a;
double b;
char c;
float** p_p_f;
wchar_t k;
bool f;
};

int main()
{
Blah const &poo = Blah();

Blah &cow = const_cast<Blah &>(poo);

cow.a = 5;
}

Any thoughts on this?
-JKop
Jul 22 '05 #1
10 1554
* JKop:

Any thoughts on this?


It's a correct observation. And yes it's problematic. The Mojo article
that (now long ago, measured in software development time) started the
current debate:
<url: http://www.cuj.com/documents/s=8246/cujcexp2102alex andr/alexandr.htm>.

And recently in this forum Chris Theis raised the question of what's
happened since then.

That's a new thread NRVO in [comp.std.c++], but very little concrete
has been forthcoming.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
JKop wrote in news:wL******** ***********@new s.indigo.ie in comp.lang.c++:
You're allowed to use "const_cast " to cast away the constness if the
original object was in fact non-const...

Thats correct, but ...
struct Blah
{ };

int main()
{
Blah const &poo = Blah();
The problem is here, the rvalue is allowed to bind via a temporary
and although we all know that the temp will in reality (*) be no
more constant than the rvalue, the standard allows it to be.

*) Here: reality == Desktop PC's

So the actual object "poo" refers to is a real constant.

Blah &cow = const_cast<Blah &>(poo);
That is OK, but ...

cow.a = 5;
That is UB.
}


Microsofts VC++ has a mode where it check's (at runtime) for
buffer overflows, such a system could also be used to check for
the constantness of the temp "poo" was bound too, and your code
nolonger works.

Also in this case, since the "Blah" that was used is a value
initialized POD, the constant could actualy be in read-only
memory, bang goes your code again (or maybe it doesn't compile).

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
The problem is here, the rvalue is allowed to bind via a temporary
and although we all know that the temp will in reality (*) be no
more constant than the rvalue, the standard allows it to be.
No it does not. Temporaries are not necessarily const. A temporary
of class type can be modified.

That is OK, but ...

cow.a = 5;

That is UB.

No.
Also in this case, since the "Blah" that was used is a value
initialized POD, the constant could actualy be in read-only
memory, bang goes your code again (or maybe it doesn't compile).


No.
Jul 22 '05 #4
Ron Natalie wrote in news:41******** *************** @news.newshosti ng.com in
comp.lang.c++:
The problem is here, the rvalue is allowed to bind via a temporary
and although we all know that the temp will in reality (*) be no
more constant than the rvalue, the standard allows it to be.
No it does not.


Your right it doesn't allow it, it *requires* it:

8.5.3/5:

The relevent bit:

— A temporary of type “cv1 T2” [sic] is created, and a constructor is
called to copy the entire rvalue object into the temporary. The
reference is bound to the temporary or to a sub-object within the
temporary.93)
Temporaries are not necessarily const. A temporary
of class type can be modified.
No. No.


Yes, Yes.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #5
Rob Williscroft wrote:
Ron Natalie wrote in news:41******** *************** @news.newshosti ng.com in
comp.lang.c++:

The problem is here, the rvalue is allowed to bind via a temporary
and although we all know that the temp will in reality (*) be no
more constant than the rvalue, the standard allows it to be.
No it does not.

Your right it doesn't allow it, it *requires* it:


Did you miss the "one of the following ways (the choice is implementation-
defined" a few lines above the quoted section?

8.5.3/5:

The relevent bit:

— A temporary of type “cv1 T2” [sic] is created, and a constructor is
called to copy the entire rvalue object into the temporary. The
reference is bound to the temporary or to a sub-object within the
temporary.93)

Temporaries are not necessarily const. A temporary
of class type can be modified.

No.


No.

Yes, Yes.


NO NO

Rob.


Victor
Jul 22 '05 #6
Victor Bazarov wrote in
news:ay******** ********@newsre ad1.dllstx09.us .to.verio.net in
comp.lang.c++:
The problem is here, the rvalue is allowed to bind via a temporary
and although we all know that the temp will in reality (*) be no
more constant than the rvalue, the standard allows it to be.

No it does not.

Your right it doesn't allow it, it *requires* it:


Did you miss the "one of the following ways (the choice is
implementation- defined" a few lines above the quoted section?


Thats " ... the rvalue is alowed to bind via a temporary ... " as I
said above, but if it does the temporary is *required* to be const.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #7
Rob Williscroft wrote:
Victor Bazarov wrote in
news:ay******** ********@newsre ad1.dllstx09.us .to.verio.net in
comp.lang.c++:

>The problem is here, the rvalue is allowed to bind via a temporary
>and although we all know that the temp will in reality (*) be no
>more constant than the rvalue, the standard allows it to be.

No it does not.
Your right it doesn't allow it, it *requires* it:


Did you miss the "one of the following ways (the choice is
implementatio n- defined" a few lines above the quoted section?

Thats " ... the rvalue is alowed to bind via a temporary ... " as I
said above, but if it does the temporary is *required* to be const.


Ah, sorry, I didn't understand what you were hinting at. Yes, if the
temporary _is_ created, that temporary will be const. It doesn't have
to be created, though.

So, using the original example

Blah const &poo = Blah();
Blah &cow = const_cast<Blah &>(poo);
cow.a = 5;

the behaviour is NOT undefined, but rather _implementation _defined_. That
is because the 'poo' reference _may_ bind to the original temporary and
not to an intermediate one.

Victor
Jul 22 '05 #8
Victor Bazarov wrote in
news:iT******** ********@newsre ad1.dllstx09.us .to.verio.net in
comp.lang.c++:
Thats " ... the rvalue is alowed to bind via a temporary ... " as I
said above, but if it does the temporary is *required* to be const.


Ah, sorry, I didn't understand what you were hinting at. Yes, if the
temporary _is_ created, that temporary will be const. It doesn't have
to be created, though.

So, using the original example

Blah const &poo = Blah();
Blah &cow = const_cast<Blah &>(poo);
cow.a = 5;

the behaviour is NOT undefined, but rather _implementation _defined_.
That is because the 'poo' reference _may_ bind to the original
temporary and not to an intermediate one.


Yes, but what is implementation defined here is wether UB happens
or not. IMO that is UB (the same as n * 0 == 0 for any n). YMMV.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #9

"Rob Williscroft" <rt*@freenet.co .uk> wrote in message
news:Xn******** *************** ***********@130 .133.1.4...
Victor Bazarov wrote in
news:iT******** ********@newsre ad1.dllstx09.us .to.verio.net in
comp.lang.c++:
Thats " ... the rvalue is alowed to bind via a temporary ... " as I
said above, but if it does the temporary is *required* to be const.


Ah, sorry, I didn't understand what you were hinting at. Yes, if the
temporary _is_ created, that temporary will be const. It doesn't have
to be created, though.

So, using the original example

Blah const &poo = Blah();
Blah &cow = const_cast<Blah &>(poo);
cow.a = 5;

the behaviour is NOT undefined, but rather _implementation _defined_.
That is because the 'poo' reference _may_ bind to the original
temporary and not to an intermediate one.


Yes, but what is implementation defined here is wether UB happens
or not. IMO that is UB (the same as n * 0 == 0 for any n). YMMV.


But is UB * 0 always == 0 ???

-:)

-Mike

Jul 22 '05 #10

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

Similar topics

11
2080
by: JKop | last post by:
The following compiles: // syrup.cpp struct DoubleInDisguise { double data; };
13
1661
by: S.Tobias | last post by:
I'm examining the existence of temporary objects by looking at their addresses. The trick is to create a structure that contains an array as its first member. In an expression the array rvalue is converted to a pointer to its first member. Since this address is also the address of the array, and that is the address of the structure, I conclude that this is also the address of the temporary storage for the structure (r)value. I'm...
10
1540
by: ATASLO | last post by:
In the following example, section #3 fails under VC98, VC2003, VC2005 Express Beta (Aug 2004) and g++ 3.3.2. Is this just a pitfall of the C++ specification? Why don't any of the above compilers at least flag this as a warning as they would when say trying to return a const & to a local? In Section #2, the const B& Bref is initialized and bound to the temporary returned from GetSettings(). That is the temporary B exists until Bref goes...
3
1653
by: bb | last post by:
Hi, Have a query regarding the life of temporaries. Here is the code... class MyNumber { public: MyNumber(int n) : n(n) { cout << "Object Constructed." << endl; }
12
2594
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned by value temporaries are created?(I'm not touching yet the cases where standard allows optimizations from the side of implementations to avoid copying) If so, please quote part of the standard that says that. Assuming it is true, I can imagine two...
6
1556
by: wizwx | last post by:
Is there anything wrong with the following code? class A { ... }; class B : public A { ... }; // definitions of class A and B, these are OK Foo() { A & a = B(); // ?? A * p = &B(); // ?? ....... }
0
1073
by: Victor Bazarov | last post by:
aitorf666@gmail.com wrote: My bet is that it's not going to happen. Too late. Many compilers have already implemented the && syntax. Besides, would you allow the keyword 'mutable' to be used in the context of declaring a pointer to something? What if you want to declare a mutable data member that is itself an r-value reference? "mutable mutable"? V --
0
1100
by: Jerry Coffin | last post by:
In article <9f60e411-a5b1-4571-9d3d-005432378cd4@ 56g2000hsm.googlegroups.com>, aitorf666@gmail.com says... That's not the real reason for rvalue references. There are two primary reasons for rvalue references: providing move semantics, and providing forwarding semantics. The ability to modify the original object with impunity is the _mechanism_ behind the move semantics. Looking at this as 'mutable', however is looking at how you do...
9
3339
by: usao | last post by:
Does anyone have an example of how to create a class which describes and array, such that I can use the subscript operator on both the left and right side of an assignment statement? This is as close as I can get, but I feel it's kludgey and slow... #include <iostream.h> template<class T> class foo { T *data;
0
9643
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
10319
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...
1
10087
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5380
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
4046
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
3
2877
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.