473,769 Members | 3,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

temporaries

Hello all,
Please consider this code :

class X {
int x;
public:
X(int p ) : x(p) { }
operator int() { x = 1; return x; }
};

X foo(int i)
{
return X(i);
}
int main()
{
int u = 100;
int p = foo(u);
}

What I understand here (Please correct if I am wrong) :
The function foo takes an integer and returns a value of type X (this
is 'return by value'). The return value of foo is stored in some
temporary. This value is then converted to an integer, using operator
int() and then assignment takes place.

What I donot understand is that :
temporaries are always constant. On the other hand, operator int() is
not a const function (rather, it cannot be a const function, it
modifies the data member x). Connsequently, this code should fail to
compile. (How can operator int() be called on a const object ? )

But thats not the case. This is a valid C++ code.
Any explanation will be useful. Thanks.

Nov 22 '05 #1
1 1410
Neelesh Bodas wrote:
Hello all,
Please consider this code :

class X {
int x;
public:
X(int p ) : x(p) { }
operator int() { x = 1; return x; }
};

X foo(int i)
{
return X(i);
}
int main()
{
int u = 100;
int p = foo(u);
}

What I understand here (Please correct if I am wrong) :
The function foo takes an integer and returns a value of type X (this
is 'return by value'). The return value of foo is stored in some
temporary. This value is then converted to an integer, using operator
int() and then assignment takes place.
The return value may be stored in a temporary. But since foo always
returns the same object, the compiler may optimize the temporary away.
But doing so would not affect the correctness or the outward behavior
of the program.
What I donot understand is that :
temporaries are always constant. On the other hand, operator int() is
not a const function (rather, it cannot be a const function, it
modifies the data member x). Connsequently, this code should fail to
compile. (How can operator int() be called on a const object ? )
X's operator() int can be called on a non-const object because it is a
non-const method - which is the only requirement that must be met to
invoke operator int(). There is no rule that states that temporaries
must be constant. True, the compiler will not bind a temporary to a
reference passed as a parameter unless the reference is declared const;
but that rule is not at all the equivalent to a rule that temporaries
themselves must always be const. And in fact there is no such rule.
But thats not the case. This is a valid C++ code.


There is no obvious reason to expect this program not to work.

Greg

Nov 22 '05 #2

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

Similar topics

1
1692
by: kaede | last post by:
Hi all, I would like to know if the following code is valid and not ill-formed. Data getData() { return Data(1, 2, 3); }
13
1868
by: John Harrison | last post by:
Rather a long program I'm afraid but I don't think I can cut it down any further. What I'm trying to do is construct a complex object Y from several X objects in a complex expression. I'm trying to do this without creating any temporaries of Y. To do that I'm defined a number of proxy classes which contain references to the arguments in the expression. All the proxies define a conversion operator to Y which will ultimately be used to...
10
1553
by: JKop | last post by:
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;
5
1536
by: Dave | last post by:
Hello all, I've been wondering... Why is it that a reference may be bound only to a const object? If a reference were bound to a non-const object and that object were modified, what harm could result? A temporary is just as real of an object as any other. It lacks a name, but that doesn't make it less real. Class (no pun intended) warfare seems to be alive and well! Thanks, Dave
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; }
7
1692
by: REH | last post by:
Though unsafe, is this legal: const int& foo(const int& i) { return i; } int j = foo(5); does the temporary live long enough to be assigned to j?
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
1555
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(); // ?? ....... }
13
3985
by: dragoncoder | last post by:
Hi everyone, please consider the following function:- const int& foo ( const double& d ) { return d; } g++ compiles it with warnings and solaris CC gives error. I want to know if the code is correct according to the standard ?
0
10049
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...
1
9997
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
9865
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
8873
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
7413
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
5310
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...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
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
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.