r-value references not working in MSVC (VS2010) ?

C

ctgqumgf

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!");
}
 
S

Stefan van Kessel

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).
 
C

ctgqumgf

Thanks for the reply. I too played around a bit and found using "int" instead of "std::string" working. Seems like MSVC isn't the best choice for learning C++11 ...
 
R

ralph

Thanks for the reply. I too played around a bit and found using "int"
instead of "std::string" working. Seems like MSVC isn't the best
choice for learning C++11 ...

If that is your intention, to learn C++11, then you are definitely
correct - IT IS NOT the compiler of choice. <g>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top