Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

error in using template

12 views
Skip to first unread message

Jayden Shui

unread,
Feb 6, 2012, 11:16:14 AM2/6/12
to
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

Chenglong Chen

unread,
Feb 7, 2012, 4:35:01 AM2/7/12
to
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;
}
0 new messages