Getting value_type of container using variadic templates

This is a discussion on Getting value_type of container using variadic templates within the C++ Programming forums, part of the General Programming Boards category; Given a type T, i need the type of the elements of T if T is an STL container, otherwise ...

  1. #1
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964

    Getting value_type of container using variadic templates

    Given a type T, i need the type of the elements of T if T is an STL container, otherwise the result should just be the type T.

    I'm no good with templates, but here is what i've tried using the hit-and-miss methodology:

    Code:
    template <typename T> struct element_type
    {
       typedef T type;
    };
    
    template <template <typename...> class Container, typename T, typename Ts...> struct element_type<Container<T, Ts...>>
    {
       typedef T type;
    };
    The idea is that element_type<int>::type would just be int, and element_type<std::vector<int>>::type would also be int. Unfortunately this code just gives me a bunch of template errors, any hints as to what the problem is?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  2. #2
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    I seem to have figured it out:

    Code:
    template <typename T> struct element_type
    {
       typedef T type;
    };
    
    template <template <typename...> class Container, typename T, typename... Ts> struct element_type<Container<T, Ts...>>
    {
       typedef T type;
    };
    I'd managed to confuse myself about where the ellipsis should be.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,147
    O_o

    So, nullary container templates get no love?

    (Hint: "SFINAE" over a check for `<???>::value_type' and other requirements of a container.)

    By the by, this should be done as `is_container<???>' paired with `value_type<???>'.

    With a couple of such primitives, you get `typename if_<is_container<T>, value_type<T>, T>::type'.

    You can, of course, implement `element_type' by inheriting `if_<is_container<T>, value_type<T>, T>' if you go that route, and you don't have to keep doing the same core--conditionally checking for a template expansion--mechanic for each new meta-function that needs to know about "containerness".

    Soma
    “Often out of periods of losing come the greatest strivings toward a new winning streak.” -- Fred Rogers
    “Salem Was Wrong!” -- Pedant Necromancer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variadic templates and std::initializer_lists
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 11-28-2012, 11:14 AM
  2. variadic templates
    By Elkvis in forum C++ Programming
    Replies: 2
    Last Post: 05-11-2009, 03:57 PM
  3. value_type of vector
    By George2 in forum C++ Programming
    Replies: 5
    Last Post: 11-19-2007, 02:34 AM
  4. Replies: 1
    Last Post: 01-23-2006, 06:12 PM
  5. Replies: 4
    Last Post: 03-21-2004, 02:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21