Hi,
besides the more complex possibility to use boost::tuple and generate recursive Tree<X> List : derived from Tree<X-1> ...: derived from Tree<0> specialization, why not using a boost::array instead?
Here an example:
template<class T, size N>
struct Tree
{
typedef boost::array<T, N> parameters_types;
T sum(parameters_type const& p)
{
return std::accumulate(p.begin(), p.end(), 0);
}
};
This is type safe and fast ;) and your users can initialize boost array with an initializer list like:
parameters_type x = { 1, 2, 3, 4, 5... };
Regards,
Ovanes
After some though, here is more precisely what I'd like to have...
I apologize that it is quite different from the initial problem:
template<int n> class Tree {
static double sum(); //
};
If the user instantiates tree<2>, he should get:
template<> class Tree<2> {
static double sum(double d1, double d2);
};
template<> class Tree<3> {
static double sum(double d1, double d2, double d3);
};
etc etc...
so that in user code, for e.g.:
double d= Tree<4>::sum(d1, d2, d3, d4);
should compile.
Is it possible for me to just define the template Tree for the n-case
without the 2- and 3- specializations?
Rds,--
-----Original Message-----
From: boost-users-bounces@lists.boost.org
[mailto:boost-users-bounces@lists.boost.org] On Behalf Of Steven Watanabe
Sent: 28 January 2008 16:42
To: boost-users@lists.boost.org
Subject: Re: [Boost-users] Template instantiation function arguments and
Boost.Preprocessor
AMDG
Hicham Mouline wrote:
> hi,
>
> i have a template function, the arguments of which i wish to have depend
on a non-type int template argument:
>
> template<int n>
> double f<n>( double arg0, double arg1, ..., double argn ) // ... is not
the variadic notation for var number of args
> {
> // for(int i=0; i<n; i++)
> // code
> return sum;
> }
>
> int main()
> {
> return x = f<3>( 1.0, 2.0, 3.0 );
> }
>
> BOOST_PP_REPEAT can't seem to do the job as the template argument n needs
to be known
> at the preprocessing stage, which happens before the template
instantiation stage (
> this is part of compilation)
>
I don't understand why you want n to be passed explicitly. Can't it be
deduced from the
number of arguments (warning untested):
#define SUM_IMPL(z, n, data) + arg ## n
#define F_DEF(z, n, data) double f(BOOST_PP_ENUM_PARAMS_Z(z, n, double
arg)) { return(0.0 BOOST_PP_REPEAT_ ## z(n, SUM_IMPL, ~)); }
BOOST_PP_REPEAT(MAX_ARITY, F_DEF, ~)
In Christ,
Steven Watanabe
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.516 / Virus Database: 269.19.14/1247 - Release Date: 1/28/2008
10:59 AM
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users