2009/6/28 Zachary Turner <divisortheory@gmail.com>
Is there a way, using some combination of boost preprocessor and mpl,
that I can write a class

template<class T, int n>
class foo
{
public:

   void foo(T arg1, T arg2, ..., T argn);
};

In other words, generate a function that requires n arguments where n
is a template parameter?

Using preprocessor you can generate a set of functions taking from 0 to M (some predefined constant) arguments. Inside of these functions static assert that number of arguments is equal to n. If a caller calls a function with wrong number of arguments, the compile error will occur, which is exactly what you need.

  #include <iostream>
  #include <boost/preprocessor.hpp>
  #include <boost/static_assert.hpp>

  #define M 10

  template <class T, int N>
  struct foo {
  #define BOOST_PP_LOCAL_MACRO(Q) \
    void bar(BOOST_PP_ENUM_PARAMS(Q, T arg)) { \
      BOOST_STATIC_ASSERT(Q == N); \
      std::cout << "I'm bar's body" << std::endl; \
    }
  #define BOOST_PP_LOCAL_LIMITS (0, M)
  #include BOOST_PP_LOCAL_ITERATE()
  };

  int main() {
    foo<int, 2> f;
    f.bar(1, 2);  // Works.
    f.bar(1, 2, 3);  // Does not work.
  }

Roman Perepelitsa.