Frank-René Schäfer said:
The idea was to have something that
represents a list of arguments, i.e.
[snip example pseudocode]
First of all, thanks for focusing me on this problem! I certainly
have use for the result. Note that if you end up using the code
you'll probably want to add at least non-const access. Before I
explain the code, here's the achieved initialization syntax:
typedef mpl::vector<V, int, int, TempTest> Types;
X<Types> x(tltools::Arguments<Types>(V(3, 2.5))(7)()(1));
The code starts out with the "library" part in namespace tltools,
containing the Arguments and Instantiator facilities. Instantiator
corresponds to your linear_generator; I used a different name to
avoid confusion in case you have already implemented the latter.
After that those facilities' use is demonstrated.
Both the Arguments and Instantiator class templates work in the same
basic way: they build an inheritance chain of template instantiations
from a typelist, with a node for each element type, using the primary
template except for the innermost base. This is similar to what I
posted before, but this time there is no sentinel class at the end.
Each node of an Arguments chain has a constructor taking the
corresponding type in the typelist. However, this ctor is only used
to pass the first argument with the intent to smooth out the point-
of-use syntax. Any further arguments are passed through the
operator() in the primary template that consequently takes the *base*
argument type and is not present in the template specialization. A
conceptually parameterless ctor is also required of the base, but it
cannot be the default ctor as that would disallow a default parameter
to the pass-in ctor -- hence the dummy parameter.
Argument storage and retrieval is delegated to the specialization
that type-safely encapsulates a void* container. It is also safe for
temporaries because those persist until the end of a statement like
the initialization of x in the code excerpt above. Finally, the
specialization has a conversion back to the outermost type, again for
convenient use. To ensure that this conversion is only used on a
completely filled Arguments structure, the primary template uses
protected inheritance.
Each node of an Instantiator inheritance chain has a field of the
corresponding type in the typelist and lets the user retrieve fields
of subsequent types specified by typelist iterators. It also gives
access to the next node, except that a virtual sentinel follows the
last node; this simplifies defining functions that operate on the
whole structure. Finally, there is the converting constructor from
Arguments for the same typelist.
After this "library" part comes a demonstration of the Arguments and
Instantiator facilities' use, including inductive definition of
functions over the Instantiator structure. The struct TempTest is
just to confirm that the compiler gets temporary lifetime right here.
Please let me know of any improvements you can think of!
Martin
#include <iostream>
#include <numeric>
#include <vector>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/begin_end.hpp>
#include <boost/mpl/next_prior.hpp>
#include <boost/mpl/deref.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/empty.hpp>
#include <boost/mpl/distance.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/static_assert.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/is_convertible.hpp>
namespace mpl = boost::mpl;
namespace tltools {
template<typename Types>
struct last {
typedef typename mpl:

rior<typename mpl::end<Types>::type>::type
type;
};
//-------
namespace detail {
struct Dummy;
typedef const Dummy* DummyPtr;
}
template<typename Types, typename Position = typename
mpl::begin<Types>::type>
struct Arguments
: protected Arguments<Types, typename mpl::next<Position>::type>
{
BOOST_STATIC_ASSERT(!mpl::empty<Types>::value);
typedef typename mpl::next<Position>::type NextPosition;
typedef Arguments<Types, NextPosition> Base;
typedef typename mpl::deref<Position>::type Value;
using Base::get;
Arguments(const Value& value = Value())
: Base(detail:

ummyPtr(0))
{ this->template put<Position>(value); }
Base& operator()(const typename Base::Value& value = typename
Base::Value())
{
this->template put<NextPosition>(value);
return *this;
}
protected:
Arguments(detail:

ummyPtr) : Base(detail:

ummyPtr(0)) {}
};
template<typename Types>
struct Arguments<Types, typename last<Types>::type> {
BOOST_STATIC_ASSERT(!mpl::empty<Types>::value);
typedef typename last<Types>::type Position;
typedef typename mpl::deref<Position>::type Value;
Arguments(const Value& value = Value())
: pointers_(mpl::size<Types>::value)
{ put<Position>(value); }
template<typename Iter>
void put(const typename mpl::deref<Iter>::type& value)
{
pointers_[mpl::distance<
typename mpl::begin<Types>::type, Iter>::value] = &value;
}
template<typename Iter>
const typename mpl::deref<Iter>::type& get() const
{
return *static_cast<const typename mpl::deref<Iter>::type*>
(pointers_[mpl::distance<typename mpl::begin<Types>::type,
Iter>::value]);
}
operator const Arguments<Types>& () const
{ return static_cast<const Arguments<Types>&>(*this); }
protected:
Arguments(detail:

ummyPtr) : pointers_(mpl::size<Types>::value)
{}
private:
std::vector<const void*> pointers_;
};
//-------
template<typename Types, typename Position = typename
mpl::begin<Types>::type>
struct Instantiator : Instantiator<Types, typename
mpl::next<Position>::type> {
BOOST_STATIC_ASSERT(!mpl::empty<Types>::value);
typedef typename mpl::next<Position>::type NextPosition;
typedef Instantiator<Types, NextPosition> Base;
typedef typename mpl::deref<Position>::type Value;
Value value;
Instantiator(const Arguments<Types>& arguments)
: Base(arguments), value(arguments.get<Position>())
{}
template<typename Iter>
const Instantiator<Types, Iter>& get(typename boost::disable_if<
boost::is_same<Iter, Position> >::type* dummy = 0)
{ return Base::template get<Iter>(); }
template<typename Iter>
const Instantiator& get(typename boost::enable_if<
boost::is_same<Iter, Position> >::type* dummy = 0)
{ return *this; }
const Base& next() const { return *this; }
};
typedef detail:

ummyPtr EndOfInstances;
template<typename Types>
struct Instantiator<Types, typename last<Types>::type>
{
BOOST_STATIC_ASSERT(!mpl::empty<Types>::value);
typedef typename last<Types>::type Position;
typedef typename mpl::deref<Position>::type Value;
Value value;
Instantiator(const Arguments<Types>& arguments)
: value(arguments.get<Position>())
{}
template<typename Iter>
const Instantiator& get()
{
BOOST_STATIC_ASSERT((boost::is_same<Iter, Position>::value));
return *this;
}
EndOfInstances next() const { return 0; }
};
} // namespace tltools
//-----------------------------------------------
double sum(double value)
{ return value; }
template<typename T>
double sum(const std::vector<T>& v)
{ return std::accumulate(v.begin(), v.end(), 0.0,
std:

lus<double>()); }
double sum(tltools::EndOfInstances)
{ return 0.0; }
template<typename Types, typename Position>
double sum(const tltools::Instantiator<Types, Position>& instances)
{ return sum(instances.value) + sum(instances.next()); }
template<typename Types>
struct X {
tltools::Instantiator<Types> content;
X(const tltools::Arguments<Types>& arguments) : content(arguments)
{}
virtual double process()
{
typedef typename mpl::begin<Types>::type Begin;
typedef typename mpl::end<Types>::type End;
typedef typename mpl::next<Begin>::type Second;
BOOST_STATIC_ASSERT((!boost::is_same<Second, End>::value));
return sum(content) - sum(content.template get<Second>().value);
}
};
struct TempTest {
int value;
operator int() const { return value; }
TempTest(int value) : value(value) {}
TempTest(const TempTest& other) : value(other.value) {}
~TempTest() { value = 0; }
};
int main() {
typedef std::vector<double> V;
typedef mpl::vector<V, int, int, TempTest> Types;
X<Types> x(tltools::Arguments<Types>(V(3, 2.5))(7)()(1));
std::cout << "Expected 8.5, got " << x.process() << ".\n";
{ char c; std::cin >> c; }
return 0;
}
/* end of code */