Is it possible to store variadic arguments into a member variable?

Tim

I want to know if it is possible to store variadic template arguments into a member variable, for example a tuple? So I can use it in another method. I want it to work something like this, see below:

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;
};

I am fairly new to C++ programming. I have some experience in other languages such as C#, AS3, Java.

Yakk - Adam Nevraumont

Assuming I read your mind right, you save the args by not saving the args.

First, write this:

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 takes a tuple, and does the operation using those arguments.

BuildOperation takes arguments, bundles them into a tuple, and generates a saved_operation from them.

saved_operation is basically a saved method call. I don't store this because by avoiding that, the default copy ctor does the right thing. Instead, you pass this in each time you use it.

Now using the above, we implement your stuff:

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);
}

A copy of the tuple is actually stored inside the saved_operation object, but that is an implementation detail.

The trick is we care not about the data, but rather what we will do with the data later.

I used some concrete types (the int double etc), but those can just as easily be template methods as well.

If you need the efficiency, a bit more care involving moving data around instead of copying can be useful. But I kept it relatively simple. If you really need a pack of any args, you might have to google the "indexes trick" to unpack the tuple of unknown content back into parameters.

In this case, std::function is a type erasure class that erases the details of what it is constructed from, except for the fact it can be copied, destroyed, invoked with a particular signature (and also cast-back-to-source-type, which few people use).

We exploit this to "forget" the tuple and instead just remember the operation we want to do on the tuple.

This technique can be used in more general situations: you can type erase anything with a fixed signature, or really anything that can be boiled down to a fixed signature (which is a bit broader).

Words to search for for more on this topic include "runtime concepts" and "type erasure". Examining how std::function can be implemented.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Conditionally enable a constructor whenever a member variable can be constructed by variadic arguments

From Dev

Is it possible with LESS CSS to store multiple arguments in a variable?

From Java

How to store variadic template arguments?

From Dev

std::function to variadic member function and then bind variadic template arguments

From Dev

How can I store the arguments of a variadic type?

From Dev

How to store template variadic arguments into std::ofstream?

From Dev

std::function with C variadic arguments, not templated variable arguments

From Dev

How to store parameters from variadic template member function into vector?

From Dev

Most efficient way to store variadic function template arguments into a union vector?

From Dev

Is it possible to pass a variable as a structure member?

From Dev

Defining member variable using decltype on variadic function template of a class template

From Dev

Compiler error or correct behavior for static const member variable, variadic templates, and &&?

From Dev

Is it possible to use dynamic variable for a class member variable

From Dev

How to use variadic templates to wrap a variable number of function arguments?

From Dev

Unpack all variadic template arguments except last one as type of variable

From Dev

Is it possible to store the results of a mixin in a variable?

From Dev

How to store function as class member variable

From Dev

How to store parseObject.getString() into a member variable

From Dev

Is it possible to restrict a member function from accessing a member variable?

From Dev

Is it possible to access a member variable of a pair from queue member function directly?

From Dev

Is it possible to restrict a member function from accessing a member variable?

From Dev

localizeWithFormat and variadic arguments in Swift

From Dev

Convert arguments of variadic function

From Dev

Functions as arguments of variadic templates

From Dev

Variadic macro without arguments

From Dev

Variadic macros arguments

From Dev

How to pass a member function which has variable arguments as template argument?

From Dev

Variadic template class - variadic member function

From Dev

Why is it possible to store a function of the prototype inside a variable?

Related Related

  1. 1

    Conditionally enable a constructor whenever a member variable can be constructed by variadic arguments

  2. 2

    Is it possible with LESS CSS to store multiple arguments in a variable?

  3. 3

    How to store variadic template arguments?

  4. 4

    std::function to variadic member function and then bind variadic template arguments

  5. 5

    How can I store the arguments of a variadic type?

  6. 6

    How to store template variadic arguments into std::ofstream?

  7. 7

    std::function with C variadic arguments, not templated variable arguments

  8. 8

    How to store parameters from variadic template member function into vector?

  9. 9

    Most efficient way to store variadic function template arguments into a union vector?

  10. 10

    Is it possible to pass a variable as a structure member?

  11. 11

    Defining member variable using decltype on variadic function template of a class template

  12. 12

    Compiler error or correct behavior for static const member variable, variadic templates, and &&?

  13. 13

    Is it possible to use dynamic variable for a class member variable

  14. 14

    How to use variadic templates to wrap a variable number of function arguments?

  15. 15

    Unpack all variadic template arguments except last one as type of variable

  16. 16

    Is it possible to store the results of a mixin in a variable?

  17. 17

    How to store function as class member variable

  18. 18

    How to store parseObject.getString() into a member variable

  19. 19

    Is it possible to restrict a member function from accessing a member variable?

  20. 20

    Is it possible to access a member variable of a pair from queue member function directly?

  21. 21

    Is it possible to restrict a member function from accessing a member variable?

  22. 22

    localizeWithFormat and variadic arguments in Swift

  23. 23

    Convert arguments of variadic function

  24. 24

    Functions as arguments of variadic templates

  25. 25

    Variadic macro without arguments

  26. 26

    Variadic macros arguments

  27. 27

    How to pass a member function which has variable arguments as template argument?

  28. 28

    Variadic template class - variadic member function

  29. 29

    Why is it possible to store a function of the prototype inside a variable?

HotTag

Archive