#include <iostream> # include <set> #include <chrono> #ifndef UNUSED #define UNUSED(v) (void)(v) #endif typedef std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> time_point; //void logg(const time_point& tp, std::string& name) void logg(const time_point& tp, const std::string& name) { UNUSED(tp); UNUSED(name); std::cout << " name: " << name << std::endl; } void log_add(const std::string& name, std::multiset<std::string>& names) { auto now = std::chrono::system_clock::now(); std::string temp("abc"); logg(now, std::string("xxx")); logg(now, "xxx"); logg(now, temp); names.emplace(name); } template <typename T> void log_add_T(T&& name, std::multiset<std::string>& names) { auto now = std::chrono::system_clock::now(); logg(now, "xxx"); names.emplace(std::forward<T>(name)); } int main(int argc, char *argv[]) { std::multiset<std::string> nameset; log_add("abcd", nameset); log_add_T("aaa", nameset); return 0; }
1. 如果采用logg函数的形式为 void logg(const time_point& tp, std::string& name), 则
logg(now, std::string("xxx"));
logg(now, "xxx");
都会编译失败。
2. log_add_T函数采用万能引用参数,利用完美转发,效率比log_add高!
原文地址:https://www.cnblogs.com/kex1n/p/11839232.html
时间: 2024-10-19 09:03:33