Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;如何将参数包存储为变量_C++ - Fatal编程技术网

C++ C++;如何将参数包存储为变量

C++ C++;如何将参数包存储为变量,c++,C++,目前,我在尝试存储参数包时遇到问题,以下是设计的示例代码: template<typename Func, typename... Args> void handleFunc(Func func, Args&&... args) { struct nest { Func nestFunc; Args... nestArgs; // I DONT KNOW WHAT TO DO HERE void setup(Fu

目前,我在尝试存储参数包时遇到问题,以下是设计的示例代码:

template<typename Func, typename... Args>
void handleFunc(Func func, Args&&... args) {
    struct nest {
        Func nestFunc;
        Args... nestArgs; // I DONT KNOW WHAT TO DO HERE
        void setup(Func func, Args... args) {
            nestFunc = func;
            nestArgs = (args)...; // SO I CAN SET IT HERE
        }
        // Later I will forward this and run the function with its arguments
        unsigned process() {
            nestFunc(std::forward<Args>(nestArgs)...); // USE IT HERE
            return 0;
        }
    };
    nest* myNest;
    myNest->setup(func, (args)...);
}
模板
void handleFunc(Func Func,Args&…Args){
结构巢{
Func-nestFunc;
Args…nestArgs;//我不知道在这里该怎么办
无效设置(函数、参数…参数){
nestFunc=func;
nestArgs=(args)…;//所以我可以在这里设置它
}
//稍后,我将转发它,并使用它的参数运行该函数
未签名进程(){
nestFunc(std::forward(nestArgs);//在这里使用它
返回0;
}
};
鸟巢*myNest;
myNest->setup(函数,(参数)…);
}

这是问题涉及的所有方面的一个示例,我需要在我的嵌套结构中存储以后调用的参数。此外,如果你有一个解决方案来存储它,但设置它是不同于我的,请也让我知道这一点。谢谢。

从2018年开始编辑:在C++17中,这个问题的答案是不同的。您仍然需要将参数存储在
::std::tuple
中,但在调用函数时,该函数会处理解包该tuple并为您调用该函数。如果您需要对
::std::apply
之外的其他功能使用索引技巧,那么您应该研究的还有与之相关的助手函数
::std::make_index_sequence

现在回到2013年的C++11/14答案

您必须使用
::std::tuple

稍后,我将提取最相关的位,并将它们放在您的代码中

:

将参数保存到元组中。我使用了
::std::move
,我认为这样做是正确的。但有可能我错了,我应该使用
::std::forward
。除了表示意图外,我从来没有弄清楚确切的区别

可以找到使用保存的参数实际执行调用的代码。现在,该代码非常具体地描述了我正在做的事情。实现索引技巧的位包括创建一组整数,这些整数映射到索引以用作参数
::std::get
模板。一旦有了这个整数包,就可以使用它来扩展对
::std::get
的调用,以获取所有元组元素作为单个参数

我将尝试以一种相对简单的方式编写代码:

#include <tuple>
#include <cstddef>
#include <string>
#include <utility>

template < ::std::size_t... Indices>
struct indices {};

template < ::std::size_t N, ::std::size_t... Is>
struct build_indices : build_indices<N-1, N-1, Is...>
{};

template < ::std::size_t... Is>
struct build_indices<0, Is...> : indices<Is...>
{};

template <typename FuncT, typename ArgTuple, ::std::size_t... Indices>
auto call(const FuncT &f, ArgTuple &&args, const indices<Indices...> &)
   -> decltype(f(::std::get<Indices>(::std::forward<ArgTuple>(args))...))
{
   return ::std::move(f(::std::get<Indices>(::std::forward<ArgTuple>(args))...));
}

template <typename FuncT, typename ArgTuple>
auto call(const FuncT &f, ArgTuple &&args)
     -> decltype(call(f, args,
                      build_indices< ::std::tuple_size<ArgTuple>::value>{}))
{
    const build_indices< ::std::tuple_size<ArgTuple>::value> indices;

    return ::std::move(call(f, ::std::move(args), indices));
}

int myfunc(::std::string name, const unsigned int foo)
{
   return 0;
}

int foo(::std::tuple< ::std::string, const unsigned int> saved_args)
{
   return call(myfunc, ::std::move(saved_args));
}
#包括
#包括
#包括
#包括
模板<::标准::大小\u t。。。指数>
结构索引{};
模板<::std::size\u t N,::std::size\u t。。。是>
结构构建索引:构建索引
{};
模板<::标准::大小\u t。。。是>
结构构建索引:索引
{};
模板
自动调用(常量FuncT&f、ArgTuple&&args、常量索引&)
->decltype(f(::std::get(::std::forward)


另外,这是一个您必须稍微适应您的具体情况的示例。基本上,只需在某个地方调用
call(nestFunc,saved_args)

我知道已经有一段时间了,但我也有类似的需求,并提出了此解决方案,希望它能帮助某人:

#include <functional>

template<typename Func, typename... Args>
struct nest {
    std::function<void()> callBack;

    void setup(Func func1, Args... args) {
        callBack = [func1, args...]()
        {
            (func1)(args...);
        };
    }

    unsigned process() {
        callBack();
        return 0;
    }
};

template<typename Func, typename... Args>
void handleFunc(Func func, Args&&... args) {
    nest<Func, Args...> myNest;
    myNest.setup(func, args...);
}
#包括
模板
结构巢{
函数回调;
无效设置(Func func1,参数…参数){
回调=[func1,args…]()
{
(1)(args…);
};
}
未签名进程(){
回调();
返回0;
}
};
模板
void handleFunc(Func Func,Args&…Args){
燕窝;
myNest.setup(函数、参数…);
}

std::tuple
-还有,
std::bind
std::function
以及所有有趣的东西。这能回答你的问题吗?如何将这个tuple设置为(args)…;你能详细说明这些“标记”吗?@David:是的,还有更多的例子。你应该有它们。事实上,我最近不得不写这样做的代码。@Omnifarious,很有趣,我只是在想是否可以存储一个参数包以供以后扩展。你的代码是否可以存储一个可调用对象以及一系列参数以供以后调用?@Omnifarious,Sweet,就是这样事实上,我一直在寻找一些我一直想要的应用。老实说,我从一段时间前就已经拥有了一个<>代码> STD::函数< /C>(它不需要返回值),绑定的论据。聪明的解决C++错误的语法包。将它们全部存储在lambda中而不是元组中是非常聪明的。尽管使用广义lambda捕获表达式转发所有参数(然后在lambda中的调用中转发它们)可能是一个更好的主意。
#include <functional>

template<typename Func, typename... Args>
struct nest {
    std::function<void()> callBack;

    void setup(Func func1, Args... args) {
        callBack = [func1, args...]()
        {
            (func1)(args...);
        };
    }

    unsigned process() {
        callBack();
        return 0;
    }
};

template<typename Func, typename... Args>
void handleFunc(Func func, Args&&... args) {
    nest<Func, Args...> myNest;
    myNest.setup(func, args...);
}