error in using template

Discussion in 'C++' started by Jayden Shui, Feb 6, 2012.

  1. Jayden Shui

    Jayden Shui Guest

    Hi All,

    I have a small code to calculate binary operation of two arrays

    #pragma once
    #include <functional>

    template<template<typename T> class Fn, typename T>
    struct Apply
    {
    template<int N>
    void To(T* r, T const* left, T const* right) const
    {
    r[N-1] = Fn<T>()(left[N-1], right[N-1]);
    Apply<Fn, T>().To<N-1>(r, left, right);
    }

    template<>
    void To<0>(T* r, T const* left, T const* right) const
    {}
    };

    int main()
    {
    int a[3], b[3], c[3];
    Apply<std::plus, int>().To<3>(a, b, c); // a = b + c;
    return 0;
    }

    When compilation, I get error:

    error : no instance of function template "Apply<Fn, T>::To" matches
    the specified type.

    Please help me find the bug. Thanks a lot!!

    Jayden
    Jayden Shui, Feb 6, 2012
    #1
    1. Advertising

  2. On 2ÔÂ7ÈÕ, ÉÏÎç12ʱ16·Ö, Jayden Shui <> wrote:
    > Hi All,
    >
    > I have a small code to calculate binary operation of two arrays
    >
    > #pragma once
    > #include <functional>
    >
    > template<template<typename T> class Fn, typename T>
    > struct Apply
    > {
    > template<int N>
    > void To(T* r, T const* left, T const* right) const
    > {
    > r[N-1] = Fn<T>()(left[N-1], right[N-1]);
    > Apply<Fn, T>().To<N-1>(r, left, right);
    > }
    >
    > template<>
    > void To<0>(T* r, T const* left, T const* right) const
    > {}
    >
    > };
    >
    > int main()
    > {
    > int a[3], b[3], c[3];
    > Apply<std::plus, int>().To<3>(a, b, c); // a = b + c;
    > return 0;
    >
    > }
    >
    > When compilation, I get error:
    >
    > error : no instance of function template "Apply<Fn, T>::To" matches
    > the specified type.
    >
    > Please help me find the bug. Thanks a lot!!
    >
    > Jayden




    hi,



    #include <functional>
    template<template<typename T> class Fn, typename T , int N>
    struct Apply
    {
    void To(T* r, T const* left, T const* right) const {
    r[N-1] = Fn<T>()(left[N-1], right[N-1]);
    Apply<Fn, T, N-1>().To(r, left, right);
    }
    };
    template<template<typename T> class Fn, typename T>
    struct Apply<Fn,T,0>
    {
    void To(T* r,T const* left, T const* right) const {}
    };

    int main()
    {
    int a[3], b[3], c[3];
    Apply<std::plus, int, 3>().To(a, b, c); // a = b + c;
    return 0;
    }
    Chenglong Chen, Feb 7, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Theis
    Replies:
    2
    Views:
    453
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    521
    tom_usenet
    Jul 24, 2003
  3. Replies:
    1
    Views:
    2,083
    Gianni Mariani
    Jun 8, 2007
  4. Peng Yu
    Replies:
    3
    Views:
    754
    Thomas J. Gritzan
    Oct 26, 2008
  5. nguillot
    Replies:
    5
    Views:
    514
Loading...

Share This Page