If your compiler allows this, then it's not a standard compatible C++ compiler. You can not bind a temporary rvalue to a non-const lvalue reference. It's the rule. Both clang and gcc don't compile that code for func2(STRUCT(65));
.
Instead you have alternatives:
void func1(int&& i){}
void func1(const int& i){}
Legacy from C++03: A (lvalue) reference to a non-const type (int &i
) supposed to able to change the parameter then passing a temporary object such as 56
is not logical because it not changeable. A reference to a const type (const int &i
) supposed to just observe the value as read-only, then passing a temporary value such as 52
is legal.
In C++11 you can reference to a non-const temporary object by &&
.