std::invoke 不喜欢可变参数模板成员函数?

用户5406764

我正在尝试使用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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

std :: function到可变参数成员函数,然后绑定可变参数模板参数

std :: bind与可变参数模板成员函数和通用引用

使用std :: invoke调用模板化函数

std :: function的可变参数模板参数

具有扩展的元组参数std :: invoke与std :: apply的调用成员函数

多维std :: array的可变参数模板

指向std :: invoke中成员函数对象的指针

可变参数模板,std :: function和lambdas作为类成员

使用来自 std::vector 的参数调用可变参数模板化函数

可变参数模板函数:没有匹配的调用函数,std :: endl

函数采用可变参数模板包将std :: strings转换为const char *?

从可变参数模板数组引用构造函数初始化双嵌套std :: array

从具有可变参数模板构造函数的类型构造 std::function 对象

可变参数模板作为std :: function的参数

std :: function中的可变参数模板参数匹配

使用std :: function作为参数的可变参数模板

如何从可变参数模板参数创建std :: tuple <>?

为什么传递给模板函数的std :: string对象不喜欢std :: string重载?

可变参数模板和std :: array意外行为

在可变参数模板中使用std :: placeholders

C ++ 11可变参数模板和std :: endl

std :: holds_alternative可变参数模板

可变参数模板,类型扣除和std :: function

std :: bind与可变参数模板和自动返回类型

计算可变参数模板元组中的 std::optional 类型

如何使用可变参数模板使用std :: function

对可变参数模板结构使用std :: visit

constexpr可变参数模板并解压std :: array

可变参数模板展开到std :: tuple