void log_and_consume(std::string&& message) { std::cout << "LOG: logging with rvalue\n"; consume(message); } void log_and_consume(std::string const& message) { std::cout << "LOG: logging with lvalue\n"; consume(message); } int main() { auto msg = std::string("sample message"); log_and_consume(msg); log_and_consume("sample message"); }
期望的输出:
LOG: logging with lvalue Consumes an lvalue LOG: logging with rvalue Consumes an rvalue
实际的输出:
LOG: logging with lvalue Consumes an lvalue LOG: logging with rvalue Consumes an lvalue
template <class... Args> void forward(Args&&... args) { f(std::forward<Args>(args)...); }
折叠规则为:
&& && -> && & && -> & & & -> & && & -> &