gpt4 book ai didi

具有可变参数模板的 C++ 异步无法找到正确的函数模板特化

转载 作者:行者123 更新时间:2023-11-30 03:51:47 24 4
gpt4 key购买 nike

我有一个带有成员函数 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 )

$ 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.

(海合会)

$ 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&&...,否则参数将按值传递。

关于具有可变参数模板的 C++ 异步无法找到正确的函数模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31104064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com