473,466 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Template function adaptors

I have a self written Tensor class which I need to write a number of
elementwise operations for (eg sin, cos, abs, conj).

I am trying to implement most of these in terms of standard library
functions. Unfortunately, not all functions in the standard library are
constructed the same (eg some are template functions, some use
pass-by-value where others use pass by reference). To get around this,
I have tried to come up with an adaptor function. The main routine
looks like this:

template <typename T, typename S, S Func(T)>
Tensor<S> ElementwiseTensorOp( const Tensor<T> &subject ) {
Tensor<S> newTensor(subject.dimensions());
size_t n = subject.size();
S *newData = newTensor.data();
T *subjectData = subject.data();
for ( size_t i = 0; i < n; ++i ) newData[i] = Func(subjectData[i]);
}

I have written an adaptor function that converts pass by reference
functions to pass by value, like this:

template <typename T, typename S, S Func(const T&)>
S ArgTypeAdaptor(T arg) {
return Func(arg);
}

Now I try to use this in my Tensor functions, like so:

template <typename T>
Tensor<complex<T> > conj( const Tensor<complex<T> > &v ) {
return
ElementwiseTensorOp<
complex<T>,
complex<T>,
ArgTypeAdaptor<complex<T>, complex<T>, std::conj<T> >
( v );

}

This gives me the following error, which I don't understand:

error: no matching function for call to 'ElementwiseTensorOp(const
Periphery::Tensor<std::complex<double> >&)'

I am using gcc 4.0 on Mac OS X. Can anyone explain this to me? Is there
a better way to handle the argument type discrepancies than what I am
doing (eg STL adaptors)?

Drew McCormack

Sep 7 '05 #1
2 1740
Drew McCormack wrote:
I have a self written Tensor class which I need to write a number of
elementwise operations for (eg sin, cos, abs, conj).

I am trying to implement most of these in terms of standard library
functions. Unfortunately, not all functions in the standard library are
constructed the same (eg some are template functions, some use
pass-by-value where others use pass by reference). To get around this, I
have tried to come up with an adaptor function. The main routine looks
like this:

template <typename T, typename S, S Func(T)>
Tensor<S> ElementwiseTensorOp( const Tensor<T> &subject ) {
Tensor<S> newTensor(subject.dimensions());
size_t n = subject.size();
S *newData = newTensor.data();
T *subjectData = subject.data();
for ( size_t i = 0; i < n; ++i ) newData[i] = Func(subjectData[i]);
}

I have written an adaptor function that converts pass by reference
functions to pass by value, like this:

template <typename T, typename S, S Func(const T&)>
S ArgTypeAdaptor(T arg) {
return Func(arg);
}

Now I try to use this in my Tensor functions, like so:

template <typename T>
Tensor<complex<T> > conj( const Tensor<complex<T> > &v ) {
return
ElementwiseTensorOp<
complex<T>,
complex<T>,
ArgTypeAdaptor<complex<T>, complex<T>, std::conj<T> >
>( v ); }

This gives me the following error, which I don't understand:

error: no matching function for call to 'ElementwiseTensorOp(const
Periphery::Tensor<std::complex<double> >&)'

I am using gcc 4.0 on Mac OS X. Can anyone explain this to me? Is there
a better way to handle the argument type discrepancies than what I am
doing (eg STL adaptors)?


The following code (derived from your code) compiles fine using gcc
4.0.0. Where is your problem ?

#include <complex>

using namespace std;

template <typename T>
struct Tensor
{
Tensor<T> dimensions() const;
size_t size() const;
T * data();
const T * data() const;
};

template <typename T, typename S, S Func(T)>
Tensor<S> ElementwiseTensorOp( const Tensor<T> &subject ) {
Tensor<S> newTensor(subject.dimensions());
size_t n = subject.size();
S *newData = newTensor.data();
const T *subjectData = subject.data(); // XXXXX - fixed this - XXXXX
for ( size_t i = 0; i < n; ++i ) newData[i] = Func(subjectData[i]);
}

template <typename T, typename S, S Func(const T&)>
S ArgTypeAdaptor(T arg) {
return Func(arg);
}

template <typename T>
Tensor<complex<T> > conj( const Tensor<complex<T> > &v ) {
return
ElementwiseTensorOp<
complex<T>,
complex<T>,
ArgTypeAdaptor<complex<T>, complex<T>, std::conj<T> >( v );

}

int main()
{

Tensor< complex< float > > x;

conj( x );
}
Sep 7 '05 #2

On 2005-09-07 15:50:31 +0200, Gianni Mariani <gi*******@mariani.ws> said:
Drew McCormack wrote:
I have a self written Tensor class which I need to write a number of
elementwise operations for (eg sin, cos, abs, conj).

