I am sorry if this question was asked before or I am missing something trivial, however the following for references is not clear to me. I understand why passing a rvalue instead of lvalue is allowed when the function parameter is not a reference, however I do not understand the reason it is allowed when the function parameter is a const reference (which doesn't make sense to me) but forbidden when passing a usual reference (the logical behavior).
Suppose that I have the following code
struct A
{
explicit A(std::string s) : name{ s } { };
A(const A& a) : name{ a.name } { }
A& operator=(const A& a) { name = a.name; return *this; }
A(A&& a) noexcept : name{} { std::swap(name, a.name); }
A& operator= (A&& a) noexcept { std::swap(name, a.name); return *this; }
void talk() const{ std::cout << name << " says blablabla.\n"; }
private:
std::string name;
};
void f(A a) {}
void g(A& a) {}
void h(const A& a) {}
int main()
{
A a{ "a" };
f(a);
f(A{ "temp" });
g(a);
g(A{ "temp" }); // Compile error
h(a);
h(A{ "temp" });
}
Now, I understand why there implicitly generated overload for A&& for f, however the behavior for A& and const A& is confusing me.
- Why does the compiler forbid passing an A&& instead of A& but allows passing an A&& instead of A&?
- Is this a bug or a feature?
- In case it is a feature, what is the reason for allowing it?
Thanks.
EDIT: The suggested question is not exactly what I asked. My question was, why does it allow a binding of temporary object even when it is const. The linked question asks "why does it forbid when it is not const".
Accepted answer: The answer provided by eerorika in comments to his question makes most sense, which is backwards compability with pre c++11.