Boost logo

Boost Users :

From: Michael Linck (mgl_at_[hidden])
Date: 2007-11-27 13:53:43


        This my first attempt at using the boost::mpl library, so I may be off
on the wrong track. If that's the case, please let me know and I'll
come up with a different way to solve my problem. This seemed like a
good practice case though. I'm trying to write a class that can test a
function (eventually any function). The idea is that you specialize
some number of arguments ( up to ten ) and then hand it a function.
Then you feed "test cases" into it, which consist of arguments and an
expected result.

    It seemed to me that TMP might be the key avoid having to write the
template class 10 times (once for each number of template parameters).
I thought it would look something like this:

template<typename TestResultTA,
         typename TestInput1TA,
         typename TestInput2TA = void,
         typename TestInput3TA = void,
         typename TestInput4TA = void,
         typename TestInput5TA = void,
         typename TestInput6TA = void,
         typename TestInput7TA = void,
         typename TestInput8TA = void,
         typename TestInput9TA = void,
         typename TestInput10TA = void>
class TestScenario
{
  typedef boost::mpl::vector<TestInput1TA,
                             TestInput2TA,
                             TestInput3TA,
                             TestInput4TA,
                             TestInput5TA,
                             TestInput6TA,
                             TestInput7TA,
                             TestInput8TA,
                             TestInput9TA,
                             TestInput10TA> TenArgs;
  
  typedef typename boost::mpl::find<TenArgs, void>::type FirstUnusedArg;

  typedef typename TenArgs::erase<TenArgs,
                                  FirstUnusedArg,
                                  TenArgs::end> JustTheArgs;
  // I know this is non-sense but what should go here?
  typedef boost::function2<TestResultTA, JustTheArgs> TestFunctionTS;
  .
  .
  .

So far so good. I have a type that is a vector of the subset of
template arguments that are initialized.

I've thought about maybe trying to use #defines and recursion to create
a template argument string for the boost::function, or something. That
seems totally hideous, likely to fail, and my coworkers wouldn't accept
it as maintainable code anyway just because of the #defines' presence.

Is there an elegant way to go from a vector of types to an argument list
for another template? I would have to use this mechanism to also
specialize various other functions, like the one that accepts sets of
arguments, and some kind of struct for holding those arguments at
run-time.

Thank you,

mike


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net