J
JKop
Your program begins at:
int main(void);
Now, in main, you want to call a function. This particular function you want
to call defines a local object of type Taste, and then returns this local
object by value.
In main, having called this function, what you want to do is bind a
reference to the object returned from Taste and then continue on and use it
just as if it were defined in main as a local object.
The first restriction here is that the Standard declares that you can bind
the object returned from a function only to a *const* reference.
Altogether, you want just 1 object to be created. No temporaries. No copies.
But... in actual fact you're going to have 3 objects:
1: The local variable defined in the function
2: The temporary returned
3: You'll have to create another object in main and copy the temporary so as
to alleviate the constness.
I wrote the following code a few mins ago and it compiles and runs grand.
I've tried my best to make the code as non-tedious as possible to read, so
please just give it a glance over:
#include <iostream>
class Taste
{
public:
static unsigned char amount_ever;
static unsigned char amount_currently;
int mouth;
unsigned char age;
void Swallow(void)
{
mouth += 2;
}
Taste(void) : mouth(7), age(14)
{
++amount_ever;
++amount_currently;
}
Taste(Taste& original)
{
mouth = original.mouth;
age = original.age;
++amount_ever;
++amount_currently;
}
~Taste(void)
{
--amount_currently;
}
};
unsigned char Taste::amount_ever = 0;
unsigned char Taste::amount_currently = 0;
Taste Powder(void)
{
Taste cream;
cream.age = 4;
cream.Swallow();
return cream;
}
int main(void)
{
const Taste& yacht = Powder();
Taste& boat = const_cast<Taste&>(yacht);
//Now we have what we wanted, a local variable in main.
boat.age = 12;
boat.Swallow();
std::cout << "Amount ever: " << (int)Taste::amount_ever << std::endl
<< "Amount currently: " << (int)Taste::amount_currently
<< std::endl;
std::system("PAUSE");
}
Now first thing's first:
I'm going to presume that casting away that constness and editing the object
is undefined behaviour.
Can anyone come up with any argument as to why the hell you've to bind to a
*const* reference in the first place?! I'll wait for an answer to this
question before I decide for myself if casting away the constness is
"moral".
Moving on:
With the above code, by casting away the constness, I was aiming for
amount_ever == 2. To my delight, it came up 1!!
Obviously, *my* compiler has not created a temporary, it has returned the
actual local object defined in the function. Which leads me to...
Why the hell is this "optimization" compiler behaviour, as opposed to
"run-of-the-mill" compiler behaviour?!! Can anyone supply me with any
arguments as to why a function should copy the local variable and then
return the copy, as opposed to just returning the local variable itself?!
Here's my thoughts:
My objective is very clear. In my mind, I know and trully believe that I
should be able to do what I want to do with just the 1 object, as opposed to
3! Without regard to the Standard, I believe that C++ as an excellent
programming language should be able to achieve this, that it should be "run-
of-the-mill" compiler behaviour, as opposed to "optimization" compiler
behaviour.
-JKop
int main(void);
Now, in main, you want to call a function. This particular function you want
to call defines a local object of type Taste, and then returns this local
object by value.
In main, having called this function, what you want to do is bind a
reference to the object returned from Taste and then continue on and use it
just as if it were defined in main as a local object.
The first restriction here is that the Standard declares that you can bind
the object returned from a function only to a *const* reference.
Altogether, you want just 1 object to be created. No temporaries. No copies.
But... in actual fact you're going to have 3 objects:
1: The local variable defined in the function
2: The temporary returned
3: You'll have to create another object in main and copy the temporary so as
to alleviate the constness.
I wrote the following code a few mins ago and it compiles and runs grand.
I've tried my best to make the code as non-tedious as possible to read, so
please just give it a glance over:
#include <iostream>
class Taste
{
public:
static unsigned char amount_ever;
static unsigned char amount_currently;
int mouth;
unsigned char age;
void Swallow(void)
{
mouth += 2;
}
Taste(void) : mouth(7), age(14)
{
++amount_ever;
++amount_currently;
}
Taste(Taste& original)
{
mouth = original.mouth;
age = original.age;
++amount_ever;
++amount_currently;
}
~Taste(void)
{
--amount_currently;
}
};
unsigned char Taste::amount_ever = 0;
unsigned char Taste::amount_currently = 0;
Taste Powder(void)
{
Taste cream;
cream.age = 4;
cream.Swallow();
return cream;
}
int main(void)
{
const Taste& yacht = Powder();
Taste& boat = const_cast<Taste&>(yacht);
//Now we have what we wanted, a local variable in main.
boat.age = 12;
boat.Swallow();
std::cout << "Amount ever: " << (int)Taste::amount_ever << std::endl
<< "Amount currently: " << (int)Taste::amount_currently
<< std::endl;
std::system("PAUSE");
}
Now first thing's first:
I'm going to presume that casting away that constness and editing the object
is undefined behaviour.
Can anyone come up with any argument as to why the hell you've to bind to a
*const* reference in the first place?! I'll wait for an answer to this
question before I decide for myself if casting away the constness is
"moral".
Moving on:
With the above code, by casting away the constness, I was aiming for
amount_ever == 2. To my delight, it came up 1!!
Obviously, *my* compiler has not created a temporary, it has returned the
actual local object defined in the function. Which leads me to...
Why the hell is this "optimization" compiler behaviour, as opposed to
"run-of-the-mill" compiler behaviour?!! Can anyone supply me with any
arguments as to why a function should copy the local variable and then
return the copy, as opposed to just returning the local variable itself?!
Here's my thoughts:
My objective is very clear. In my mind, I know and trully believe that I
should be able to do what I want to do with just the 1 object, as opposed to
3! Without regard to the Standard, I believe that C++ as an excellent
programming language should be able to achieve this, that it should be "run-
of-the-mill" compiler behaviour, as opposed to "optimization" compiler
behaviour.
-JKop