Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: How call a function with type parameter on variadic template arguments?

So I have a function with variadic template arguments and I'm trying to invoke a method (with a type parameter) for every argument, pack every result value in a std::tuple and return it. However variadic templates are pretty hard for me and I haven't fully understood them yet.

Is it even possible to realize this in C++?

Here's my code so far (which has an error in the getMultiple function). Thank you very much for helping!

#include <iostream>
#include <fstream>
#include <sstream>

template<typename T>
T get(std::istream &stream) {
    T data;
    stream >> data;
    return data;
}

template<typename ... Ts>
std::tuple<Ts...> getMultiple(std::istream &stream) {
    // What am I doing wrong here?
    return std::make_tuple((get<Ts...>(stream)));
}

int main() {
    std::istringstream stream("count 2");
    auto [command, number] = getMultiple<std::string, int>(stream);

    return 0;
}
like image 685
Krystex Avatar asked Jan 04 '21 23:01

Krystex


People also ask

How do you access variadic arguments?

To access variadic arguments, we must include the <stdarg. h> header.

Can variadic methods take any number of parameters?

A variadic function allows you to accept any arbitrary number of arguments in a function.

Is printf variadic function?

Variadic functions are functions (e.g. printf) which take a variable number of arguments.

What is a variadic template C++?

Variadic templates are class or function templates, that can take any variable(zero or more) number of arguments. In C++, templates can have a fixed number of parameters only that have to be specified at the time of declaration. However, variadic templates help to overcome this issue.

What are variadic function templates in C++?

Variadic function templates in C++. Variadic templates are template that take a variable number of arguments. Variadic function templates are functions which can take multiple number of arguments.

What are variadic arguments in C++?

Douglas Gregor and Jaakko Järvi came up with this feature for C++. Variadic arguments are very similar to arrays in C++. We can easily iterate through the arguments, find the size (length) of the template, can access the values by an index, and can slice the templates too.

How do I call another method from a template parameter pack?

In your template, you can then call another method with that parameter pack, by expanding (or unpacking) it into the function argument list. For instance, with “values…”: template <class... T_values>

What is a variable parameter function in C programming language?

The C programming language provides a solution for this situation and you are allowed to define a function which can accept variable number of parameters based on your requirement. The following example shows the definition of such a function.


1 Answers

First of all, you forgot the

#include <tuple>

And the syntax you're probably looking for is:

return std::make_tuple(get<Ts>(stream)...);
like image 161
Sam Varshavchik Avatar answered Oct 20 '22 19:10

Sam Varshavchik