struct S{
   int i;
   double d;
   S(const S & incoming_S):i(S.i),d(S.d){}
};
void test(){
   S myS=S(      /*temporary variable*/ S(23,3.14)          );
}

Questions:
1) During initialisation, is the temporary variable S(23,3.14) actually created or can it (and is it actually for typical compilers such as VS2008) optimised away?
2) Same question for placement new situation such as:

void test{
   char buffer[sizeof(S)];
   new buffer(0) S(   /*temporary variable */   S(23,3.14)                   );
}

Well, it is certainly useless to do it this way, but if you do, then yes, the compiler will optimize this away. If it doesn't, then it is a very pessimizing compiler (pessimizing = anti-optimizing). All modern compilers do this.

In general, the rule of optimizations is the "as if" rule, that is, the optimized code should behave as if it was the verbatim translation of the original code (i.e. not optimized in any way). However, there are two notable exceptions to this rule, that is, elision of temporaries and return-value optimization. The former means that if a temporary is created and immediately copied into another variable, then it can sometimes be optimized away (but it depends on the context). The latter means that the value that is returned from a function can be created in-place of the variable it is going to get copied to (again, under some circumstances).

Because, in C++, you can execute code during the constructors/destructor of an object, it means that optimizing away the creation of a temporary object technically breaks the "as if" rule. But, because those optimizations are so important for performance, the C++ standard allows it, and all decent compilers take advantage of this permission. So, this is something to keep in mind when writing constructors/destructor code, that is, you can't really predict how many times they will be called, all you can rely on is that anything that is created is eventually destroyed (i.e. every constructor call will eventually lead to a destructor call for the same object).

Also note that C++0x (or C++11), the new standard for C++, introduces the concept of rvalue-references which allows you to specifically write optimized code for when temporary objects are involved (the standard technical term for a temporary variable is an "rvalue", because they can only appear on the right-side of an assignment).

Edited 5 Years Ago by mike_2000_17: n/a

Thank you for this excellent reply.

Regarding "Also note that C++0x (or C++11), the new standard for C++, introduces the concept of rvalue-references which allows you to specifically write optimized code for when temporary objects are involved (the standard technical term for a temporary variable is an "rvalue", because they can only appear on the right-side of an assignment). "

Where can I get more detailed information on this subject ?

>>Where can I get more detailed information on this subject ?

This article is pretty good on rvalue-refs, its minimalistic, but it's all you really need to know.

Otherwise, for general C++11 info, just use the wiki (rvalue-ref is the first section).

This question has already been answered. Start a new discussion instead.