Repeating template parameter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mathieu

    #1

    Repeating template parameter

    Hi there,

    I guess this is a pretty vague question but I stumble on it a couple
    of times and never really knew what was a nice solution to it. I am
    trying to avoid passing twice a template parameter when it could be
    either deduced or simple reused. For instance I have a calculator
    class that take a simple helper function that perform an operation on
    the same type as my calculator,.
    I would write something like this:

    template <typename T>
    struct Helper1
    {
    inline double operator() (T t) { return t; }
    };
    template <typename T>
    struct Helper2
    {
    inline double operator() (T t) { return -t; }
    };

    template <typename T, typename THelper>
    struct Calculator
    {
    static double compute(double t) {
    THelper th;
    return th(t);
    }
    };

    int main()
    {
    Calculator<doub le, Helper1<double c;
    return 0;
    }

    Notice how I write twice 'double' in

    Calculator<doub le, Helper1<double c;

    Thanks for suggestion,
    -Mathieu
  • Ondra Holub

    #2
    Re: Repeating template parameter

    On 10 Pro, 15:32, mathieu <mathieu.malate ...@gmail.comwr ote:
    Hi there,
    >
    I guess this is a pretty vague question but I stumble on it a couple
    of times and never really knew what was a nice solution to it. I am
    trying to avoid passing twice a template parameter when it could be
    either deduced or simple reused. For instance I have a calculator
    class that take a simple helper function that perform an operation on
    the same type as my calculator,.
    I would write something like this:
    >
    template <typename T>
    struct Helper1
    {
    inline double operator() (T t) { return t; }};
    >
    template <typename T>
    struct Helper2
    {
    inline double operator() (T t) { return -t; }
    >
    };
    >
    template <typename T, typename THelper>
    struct Calculator
    {
    static double compute(double t) {
    THelper th;
    return th(t);
    }
    >
    };
    >
    int main()
    {
    Calculator<doub le, Helper1<double c;
    return 0;
    >
    }
    >
    Notice how I write twice 'double' in
    >
    Calculator<doub le, Helper1<double c;
    >
    Thanks for suggestion,
    -Mathieu
    Try this:

    template<
    typename T,
    template<typena me Tclass THelper
    >
    struct Calculator

    Comment

    • Daniel Lidstrom

      #3
      Re: Repeating template parameter

      On 10 Dec, 15:32, mathieu <mathieu.malate ...@gmail.comwr ote:
      template <typename T, typename THelper>
      struct Calculator
      {
      static double compute(double t) {
      THelper th;
      return th(t);
      }
      >
      };
      >
      int main()
      {
      Calculator<doub le, Helper1<double c;
      return 0;
      >
      }
      >
      Notice how I write twice 'double' in
      >
      Calculator<doub le, Helper1<double c;
      If you're using a modern C++ compiler (VC7.1 or newer, gcc 3.4, etc)
      you can use template template parameters. Here's what it will look
      like:

      template <typename T, template<typena meclass THelper>
      struct Calculator
      {
      static double compute(double t) {
      THelper<Tth;
      return th(t);
      }

      };

      int main()
      {
      Calculator<doub le, Helper1c;
      double t = c.compute(5);
      return 0;
      }


      Hope this helps!

      --
      Daniel

      Comment

      • Abhishek Padmanabh

        #4
        Re: Repeating template parameter

        On Dec 10, 7:32 pm, mathieu <mathieu.malate ...@gmail.comwr ote:
        Hi there,
        >
        I guess this is a pretty vague question but I stumble on it a couple
        of times and never really knew what was a nice solution to it. I am
        trying to avoid passing twice a template parameter when it could be
        either deduced or simple reused. For instance I have a calculator
        class that take a simple helper function that perform an operation on
        the same type as my calculator,.
        I would write something like this:
        >
        template <typename T>
        struct Helper1
        {
        inline double operator() (T t) { return t; }};
        >
        template <typename T>
        struct Helper2
        {
        inline double operator() (T t) { return -t; }
        >
        };
        >
        template <typename T, typename THelper>
        struct Calculator
        {
        static double compute(double t) {
        THelper th;
        return th(t);
        }
        >
        };
        >
        int main()
        {
        Calculator<doub le, Helper1<double c;
        return 0;
        >
        }
        >
        Notice how I write twice 'double' in
        >
        Calculator<doub le, Helper1<double c;
        >
        You can make THelper as a template template parameter to the
        calculator template as below:

        template <typename T, template<typena meclass THelper>
        struct Calculator
        {
        static double compute(double t) {
        THelper<Tth;
        return th(t);
        }
        };

        Comment

        • mathieu

          #5
          Re: Repeating template parameter

          On Dec 10, 4:58 pm, Abhishek Padmanabh <abhishek.padma n...@gmail.com>
          wrote:
          On Dec 10, 7:32 pm, mathieu <mathieu.malate ...@gmail.comwr ote:
          >
          >
          >
          Hi there,
          >
          I guess this is a pretty vague question but I stumble on it a couple
          of times and never really knew what was a nice solution to it. I am
          trying to avoid passing twice a template parameter when it could be
          either deduced or simple reused. For instance I have a calculator
          class that take a simple helper function that perform an operation on
          the same type as my calculator,.
          I would write something like this:
          >
          template <typename T>
          struct Helper1
          {
          inline double operator() (T t) { return t; }};
          >
          template <typename T>
          struct Helper2
          {
          inline double operator() (T t) { return -t; }
          >
          };
          >
          template <typename T, typename THelper>
          struct Calculator
          {
          static double compute(double t) {
          THelper th;
          return th(t);
          }
          >
          };
          >
          int main()
          {
          Calculator<doub le, Helper1<double c;
          return 0;
          >
          }
          >
          Notice how I write twice 'double' in
          >
          Calculator<doub le, Helper1<double c;
          >
          You can make THelper as a template template parameter to the
          calculator template as below:
          >
          template <typename T, template<typena meclass THelper>
          struct Calculator
          {
          static double compute(double t) {
          THelper<Tth;
          return th(t);
          }
          >
          };
          Thanks ! I never used template template parameter before :)

          pretty cool indeed !

          -Mathieu

          Comment

          Working...