Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   r-value references not working in MSVC (VS2010) ? (http://www.velocityreviews.com/forums/t953262-r-value-references-not-working-in-msvc-vs2010.html)

ctgqumgf@sharklasers.com 10-11-2012 04:47 PM

r-value references not working in MSVC (VS2010) ?
 
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!");
}

Stefan van Kessel 10-11-2012 05:19 PM

Re: r-value references not working in MSVC (VS2010) ?
 
On 2012-10-11 6:47 PM, ctgqumgf@sharklasers.com 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).

ctgqumgf@sharklasers.com 10-11-2012 06:23 PM

Re: r-value references not working in MSVC (VS2010) ?
 
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 ...

ralph 10-11-2012 07:18 PM

Re: r-value references not working in MSVC (VS2010) ?
 
On Thu, 11 Oct 2012 11:23:53 -0700 (PDT), ctgqumgf@sharklasers.com
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>




All times are GMT. The time now is 10:22 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.