error in using template

J

Jayden Shui

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
 
C

Chenglong Chen

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;
}
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top