带有可变模板的C++异步找不到正确的函数模板专用化
C++ async with variadic template can not find correct function template specialization
我有一个类,它有一个成员函数f
,我用可变模板和forward
包装它,以制作另一个成员功能rf
(只需在f
的末尾添加一个特定的参数,就可以做一些不同的事情)。然后,我通过用async
包装rf
来制作另一个成员函数async_rf
,但它不起作用。我试图通过用额外的特定参数包装f
来制作async_rf
,并且它是有效的。
代码:
#include <future> // std::async, std::future
#include <iostream>
class test {
public:
void f(int tmp, bool reverse = 0)
{
std::cout << tmp << " | " << reverse << std::endl;
}
template<typename... Args>
void rf(Args... args)
{
f(std::forward<Args>(args)..., 1);
}
template<typename... Args>
std::future<void> async_rf(Args... args)
{
// doesn't work
return std::async (&test::rf, this, std::forward<Args>(args)...);
// work
return std::async (&test::f, this, std::forward<Args>(args)..., 1);
}
};
int main()
{
test s;
auto tmp = s.async_rf(10);
tmp.get();
return 0;
}
这是编译时的错误消息:
$ clang++ --version
clang version 3.6.1 (tags/RELEASE_361/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
$ clang++ -std=c++14 -Wall -lpthread src/test.cpp -o bin/test
src/test.cpp:23:16: error: no matching function for call to 'async'
return std::async (&test::rf, this, std::forward<Args>(args)...);
^~~~~~~~~~
src/test.cpp:35:18: note: in instantiation of function template specialization 'test::async_rf<int>' requested here
auto tmp = s.async_rf(10);
^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/5.1.0/../../../../include/c++/5.1.0/future:1723:5: note: candidate template
ignored: couldn't infer template argument '_Fn'
async(_Fn&& __fn, _Args&&... __args)
^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/5.1.0/../../../../include/c++/5.1.0/future:1703:5: note: candidate template
ignored: substitution failure [with _Fn = test *, _Args = <int>]: no type named 'type' in
'std::result_of<test *(int)>'
async(launch __policy, _Fn&& __fn, _Args&&... __args)
^
1 error generated.
(gcc)
$ g++ --version
g++ (GCC) 5.1.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -std=c++14 -Wall -lpthread src/test.cpp -o bin/test
src/test.cpp: In instantiation of ‘std::future<void> test::async_rf(Args ...) [with Args = {int}]’:
src/test.cpp:35:29: required from here
src/test.cpp:23:27: error: no matching function for call to ‘async(<unresolved overloaded function type>, test*, int)’
return std::async (&test::rf, this, std::forward<Args>(args)...);
^
In file included from src/test.cpp:1:0:
/usr/include/c++/5.1.0/future:1703:5: note: candidate: template<class _Fn, class ... _Args> std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...)
async(launch __policy, _Fn&& __fn, _Args&&... __args)
^
/usr/include/c++/5.1.0/future:1703:5: note: template argument deduction/substitution failed:
src/test.cpp:23:27: note: cannot convert ‘&((test*)this)->*test::rf’ (type ‘<unresolved overloaded function type>’) to type ‘std::launch’
return std::async (&test::rf, this, std::forward<Args>(args)...);
^
In file included from src/test.cpp:1:0:
/usr/include/c++/5.1.0/future:1723:5: note: candidate: template<class _Fn, class ... _Args> std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(_Fn&&, _Args&& ...)
async(_Fn&& __fn, _Args&&... __args)
^
/usr/include/c++/5.1.0/future:1723:5: note: template argument deduction/substitution failed:
src/test.cpp:23:27: note: couldn't deduce template parameter ‘_Fn’
return std::async (&test::rf, this, std::forward<Args>(args)...);
^
有人能提供更多关于为什么它不起作用的细节吗?为什么编译器找不到正确的模板专用化?
要求编译器通过提供给async
的参数来推断rf
的专门化将需要它研究async
的实现,这有点太多了。
只需自己指定模板参数:
return std::async (&test::rf<Args...>, this, std::forward<Args>(args)...);
顺便说一句,您可能希望在任何地方都将Args...
更改为Args&&...
,否则参数将按值传递。
由于rf
是一个函数模板,您不能简单地将其传递给std::async
。首先,您必须指定它的模板参数,即简单的Args...
。所以你的函数看起来像:
template<typename... Args>
std::future<void> async_rf(Args... args)
{
// doesn't work
return std::async (&test::rf<Args...>,this,std::forward<Args>(args)...);
// work
return std::async (&test::f, this, std::forward<Args>(args)..., 1);
}
尽管在该函数中有两个返回语句可能不是您想要做的。
相关文章:
- LLVM 选择找不到函数传递
- C++线程找不到函数作为参数(链接器)
- 找不到使用 bitset 实例化模板函数的有效方法
- NDK 编译器找不到"配对"模板
- C++链接编辑器找不到函数声明(未定义的引用)
- C++ python 中的函数来自使用 ctypes 的.dll - 找不到函数和访问冲突
- 如果标头位于预编译标头 xcode 中,则找不到默认模板参数
- 为什么C 编译器找不到函数定义
- mingw:使用 -std=c++11 编译时找不到函数
- MSSQL在库中找不到函数
- 为什么我找不到 DirectX 模板?
- 编译器找不到函数 glMultiDrawArraysIndirect
- std::调用函数时找不到函数构造函数
- 如果存在具有不同参数类型的重载,则在全局命名空间中找不到函数
- 链接器未解析的外部符号 - 在 dll 中找不到函数
- c++链接错误:编译器找不到函数的定义
- 找不到c++函数参数标识符
- 链接器在命名空间中找不到函数定义
- 托管 C++ 代码找不到函数实现
- 链接器找不到函数定义,LNK2001未解析的外部符号