第7章 按值传递或按引用传递:7.3 使用std::ref()和std::cref()
7.3 Using std::ref() and std::cref() 7.3 使用std::ref()和std::cref() Since C++11, you can let the caller decide, for a function template argument, whether to pass it by value or by reference. When a template is declared to take arguments by value, the caller can use std::cref() and std::ref(), declared in header file <functional>, to pass the argument by reference. For example: 从C++11开始,对于函数模板参数,你可以让调用者自己决定是按值还是按引用来传递。当模板被声明按值传递时,调用者可以使用std::cref()和std::ref()(声明在<functional>头文件中)将参数按引用传递给函数模板。例如: template<typename T> void printT (T arg) { … } std:: string s = " hello " ; printT(s); // 按值传递s