Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > r-value references not working in MSVC (VS2010) ?

Reply
Thread Tools

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

 
 
ctgqumgf@sharklasers.com
Guest
Posts: n/a
 
      10-11-2012
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!");
}
 
Reply With Quote
 
 
 
 
Stefan van Kessel
Guest
Posts: n/a
 
      10-11-2012
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).
 
Reply With Quote
 
 
 
 
ctgqumgf@sharklasers.com
Guest
Posts: n/a
 
      10-11-2012
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 ...
 
Reply With Quote
 
ralph
Guest
Posts: n/a
 
      10-11-2012
On Thu, 11 Oct 2012 11:23:53 -0700 (PDT),
wrote:

>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>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Snake references just as ok as Monty Python jokes/references in python community? :) seberino@spawar.navy.mil Python 8 12-12-2006 11:21 PM
Typedef A references struct B which references struct A which... DanielEKFA C++ 8 05-16-2005 10:26 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
how to understand references to variables and references to constants are distinguished? baumann.Pan@gmail.com C++ 3 11-10-2004 04:16 AM
Pointers and References (and References to Pointers) Roger Leigh C++ 8 11-17-2003 10:14 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57