Repeating template parameter

Discussion in 'C++' started by mathieu, Dec 10, 2007.

  1. mathieu

    mathieu Guest

    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<double, Helper1<double> > c;
    return 0;
    }

    Notice how I write twice 'double' in

    Calculator<double, Helper1<double> > c;

    Thanks for suggestion,
    -Mathieu
     
    mathieu, Dec 10, 2007
    #1
    1. Advertisements

  2. mathieu

    Ondra Holub Guest

    Try this:

    template<
    typename T,
    struct Calculator
     
    Ondra Holub, Dec 10, 2007
    #2
    1. Advertisements

  3. In order to instantiate the local variable 'th' in the member 'compute'
    of 'Calculator', the second argument (as currently written) has to be
    a concrete class, it cannot be a template. With your suggestion, the
    'Calculator::compute' would have to be rewritten as

    static double compute(double t) {
    THelper<T> th;
    return th(t);
    }

    V
     
    Victor Bazarov, Dec 10, 2007
    #3
  4. 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<typename> class THelper>
    struct Calculator
    {
    static double compute(double t) {
    THelper<T> th;
    return th(t);
    }

    };

    int main()
    {
    Calculator<double, Helper1> c;
    double t = c.compute(5);
    return 0;
    }


    Hope this helps!
     
    Daniel Lidstrom, Dec 10, 2007
    #4
  5. You can make THelper as a template template parameter to the
    calculator template as below:

    template <typename T, template<typename> class THelper>
    struct Calculator
    {
    static double compute(double t) {
    THelper<T> th;
    return th(t);
    }
    };
     
    Abhishek Padmanabh, Dec 10, 2007
    #5
  6. mathieu

    mathieu Guest

    Thanks ! I never used template template parameter before :)

    pretty cool indeed !

    -Mathieu
     
    mathieu, Dec 10, 2007
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.