Variadic templates std::tuple

Discussion in 'C++' started by lieve again, Dec 12, 2012.

  1. lieve again

    lieve again Guest

    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,
     
    lieve again, Dec 12, 2012
    #1
    1. Advertising

  2. lieve again

    lieve again Guest

    On 12 Dez., 20:04, Juha Nieminen <> wrote:
    > lieve again <> 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: ---


    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.
     
    lieve again, Dec 14, 2012
    #2
    1. Advertising

  3. lieve again

    Larry Evans Guest

    On 12/14/12 03:45, Juha Nieminen wrote:
    > lieve again <> 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
    >> function.

    >
    > 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: ---


    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
     
    Larry Evans, Dec 14, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Walters
    Replies:
    2
    Views:
    535
    Ben Pfaff
    Feb 13, 2004
  2. Ross A. Finlayson
    Replies:
    19
    Views:
    624
    Keith Thompson
    Mar 10, 2005
  3. Replies:
    2
    Views:
    361
    Dave Thompson
    Feb 27, 2006
  4. Replies:
    5
    Views:
    375
  5. Valeriu Catina

    c++0x variadic templates

    Valeriu Catina, Mar 13, 2008, in forum: C++
    Replies:
    3
    Views:
    619
    Erik Wikström
    Mar 13, 2008
Loading...

Share This Page