Variadic templates std::tuple
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.
So one can make things like this:
auto t = std::make_tuple(1, "Foo", 3.14);
std::cout << std::get<0>(t)
<< ", " << std::get<1>(t)
<< ", " << std::get<2>(t) << "\n";
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?
Re: Variadic templates std::tuple
On 12 Dez., 20:04, Juha Nieminen <nos...@thanks.invalid> wrote:
> lieve again <hablando...@gmail.com> wrote:
> > 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.
> "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: n...@netfront.net ---
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
Re: Variadic templates std::tuple
On 12/14/12 03:45, Juha Nieminen wrote:
> lieve again <email@example.com> wrote:
>> Know anyone an example code with variadic templates that also has
>> types or members? I mean a real class and not just a recursive
> 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:
> --- news://freenews.netfront.net/ - complaints: firstname.lastname@example.org ---
The following boost.devel post:
demonstrates that there's no "optimal" way (at least w.r.t. compile
times) because compile times are very dependent on which compiler
BTW, AFAICT, the c11-tuple-implementation-details-part-1.html looks
like the implementations called horizontal in the boost.devel post.
|All times are GMT. The time now is 07:53 PM.|
Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.