是否可以将可变参数存储到成员变量中?
Is it possible to store variadic arguments into a member variable?
我想知道是否可以将可变参数模板参数存储到成员变量中,例如元组?所以我可以用另一种方法使用它。我希望它像这样工作,见下文:
class TempClass
{
public:
//How can I Store the arguments with the help of a variable?
std::tuple<Template Args?> savedArgs;
template<typename ...Args>
void SaveTheseArgs(args&& ...args)
{
//Store the arguments into a variable for later use
savedArgs = std::make_tuple<Args>(args);
//Do something with the args...
}
void OtherMethod()
{
//I want to pass it back to the same method with the same arguments.
SaveTheseArgs(savedArgs);
}
}
//I tried a struct template like this, but this actually doesn't store them unless I am doing something wrong.
template<typename... Args>
struct StoredArguments
{
std::tuple<Args...> StoredArgs;
};
我对 C++ 编程还很陌生。我有一些其他语言的经验,例如 C#、AS3、Java.
假设我没看错你的想法,你通过不保存参数来保存参数。
首先写下:
void DoOperation( std::tuple<int, double, std::string> const& tup ) {
auto&& some_arg_name = std::get<0>(tup);
auto&& second_arg_name = std::get<1>(tup);
// etc
// do stuff with the args
}
typedef std::function<void(TempClass*)> saved_operation;
saved_operation BuildOperation( int a, double b, std::string s ) const {
auto tup = std::make_tuple(a,b,s);
return [tup](TempClass* self){
return self->DoOperation(tup);
};
}
DoOperation
接受一个元组,并使用这些参数进行操作。
BuildOperation
接受参数,将它们捆绑成一个元组,并从中生成一个 saved_operation
。
saved_operation
基本上是一个保存的方法调用。我不存储 this
因为通过避免这种情况,默认的复制 ctor 会做正确的事情。相反,您每次使用它时都会传递 this
。
现在使用上面的,我们实现你的东西:
saved_operation saved_op;
template<typename ...Args>
void SaveTheseArgs(args&& ...args)
{
saved_op = BuildOperation(std::forward<Args>(args)...);
saved_op(this);
}
void OtherMethod()
{
assert(saved_op);
saved_op(this);
}
tuple
的副本实际上存储在 saved_operation
对象中,但这是一个实现细节。
诀窍是我们不关心数据,而是我们稍后将用数据做什么。
我使用了一些具体类型(int
double
等),但这些也可以很容易地成为模板方法。
如果您需要效率,多注意移动数据而不是复制数据会很有用。但我保持相对简单。如果你真的需要一包任何参数,你可能必须 google "indexes trick" 将未知内容的元组解压回参数。
在这种情况下,std::function
是一种类型擦除 class,它擦除构造它的细节,除了它可以被复制、销毁、使用特定签名调用的事实(还有很少有人使用的 cast-back-to-source-type)。
我们利用这个来 "forget" tuple
而不是只记住我们想要在 tuple
.
上执行的操作
此技术可用于更一般的情况:您可以键入擦除任何具有固定签名的内容,或者实际上任何可以归结为固定签名(范围更广)的内容。
要搜索有关此主题的更多信息,包括 "runtime concepts" 和 "type erasure"。检查如何 std::function
可以实现。
我想知道是否可以将可变参数模板参数存储到成员变量中,例如元组?所以我可以用另一种方法使用它。我希望它像这样工作,见下文:
class TempClass
{
public:
//How can I Store the arguments with the help of a variable?
std::tuple<Template Args?> savedArgs;
template<typename ...Args>
void SaveTheseArgs(args&& ...args)
{
//Store the arguments into a variable for later use
savedArgs = std::make_tuple<Args>(args);
//Do something with the args...
}
void OtherMethod()
{
//I want to pass it back to the same method with the same arguments.
SaveTheseArgs(savedArgs);
}
}
//I tried a struct template like this, but this actually doesn't store them unless I am doing something wrong.
template<typename... Args>
struct StoredArguments
{
std::tuple<Args...> StoredArgs;
};
我对 C++ 编程还很陌生。我有一些其他语言的经验,例如 C#、AS3、Java.
假设我没看错你的想法,你通过不保存参数来保存参数。
首先写下:
void DoOperation( std::tuple<int, double, std::string> const& tup ) {
auto&& some_arg_name = std::get<0>(tup);
auto&& second_arg_name = std::get<1>(tup);
// etc
// do stuff with the args
}
typedef std::function<void(TempClass*)> saved_operation;
saved_operation BuildOperation( int a, double b, std::string s ) const {
auto tup = std::make_tuple(a,b,s);
return [tup](TempClass* self){
return self->DoOperation(tup);
};
}
DoOperation
接受一个元组,并使用这些参数进行操作。
BuildOperation
接受参数,将它们捆绑成一个元组,并从中生成一个 saved_operation
。
saved_operation
基本上是一个保存的方法调用。我不存储 this
因为通过避免这种情况,默认的复制 ctor 会做正确的事情。相反,您每次使用它时都会传递 this
。
现在使用上面的,我们实现你的东西:
saved_operation saved_op;
template<typename ...Args>
void SaveTheseArgs(args&& ...args)
{
saved_op = BuildOperation(std::forward<Args>(args)...);
saved_op(this);
}
void OtherMethod()
{
assert(saved_op);
saved_op(this);
}
tuple
的副本实际上存储在 saved_operation
对象中,但这是一个实现细节。
诀窍是我们不关心数据,而是我们稍后将用数据做什么。
我使用了一些具体类型(int
double
等),但这些也可以很容易地成为模板方法。
如果您需要效率,多注意移动数据而不是复制数据会很有用。但我保持相对简单。如果你真的需要一包任何参数,你可能必须 google "indexes trick" 将未知内容的元组解压回参数。
在这种情况下,std::function
是一种类型擦除 class,它擦除构造它的细节,除了它可以被复制、销毁、使用特定签名调用的事实(还有很少有人使用的 cast-back-to-source-type)。
我们利用这个来 "forget" tuple
而不是只记住我们想要在 tuple
.
此技术可用于更一般的情况:您可以键入擦除任何具有固定签名的内容,或者实际上任何可以归结为固定签名(范围更广)的内容。
要搜索有关此主题的更多信息,包括 "runtime concepts" 和 "type erasure"。检查如何 std::function
可以实现。