This program:
#include <iostream> struct T { T() {} T(const T &) { std::cout << "copy constructor "; } T(T &&) { std::cout << "move constructor "; } }; int main() { ([](T t) -> T { return t; })({}); std::cout << '\n'; ([](T t) -> T { return void(), t; })({}); std::cout << '\n'; ([](T t) -> T { return void(), std::move(t); })({}); std::cout << '\n'; }
when compiling with gcc-4.7.1 outputs ( link ):
move constructor copy constructor move constructor
Why does the comma operator have this effect? The standard says:
5.18 Comma operator [expr.comma]
1 - [...] The type and value of the result is the type and value of the right operand; the result has the same category of values โโas its right operand [...]. If the value of the correct operand is temporary, the result is temporary.
Am I missing something that allows the comma operator to influence the semantics of the program, or is it a bug in gcc?
c ++ c ++ 11 move-semantics return comma-operator
ecatmur Sep 05 '12 at 19:05 2012-09-05 19:05
source share