c ++ 11:使用向量的元素调用可变参数函数,并自动推导参数数量

汤姆

尝试扩展此处找到的示例:https : //stackoverflow.com/a/17974752

基本上,我想从自动传递的函数中推断出参数的数量(这显然对于重载函数不起作用)。我希望这也可以与函子和lambda一起使用。

无法编译对以下参数的调用call_helper:错误:解析模板参数中的错误

我似乎无法弄清楚如何传递将参数数量作为模板参数返回的constexpr。

这是我到目前为止所拥有的:

#include <vector>

template <typename R, typename ... T>
constexpr std::size_t get_args_count( R(*f)(T ...))
{
   return sizeof...(T);
}

template< std::size_t... Ns >
struct indices {
    typedef indices< Ns..., sizeof...( Ns ) > next;
};

template< std::size_t N >
struct make_indices {
    typedef typename make_indices< N - 1 >::type::next type;
};

template<>
struct make_indices< 0 > {
    typedef indices<> type;
};

void abc(int) {}
void abc2(int, int) {}

// helper function because we need a way
// to deduce indices pack

template<typename Func, size_t... Is>
void call_helper2(Func f, const std::vector<int>& args, indices<Is...>)
{
    f( args[Is]... ); // expand the indices pack
}

template<typename Func, size_t N>
void call_helper(Func f, const std::vector<int>& args)
{
    call_helper2(f, args, typename make_indices<N>::type());
}

template<typename Func>
void call(Func f, const std::vector<int>& args)
{
    if (args.size() < get_args_count(f)) throw 42;
    call_helper<get_args_count(decltype(f))>(f, args); // error: parse error in template argument list
}

int main()
{
    struct F
    {
        void operator()(int, int, int, int) {}
    };

    std::vector<int> v(4);
    call(&abc2, v);
    call(&abc, v);
    call([&](int, int, int) { (void)v.empty(); }, v);
    call(F(), v);
}

我想念什么或做错什么?任何帮助表示赞赏。

编辑:添加了functor和lambda用例

YSC

有两个错误:

  1. 在中call_helper<get_args_count(decltype(f))>(f, args)get_args_count(decltype(f))毫无意义,因为get_args_count它需要一个函数指针而不是一个类型(显然);
  2. 函数参数永远不能是constexpr(我将让您搜索为什么)。

怎么修?

您需要尽快提取参数的数量,然后参数f才能变为可在constexpr上下文中使用的表达式以外的其他参数

#include <vector>

template< std::size_t... Ns >
struct indices {
    typedef indices< Ns..., sizeof...( Ns ) > next;
};

template< std::size_t N >
struct make_indices {
    typedef typename make_indices< N - 1 >::type::next type;
};

template<>
struct make_indices< 0 > {
    typedef indices<> type;
};

void abc(int) {}
void abc2(int, int) {}

// helper function because we need a way
// to deduce indices pack

template<typename Func, size_t... Is>
void call_helper2(Func f, const std::vector<int>& args, indices<Is...>)
{
    f( args[Is]... ); // expand the indices pack
}

template<typename Func, size_t N>
void call_helper(Func f, const std::vector<int>& args)
{
    call_helper2(f, args, typename make_indices<N>::type());
}

template<class R, class ... T>
void call(R(*f)(T ...), const std::vector<int>& args)
{
    if (args.size() < sizeof...(T)) throw 42;
    call_helper<
        decltype(f), sizeof...(T)
    >(f, args);
}

int main()
{
    std::vector<int> v(2);
    call(&abc2, v);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在C ++中使用带有参数类型检查的可变参数模板在编译时获取函数的参数数量

C ++ 11:可变参数模板函数参数的数量?

参数数目可变的函数c

C++ 可变参数函数中的模板参数推导错误

调用类内的函数的C ++ 11可变参数模板

由模板参数固定的C ++函数参数数量

C ++ 11:可变参数模板推导逻辑

具有可变参数包的函数的C ++部分模板参数推导在Clang和MSVC中产生歧义调用

如何在C ++ 11中实现可变参数模式以将可变数量的参数转发给函数?

使用未定义数量的参数调用 C 函数

转发C中可变参数函数的调用

如何使用C ++ 11可变参数模板定义由向量元组支持的向量元组?

看不懂C++11模板函数参数推导

以数组为参数在C ++中调用可变参数函数

C ++ 11可变数量的参数,相同的特定类型

在C ++ 11中使用模板参数的递归可变参数void函数

c ++ 11可变参数编程,如何定义向量塔

C ++可变参数模板推导失败

C ++可变参数模板迭代向量并比较元素

当输入参数数量未知时,使用可变数量的输入参数进行函数调用

已知数量的args,在c中转发可变参数函数

调用可变参数函数模板时C ++ 11模棱两可的重载

如何在C ++ 17中创建从可变参数模板推导的向量类型的元组?

C ++可变参数模板参数的数量

C ++-通过enable_if_t推导参数包(可变模板)构造函数和复制构造函数

模板参数的C ++自动模板推导失败

C++ 模板类型参数的自动推导

C#当参数数量相等时,如何使用System.Reflection调用私有重载方法

以可变参数作为函数C ++的参数的函数