std::forward和std::move

std :: forward Perfect forward

To ensure that the parameters of the original property (in the parameter template is the time quoted): Left reference value remains left after the value of the property is transferred, the right reference value remains the right value of the property after being forwarded

void show(std::string& str) {
    std::cout<<"lvalue:"<<str<<std::endl;
}

void show(std::string&& str) {
    std::cout<<"rvalue:"<<str<<std::endl;
}


template<typename T>
void ShowWrapper(T&& t) {
    show(std::forward<T>(t));
}

void TestForward() {
    std::string str("left");
    ShowWrapper(str);//lvalue:left
    ShowWrapper("right"); //rvalue:right
}

std::move

The value of the left into the right value

void TestMove() {
    std::string str("move");
    ShowWrapper(str);//rvalue:move
}

Guess you like

Origin www.cnblogs.com/smallredness/p/11085369.html