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

Discussion in 'C++' started by ctgqumgf@sharklasers.com, Oct 11, 2012.

  1. Guest

    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!");
    }
     
    , Oct 11, 2012
    #1
    1. Advertising

  2. 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).
     
    Stefan van Kessel, Oct 11, 2012
    #2
    1. Advertising

  3. Guest

    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 ...
     
    , Oct 11, 2012
    #3
  4. ralph Guest

    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>
     
    ralph, Oct 11, 2012
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexey Smirnov
    Replies:
    2
    Views:
    1,369
    Alexey Smirnov
    Dec 17, 2009
  2. Alexey Smirnov
    Replies:
    1
    Views:
    556
    Alvin Bruney - ASP.NET MVP
    Feb 14, 2010
  3. Nick Gilbert
    Replies:
    2
    Views:
    1,705
    Nick Gilbert
    Apr 21, 2010
  4. gerry
    Replies:
    3
    Views:
    717
    Alexey Smirnov
    Apr 21, 2010
  5. Cal Who
    Replies:
    2
    Views:
    352
    Cal Who
    Apr 26, 2010
Loading...

Share This Page