Boost logo

Boost :

From: Philippe A. Bouchard (philippe_at_[hidden])
Date: 2003-08-04 22:39:29


Hello,

    Often some function and functors requires to be overloaded N times to
handle undefinite number of arguments, depending on some default setting.
Maybe the following could be used; it overloads operator , () and gradually
creates a typelist. This typelist is used to replace the undefinite number
of arguments:

#include <iostream>

using namespace std;

namespace infinite
{

template <typename T = void, typename U = void>
 struct typelist : U
 {
  typedef T type;

  T value;

  typelist(T const & a, U const & b) : value(a), U(b) {}
 };

template <>
 struct typelist<void, void>
 {
  typedef void type;
 };

template <typename T>
 struct typelist<T, void>
 {
  typedef T type;

  T value;

  typelist(T const & a) : value(a) {}
 };

typelist<> const begin = typelist<>();

}

template <typename V>
 infinite::typelist<V, void> operator , (infinite::typelist<> const &, V
const & v)
 {
  return infinite::typelist<V, void>(v);
 }

template <typename T, typename U, typename V>
 infinite::typelist< V, infinite::typelist<T, U> > operator ,
(infinite::typelist<T, U> const & t, V const & v)
 {
  return infinite::typelist< V, infinite::typelist<T, U> >(v,
infinite::typelist<T, U>(t));
 }

// Usage example:

template <typename T, typename U>
 void foo(infinite::typelist<T, U> const & i)
 {
  cout << __PRETTY_FUNCTION__ << endl;
 }

int main()
{
 foo((infinite::begin, true, (char *) "adasd", 12367, 127.2));
}

My 0,02$

Philippe


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk