I have seen the related questions and they mostly talk about if we should have const rvalue references as a parameter or not. But I still fail to reason why a non-const move constructor is being called in the following code:
#include <iostream>
using namespace std;
class A
{
public:
A (int const &&i) { cout << "const rvalue constructor"; }
A (int &&i) { cout << "non const rvalue constructor"; }
};
int const foo (void)
{
const int i = 3;
return i;
}
int main (void)
{
A a(foo());
}
const
value from a function is really quite meaningless. Nothing will stop the caller from storing the value in a non-const variable anyway. – Arkconst int
rvalue. – Sedberry