C++ 带可变模板函数错误的Std::async
下面是我试图运行的代码:C++ 带可变模板函数错误的Std::async,c++,templates,asynchronous,static,C++,Templates,Asynchronous,Static,下面是我试图运行的代码: class Logger { public: template <typename... T> static void log(T&&... args) { std::async(std::launch::async, &Logger::_logStart<T...>, std::forward<T>(args)...); } private: template
class Logger {
public:
template <typename... T>
static void log(T&&... args) {
std::async(std::launch::async, &Logger::_logStart<T...>, std::forward<T>(args)...);
}
private:
template <typename... T>
static void _logStart(T&&... args) {
std::lock_guard<std::mutex> guard(_logLock);
// do stuff
}
}
类记录器{
公众:
模板
静态无效日志(T&…args){
std::async(std::launch::async,&记录器::_logStart,std::forward(args)…);
}
私人:
模板
静态void_logStart(T&…args){
std::锁紧防护装置(logLock);
//做事
}
}
下面是我得到的错误:
1>c:\c++\pv264_project\src\Logger.hpp(14): error C2893: Failed to specialize function template 'std::future<result_of<_Ty>::type> std::async(std::launch,_Fty &&,_ArgTypes &&...)'
1> with
1> [
1> _Ty=decay<_Ty>::type (decay<_ArgTypes>::type...)
1> ]
1>c:\c++\pv264_project\src\Logger.hpp(14): note: With the following template arguments:
1>c:\c++\pv264_project\src\Logger.hpp(14): note: '_Fty=void (__cdecl *)(const char (&)[10],std::string &&,const char (&)[9])'
1>c:\c++\pv264_project\src\Logger.hpp(14): note: '_ArgTypes={const char (&)[10], std::basic_string<char,std::char_traits<char>,std::allocator<char>>, const char (&)[9]}'
1>c:\c++\pv264\u project\src\Logger.hpp(14):错误C2893:未能专门化函数模板“std::future std::async(std::launch,_Fty&&,_ArgTypes&&…”
1> 与
1> [
1> _Ty=衰变::类型(衰变::类型…)
1> ]
1> c:\c++\pv264\U project\src\Logger.hpp(14):注意:使用以下模板参数:
1> c:\c++\pv264_project\src\Logger.hpp(14):注意:“_Fty=void(uu cdecl*)(const char(&)[10],std::string&,const char(&)[9])”
1> c:\c++\pv264_project\src\Logger.hpp(14):注意:''u ArgTypes={const char(&)[10],std::basic_string,const char(&)[9]}'
我已经阅读了这个(),并且将模板添加到了_logStart的传递中,但是模板专门化仍然失败。问题出在哪里
Edit:进一步调查后,问题似乎在于使用char-literal调用此日志函数。对于函数类型和作为参数转发时,它的推导方式似乎有所不同。我如何保证_logStart和std::forward(args)中的所有类型。。。即使使用const char*?Off-topic:std::async将返回std::future,您可能需要检查其析构函数的行为!我不需要从那个异步调用中返回任何东西,而且我正在使用std::launch::async,所以它会立即被调用。我已经更新了代码,所有的东西似乎都在编译:仅供参考:>请参阅(从C++14开始),您可以使用
std::async(std::launch::async,[&]({Logger:::)\u logStart(std::forward args);})代码>。