Variadic templates std::tuple

L

lieve again

Hi!

I have read the new C++ standard, C++11, will add to possibility to
use the variadic templates, so a templates can take variable numbers
of template parameters.

Using that the C++ library will add the std::tuple class.
http://en.cppreference.com/w/cpp/utility/tuple

So one can make things like this:
int main()
{
auto t = std::make_tuple(1, "Foo", 3.14);
std::cout << std::get<0>(t)
<< ", " << std::get<1>(t)
<< ", " << std::get<2>(t) << "\n";
}

Output:

1, Foo, 3.14

I'm particularly interested in the implementation of the tuple::get
function. The function prototype looks like:

template< std::size_t I, class... Types >
typename std::tuple_element<I, tuple<Types...> >::type&
get( tuple<Types...>& t );

I understand that the class... Types means any number of arguments,
but I cant realize how can I "get" a particular member of the tuple
class, so for example, the second member.

Anyone knows how can one use the variadic templates?

Thanks,
 
L

lieve again

"class..." or "typename..." in a template declaration denotes a
parameter pack, ie. zero or more types.

Then, when you use the name (that was declared with "class..." or
"typename...") in an expression, and append "..." to that expression,
the compiler will duplicate said expression that many times, substituting
the name for the type, and separating each such expression with the
parameter separator ',' (which shouldn't be confused with the comma
operator which, while using the same ascii character, has a completely
different role and works very differently.)

If the parameter pack type is used in a function parameter list,
(for example "template<typename... Pack> void foo(Pack... pack);)
it will create a parameter pack which works in the same way (in other
words, if you use the parameter pack "variable name" in an expression
and append "..." to that expression, the compiler will duplicate said
expression as many times as there are parameters in the pack, making
proper substitutions.)

(Incidentally, one cool feature of this, something that very few C++
programmers yet understand, is that you can use the parameter pack
several times in an expression and append "..." at the end of it,
and it will work as expected, duplicating the entire expression and
substituting all the names as needed.)

Anyways, while there is a special syntax to get the amount of parameters
in a parameter pack (namely, "sizeof...()"), there is no direct way of
saying eg. "the third parameter" with a parameter pack. In order to do
that you need to do some template metaprogramming (which usually requires
recursive templates.)

This is, in fact, what usually needs to be used in order to do something
with all the parameters. (There are tricks that can be used to avoid
having to create recursive templates, but most often the recursive
templates are actually simpler and more straightforward than the trickery.)

The tuple example uses recursive templates to get you the nth parameter.

--- news://freenews.netfront.net/ - complaints: (e-mail address removed) ---

Thanks for the info, I understand the variadic template solution for
the printf, using recursive templates, but I sill can realize how it
can work with members, I mean, not pure
functions like printf.
Know anyone an example code with variadic templates that also has
types or members? I mean a real class and not just a recursive
function.
 
L

Larry Evans

Implemeting a class like std::tuple is not trivial (nor is there a
unique, unambiguous "optimal" way of doing it.) You can get some idea
for example here:

http://mitchnull.blogspot.fi/2012/06/c11-tuple-implementation-details-part-1.html

--- news://freenews.netfront.net/ - complaints: (e-mail address removed) ---

The following boost.devel post:

http://article.gmane.org/gmane.comp.lib.boost.devel/236192

demonstrates that there's no "optimal" way (at least w.r.t. compile
times) because compile times are very dependent on which compiler
is used.

BTW, AFAICT, the c11-tuple-implementation-details-part-1.html looks
like the implementations called horizontal in the boost.devel post.

-regards,
Larry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top