473,587 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ 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> ElementwiseTens orOp( const Tensor<T> &subject ) {
Tensor<S> newTensor(subje ct.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(subjectDat a[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
ElementwiseTens orOp<
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 'ElementwiseTen sorOp(const
Periphery::Tens or<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 1763
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> ElementwiseTens orOp( const Tensor<T> &subject ) {
Tensor<S> newTensor(subje ct.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(subjectDat a[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
ElementwiseTens orOp<
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 'ElementwiseTen sorOp(const
Periphery::Tens or<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> ElementwiseTens orOp( const Tensor<T> &subject ) {
Tensor<S> newTensor(subje ct.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(subjectDat a[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
ElementwiseTens orOp<
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*******@mari ani.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> ElementwiseTens orOp( const Tensor<T> &subject ) {
Tensor<S> newTensor(subje ct.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(subjectDat a[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
ElementwiseTens orOp<
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 'ElementwiseTen sorOp(const
Periphery::Tens or<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> ElementwiseTens orOp( const Tensor<T> &subject ) {
Tensor<S> newTensor(subje ct.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(subjectDat a[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
ElementwiseTens orOp<
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> ComplexArgTypeA daptor(complex< T> arg) {
return Func(arg);
}
template <typename T>
Tensor<complex< T> > conj( const Tensor<complex< T> > &v ) {
return ElementwiseTens orOp<complex<T> , complex<T>,
ComplexArgTypeA daptor<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
3008
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 them except to create different sizes of static arrays from a template, but this could be done by creating a dynamic array of different sizes...
10
7317
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
1323
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. i have: class B {}; class C {};
8
1655
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
4645
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 _Fn1 &)' : could not deduce template argument for 'overloaded function type' from 'overloaded
5
1872
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 Pro with SP3) to this adaptor and create a private LAN. Now I want to run my server application with the host IP of the second(private
272
13984
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 dimensional arrays from std::vectors ??? I want to use normal Array Syntax.
2
2413
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 { public: int x; int y;
8
287
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 ) { data.insert(id); } void remove( T id ) { data.erase(id); } private:
0
7923
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7974
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5395
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3845
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.