dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Variable Template Params On Function in Template Class

  1. #1
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    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>();
    }
    You're gonna go blind staring into that box all day.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  3. #3
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Re: Variable Template Params On Function in Template Class

    Quote Originally Posted by Lindley View Post
    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?
    You're gonna go blind staring into that box all day.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  5. #5
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Re: Variable Template Params On Function in Template Class

    Quote Originally Posted by Lindley View Post
    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
    You're gonna go blind staring into that box all day.

  6. #6
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Variable Template Params On Function in Template Class

    Quote Originally Posted by Lindley View Post
    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 ...

    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".)

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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...
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)