我正在尝试使用std::invoke()
and调用可变参数函数模板std::apply()
。
我提前道歉,因为我基本上是在这里删除一段代码并请人帮助我理解错误消息以解决问题。
因此,在下面的示例代码中,
std::invoke()
在非可变参数模板函数上工作正常。std::invoke()
关于可变参数模板函数不编译!#include <functional>
#include <tuple>
struct Thing
{
// Some simple functions to test things out
int func0() { return 0; }
int func1(int) { return 1; }
int func2(int, int) { return 2; }
// A variadic template function that causes problems below
template<typename ...Args>
int funcn(Args&&...) { return 99; }
};
int main()
{
Thing thing;
// These work fine
std::invoke(&Thing::func0, thing);
std::invoke(&Thing::func1, thing, 1);
std::invoke(&Thing::func2, thing, 1, 2);
// This one doesn't work
std::invoke(
&Thing::funcn,
thing,
1, 2, 3, 4
);
}
我得到的错误在这里:(x86-64 clang 12.0.1 (Compiler #1) 的输出)
Wrap lines
<source>:26:5: error: no matching function for call to 'invoke'
std::invoke(
^~~~~~~~~~~
functional:94:5: note: candidate template ignored: couldn't infer template argument '_Callable'
invoke(_Callable&& __fn, _Args&&... __args)
^
该std::invoke
预期可调用的函数。这funcn
是一个函数模板,您需要实例化以从中获取真正的函数,然后您可以获取它的地址。
这意味着(显式地)为函数提供模板参数,您希望如何实例化它,以便std::invoke
可以看到它可以调用的函数。
std::invoke(
&Thing::funcn<int, int, int, int>, // works now
thing,
1, 2, 3, 4
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句