Boost logo

Boost Users :

From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-01-28 10:41:43


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 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