Topic: Additional location for parameter pack expansion


Author: "peter miller" <fuchsia.groan@virgin.net>
Date: Sat, 10 Sep 2011 00:29:42 -0700 (PDT)
Raw View
Hi all,

I think the list of places where a pack expansion can appear
(14.5.3/4 [temp.variadic]) should include using declarations;
for example, I found myself wanting to code this:

template <typename...Mixins>
struct x : Mixins...
{
 using Mixins::some_method ...; // ****NOTE pack expansion.
};

I can do the above, but it takes all this clutter:

#include <cstdio>
#include <type_traits>

template <typename...Args>
struct s;

template <typename Head, typename...Tail>
auto
get_head( Head const&, Tail const&...)
 -> Head;

template <typename...Args>
struct head
{
 typedef decltype( get_head( std::declval<Args>()... ) ) type;
};

template <typename Head, typename...Tail>
auto
get_tail( Head const&, Tail const&... )
 -> s<Tail...>;

template <typename...Args>
struct tail
{
 typedef decltype( get_tail( std::declval<Args>()... ) ) type;
};



template <typename...Args>
struct s
: head<Args...>::type,  tail<Args...>::type
{
 using head<Args...>::type::print;
 using tail<Args...>::type::print;
};

template <>
struct s<>
{
private:
 struct dummy_function;
public:

 void print( dummy_function const& );
};


struct a
{
 void print( int i )
     { printf( "%d\n", i ); }
};

struct b
{
 void print( double d )
     { printf( "%f\n", d ); }
};

struct c
{
 void print( const char* s )
     { printf( "%s\n", s ); }
};

static_assert( std::is_same< head<a,b,c>::type ,a >::value
   && std::is_same< tail<a,b,c>::type, s<b,c> >::value, "test" );

int main()
{
   s<a,b,c> printer;
   printer.print( "hello" );
   printer.print( 0.1 );
   return 0;
}

Just a thought.

Peter
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/


[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]