# Variable Template Params On Function in Template Class

• August 3rd, 2010, 07:05 AM
dude_1967
Variable Template Params On Function in Template Class
Hello,

I would like to make a function with a varaiable number of template arguments which is, itself, in a template class.

The code is shown in the sample below.

If you look at the template classes in the sample below, the only difference in each template class specialization "OrderN_Calculation" is the templated subroutine "Calculation". Writing the whole class "OrderN_Calculation" with a ctor, etc is getting tiresome. Is there any way to just write the specialized, templated subroutine and avoid writing the entire class each time?

Code:

```template<const unsigned int N> class OrderN_Calculation { private:   double x; public:   OrderN_Calculation(const double& val = 0.0) : x(val) { }   double Calculation(void); }; template<> class OrderN_Calculation<0u> { private:   double x; public:   OrderN_Calculation(const double& val = 0.0) : x(val) { }   template<const unsigned int C0>   double Calculation(void)   {     return static_cast<double>(C0);   } }; template<> class OrderN_Calculation<1u> { private:   double x; public:   OrderN_Calculation(const double& val) : x(val) { }   template<const unsigned int C0,           const unsigned int C1>   double Calculation(void)   {     return    C0             + (x * C1);   } }; template<> class OrderN_Calculation<2u> { private:   double x; public:   OrderN_Calculation(const double& val) : x(val) { }   template<const unsigned int C0,           const unsigned int C1,           const unsigned int C2>   double Calculation(void)   {     return    C0           +  (x * C1)           + ((x * x) * C2);   } }; int main(int, char**) {   const double d0 = OrderN_Calculation<0u>(1.23).Calculation<2u>();   const double d1 = OrderN_Calculation<1u>(1.23).Calculation<2u, 3u>();   const double d2 = OrderN_Calculation<2u>(1.23).Calculation<2u, 3u, 4u>(); }```
• August 3rd, 2010, 07:13 AM
Lindley
Re: Variable Template Params On Function in Template Class
C++0x includes variadic templates. However, at the moment I believe only g++ supports them----MSVC 10 does not. :(
• August 3rd, 2010, 07:56 AM
dude_1967
Re: Variable Template Params On Function in Template Class
Quote:

Originally Posted by Lindley
C++0x includes variadic templates. However, at the moment I believe only g++ supports them----MSVC 10 does not. :(

Is that how tuples work? Or do tuples use different methods?
• August 3rd, 2010, 08:01 AM
Lindley
Re: Variable Template Params On Function in Template Class
I think for now the <tuple> library just has lots of possible overloads. At least, that's how Boost did it.
• August 3rd, 2010, 09:24 AM
dude_1967
Re: Variable Template Params On Function in Template Class
Quote:

Originally Posted by Lindley
I think for now the <tuple> library just has lots of possible overloads. At least, that's how Boost did it.

Makes sense. Thanks.

Best Regards, Chris
• August 3rd, 2010, 09:53 AM
superbonzo
Re: Variable Template Params On Function in Template Class
Quote:

Originally Posted by Lindley
C++0x includes variadic templates. However, at the moment I believe only g++ supports them----MSVC 10 does not. :(

and probably we will wait ages for it ... :cry: :cry: :cry:

Quote:

Originally Posted by Visual C++ Team Blog
> ... What are the statuses of Variadic Templates and template aliases?

Not implemented in VC10.

> ...

> Any plans for Variadic templates (N2242)?

Implementing variadic templates is my number one request for VC11. I can't promise that I'll get what I want, but I can promise that I'll be loudly campaigning for it.

(Our Standard Library implementation is suffering greatly due to the absence of variadic templates, and I like to say that the Standard Library is the compiler's "first and best customer".)

• August 3rd, 2010, 10:12 AM
monarch_dodra
Re: Variable Template Params On Function in Template Class
I remember reading in the standard (but couldn't find it again, so I only have my word), that Tuples have to be implemented at least up to 10 variables, but any more is implementation defined.

They KNOW that a lot of compilers don't support variadic templates, and rely on copy paste instead...