C + + Primer notes--Understanding Std::move

Source: Internet
Author: User

The standard library move function is a good example of a template that uses rvalue references. The standard library defines std::move as follows:

Template <typename t>typename remove_reference<T>::type&& Move (t&& T) {     return static_cast<typename remove_reference<t>::type&&>(T);}

We consider the following code work process:

std::string S1 ("hi"= Std::move (string("hi "));    // correct, move data from a right value s2 = std::move (S1);                // correct, but after the assignment, the value of the S1 is indeterminate.


In the first assignment, the argument is the right value of type string, so the procedure is:

    • Inferred T is of type string
    • The type member of the remove_reference<string> is a string
    • The move return type is string&&
    • The type of the function parameter T of move is string&&

Thus, this invocation instantiates Move<string>, which is the function

string&& Move (string &&t)


In the second assignment, the argument is an lvalue, so:

    • Infer that the type of T is string&
    • The type member of the remove_reference<string&> is a string
    • The move return type is string&&
    • The type of the function parameter T of move is string& &&, which is folded into string&

Thus, this invocation instantiates move<string&>

string&& Move (string &t)

Typically, static_cast can only be used for other legitimate type conversions. But there is a concession rule for rvalue: although it is not possible to implicitly convert an lvalue to an rvalue reference, we can convert an lvalue to an rvalue using the static_cast display.

C + + Primer notes--Understanding Std::move

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.