I am trying to implement most of these in terms of standard library
functions. Unfortunately, not all functions in the standard library are
constructed the same (eg some are template functions, some use
pass-by-value where others use pass by reference). To get around this,
I have tried to come up with an adaptor function. The main routine
looks like this:

template <typename T, typename S, S Func(T)>
Tensor<S> ElementwiseTensorOp( const Tensor<T> &subject ) {
Tensor<S> newTensor(subject.dimensions());
size_t n = subject.size();
S *newData = newTensor.data();
T *subjectData = subject.data();
for ( size_t i = 0; i < n; ++i ) newData[i] = Func(subjectData[i]);
}

I have written an adaptor function that converts pass by reference
functions to pass by value, like this:

template <typename T, typename S, S Func(const T&)>
S ArgTypeAdaptor(T arg) {
return Func(arg);
}

Now I try to use this in my Tensor functions, like so:

template <typename T>
Tensor<complex<T> > conj( const Tensor<complex<T> > &v ) {
return
ElementwiseTensorOp<
complex<T>,
complex<T>,
ArgTypeAdaptor<complex<T>, complex<T>, std::conj<T> >
>( v );

}

This gives me the following error, which I don't understand:

error: no matching function for call to 'ElementwiseTensorOp(const
Periphery::Tensor<std::complex<double> >&)'

I am using gcc 4.0 on Mac OS X. Can anyone explain this to me? Is there
a better way to handle the argument type discrepancies than what I am
doing (eg STL adaptors)?


The following code (derived from your code) compiles fine using gcc
4.0.0. Where is your problem ?

#include <complex>

using namespace std;

template <typename T>
struct Tensor
{
Tensor<T> dimensions() const;
size_t size() const;
T * data();
const T * data() const;
};

template <typename T, typename S, S Func(T)>
Tensor<S> ElementwiseTensorOp( const Tensor<T> &subject ) {
Tensor<S> newTensor(subject.dimensions());
size_t n = subject.size();
S *newData = newTensor.data();
const T *subjectData = subject.data(); // XXXXX - fixed this - XXXXX
for ( size_t i = 0; i < n; ++i ) newData[i] = Func(subjectData[i]);
}

template <typename T, typename S, S Func(const T&)>
S ArgTypeAdaptor(T arg) {
return Func(arg);
}

template <typename T>
Tensor<complex<T> > conj( const Tensor<complex<T> > &v ) {
return
ElementwiseTensorOp<
complex<T>,
complex<T>,
ArgTypeAdaptor<complex<T>, complex<T>, std::conj<T> >
>( v );

}

int main()
{

Tensor< complex< float > > x;

conj( x );
}


Thanks for testing this for me Gianni. It has me even more confused
though, because I don't know why it would work for you, but not for me.
I tried compiling your code separately, and that worked for me too. So
obviously there is something tricky going on.

After some more playing, I was able to get the code to compile by
changing the form of the adaptor, like this:

template <typename T, complex<T> Func(const complex<T>&)>
complex<T> ComplexArgTypeAdaptor(complex<T> arg) {
return Func(arg);
}
template <typename T>
Tensor<complex<T> > conj( const Tensor<complex<T> > &v ) {
return ElementwiseTensorOp<complex<T>, complex<T>,
ComplexArgTypeAdaptor<T, std::conj<T> > >( v );
}

At least it works, but I still don't understand.

Thanks again,
Drew

Sep 7 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: CoolPint | last post by:
Can anyone kindly explain why non-type template parameters are required by giving some examples where their uses are clearly favourable to other alternatives? I cannot think of any good use for...
10
by: Matthias | last post by:
Hello, I thought one major advantage of using functors as e.g. sorting predicates over functions would be that I can do something like this: void foo() { class Predicate { public:
4
by: Mark A. Gibbs | last post by:
good day, i'm not sure if i'm using the standard function adaptors correctly. i want to use a member function in a call to transform(), but not a member function of the class being transformed....
8
by: SpOiLeR | last post by:
Hi there... I have something like this: #include <string> #include <list> #include <algorithm> using namespace std; typdef list<string> ls;
10
by: SpOiLeR | last post by:
I have function bool IsGood (const std::string& sr); I want to use that function in std::not1 STL functor. I tried this: not1(IsGood) /* error : 'std::unary_negate<_Fn1> std::not1(const...
5
by: Naveen Mukkelli | last post by:
Hi, I've got two network adaptors in my computer(Windows server 2003). One for public network such as internet, and the other for a private network. I mean, I want to connect other 2 PCs (XP...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
2
by: many_years_after | last post by:
hi, cppers: It is said that member function can be as parameter of STL algorithm. BUT when I pass member function whose parameter is a instance of the class, it dose not work. class Point {...
8
by: Konstantin | last post by:
I have two possible implementations of my class: template <typename T> class MyContainer { public: typedef typename std::set<T>::const_iterator const_iterator; void add( T id ) {...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.