On 2012-10-11 6:47 PM,
wrote:
> Compiling the code below with g++ 4.7.0 produces expected
> output
> [&] Hello!
> [&&] Hello!
>
> Whereas MSVC 16.00.x produces output
> [&] Hello!
> [&] Hello!
>
> Does MSVC not fully understand r-value references yet?
>
> #include <iostream>
> #include <string>
>
> void print(const std::string& s) {
> std::cout << "[&] " << s << std::endl;
> }
>
> void print(std::string&& s) {
> std::cout << "[&&] " << s << std::endl;
> }
>
> int main() {
> std::string s = "Hello!";
> print(s);
> print("Hello!");
> }
Yes, MSVC 16 (VS2010) apparently doesn't bind literals to an rvalue
reference there. With
print(std::string("Hello!"));
it does call use the version of print taking an rvalue reference.
There's a detailed explanation on
http://msdn.microsoft.com/en-us/library/hh567368.aspx
under Rvalue References.
"[...] The rvalue references v2.0 rules said, string&& cannot bind to
"strval" because "strval" is an lvalue, and therefore, push_back(const
string&) is the only viable overload. [...]"
It works in MSVC 17 (VS2012).