On 1/16/2013 6:38 AM, Jarek Blakarz wrote:
>
> I thought that fun("sth") creates temporary "string" object that cannot be
> assigned to lvalue string reference.
> It turns out that I was wrong.
> Please help me understanding what is going on here and why it is correct.
You can reproduce the same behavior with
const std::string &cr = "sth";
std::string &r = "sth";
The first will compile, while the second won't.
As you correctly noted, it implicitly creates a temporary object of type
'std::string'. In C++ it has always been possible to bind 'const'
references to temporary objects, which is why the first initialization
is valid.
--
Best regards,
Andrey Tarasevioch
|