473,725 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template function type for arithmetic operators for elementary types

Greetings,

I am trying to implement "element-wise" arithmetic operators for a class
along the following lines (this is a simplified example):

// ----- BEGIN CODE -----

struct X
{
int a,b;

typedef int& optype(int&,int ); // ???

template<optype OP> void element_wise(X& x)
{
OP(a,x.a);
OP(b,x.b);
}

X& operator+=(X& x)
{
element_wise< ::operator+=<in t> >(x); // ??? (line 15)
return *this;
}
};

int main()
{
X x,y;

x.a = 1;
x.b = 2;
y.a = 3;
y.b = 4;

x += y;

return 0;
}

// ----- END CODE -----

I get the error (gcc):

test.cpp: In member function `X& X::operator+=(X &)':
test.cpp:15: error: no matching function for call to
`X::element_wis e(X&)'

This is perhaps not surprising, in view of that:

// ----- BEGIN CODE -----

int main()
{
int a = 1;

operator += (a,1);

return 0;
}
// ----- END CODE -----

fails to compile with "error: `operator+=' not defined" (this surprised
me somewhat). It seems that function-style prototypes for arithmetic
operators on elementary types don't exist.

So is it at all possible to use arithmetic operators for elementary
types as template arguments? A simple workaround is to wrap the
operators in a function, but it would be neater (and conceivably more
efficient) not to have to do this.

Regards,

--
Lionel B
Jul 23 '05 #1
5 2843
Lionel B wrote:
[...]
So is it at all possible to use arithmetic operators for elementary
types as template arguments? A simple workaround is to wrap the
operators in a function, but it would be neater (and conceivably more
efficient) not to have to do this.


Take a look at the library implementation of 'std::plus' and other
arithmetic functors. They are defined in <functional> and described
in 20.3.2. My guess is you should simply use them and/or provide your
own of similar form.

V
Jul 23 '05 #2
Lionel B wrote:
Greetings,

I am trying to implement "element-wise" arithmetic operators for a class
along the following lines (this is a simplified example):

// ----- BEGIN CODE -----

struct X
{
int a,b;

typedef int& optype(int&,int ); // ???

template<optype OP> void element_wise(X& x)
{
OP(a,x.a);
OP(b,x.b);
}

X& operator+=(X& x)
{
element_wise< ::operator+=<in t> >(x); // ??? (line 15)
return *this;
}
};
Not all compilers will support this yet because of the template-template
parameter. This compiled on GCC 4.0(snapshot).

template < template <typename Tf> class F, typename T >
void Do( T & res, const T& val )
{
F<T>::DoThing( res, val );
}


template <typename Tf>
struct PlusEqual
{
static void DoThing( Tf & res, const Tf & val )
{
res += val;
}
};
template <typename Tf>
struct MinusEqual
{
static void DoThing( Tf & res, const Tf & val )
{
res -= val;
}
};
struct X
{
float a;
int b;
template < template <typename Tf> class F >
void ElementWise( const X & rhs )
{
Do<F>( a, rhs.a );
Do<F>( b, rhs.b );
}
X& operator+=( const X& rhs )
{
ElementWise<Plu sEqual>( rhs );
return * this;
}
X& operator-=( const X& rhs )
{
ElementWise<Plu sEqual>( rhs );
return * this;
}
};

I don't know how useful this is since it's limited in utility, however I
think that does what you were trying to do. In the example you posted,
there were numerous issues, I won't go into the details other than to
say, you can't pass a function as a template parameter in your case
since you don't know what the type of the parameters are, hence you can
pass a template as the template parameter.

int main()
{
X x,y;

x.a = 1;
x.b = 2;
y.a = 3;
y.b = 4;

x += y;

return 0;
}
This should now compile.

....
// ----- BEGIN CODE -----

int main()
{
int a = 1;

operator += (a,1);
You're invoking global operator += on two ints:

"ComeauTest .c", line 1: error: nonmember operator requires a parameter
with class
or enum type
int operator +=( int &, int );

You can't override the built-in global operators, but you can create
your own types and define new global operators.

return 0;
}
// ----- END CODE -----

fails to compile with "error: `operator+=' not defined" (this surprised
me somewhat). It seems that function-style prototypes for arithmetic
operators on elementary types don't exist.

So is it at all possible to use arithmetic operators for elementary
types as template arguments? A simple workaround is to wrap the
operators in a function, but it would be neater (and conceivably more
efficient) not to have to do this.


Most compilers I have used will optimize the function calls away and the
efficiency become a non-issue.
Jul 23 '05 #3
Gianni Mariani wrote:

Correction:

....

template <typename Tf>
struct MinusEqual
{
static void DoThing( Tf & res, const Tf & val )
{
res -= val;
}
};
struct X
{
float a;
int b;
....


X& operator-=( const X& rhs )
{
ElementWise<Plu sEqual>( rhs );
Should be:
ElementWise<Min usEqual>( rhs );
return * this;
}
};

Jul 23 '05 #4
"Gianni Mariani" <gi*******@mari ani.ws> wrote in message
news:Y9******** ************@sp eakeasy.net...
Lionel B wrote:
Greetings,

I am trying to implement "element-wise" arithmetic operators for a class along the following lines (this is a simplified example):

// ----- BEGIN CODE -----

struct X
{
int a,b;

typedef int& optype(int&,int ); // ???

template<optype OP> void element_wise(X& x)
{
OP(a,x.a);
OP(b,x.b);
}

X& operator+=(X& x)
{
element_wise< ::operator+=<in t> >(x); // ??? (line 15)
return *this;
}
};
Not all compilers will support this yet because of the

template-template parameter. This compiled on GCC 4.0(snapshot).

/snip code/
Cheers, yes that works for my gcc 3.3.3 too.
I don't know how useful this is since it's limited in utility, however I think that does what you were trying to do.
It does. BTW, the reason for what I was trying to do is simply to avoid
code duplication: my "real" ElementWise() function is rather lengthy and
complex, and needs to be parametrised by operator type (plus, minus,
etc.).

I suspect the "functional " approach you give above is probably
uneccessarily sophisticated for my needs... the following works
perfectly well for me.

typedef void optype(int&, const int&);

void plus_equal(int& a, int b)
{
a += b;
}

struct X
{
int a,b;

template<optype OP> void element_wise(X& x)
{
OP(a,x.a);
OP(b,x.b);
}

X& operator+=(X& x)
{
element_wise<pl us_equal>(x);
return *this;
}
};
In the example you posted,
there were numerous issues, I won't go into the details other than to
say, you can't pass a function as a template parameter in your case
since you don't know what the type of the parameters are
Not sure I follow you... I *do* know the parameter types of the function
I want to pass as template parameter: namely (int&, const int), as
above. My issue was whether I could pass the global operator+= function
as a parameter to my to element_wise() ...
/snip/
// ----- BEGIN CODE -----

int main()
{
int a = 1;

operator += (a,1);


You're invoking global operator += on two ints:

"ComeauTest .c", line 1: error: nonmember operator requires a parameter
with class
or enum type
int operator +=( int &, int );

You can't override the built-in global operators, but you can create
your own types and define new global operators.


Yup - that's my solution above.
So is it at all possible to use arithmetic operators for elementary
types as template arguments?
So... no.
A simple workaround is to wrap the
operators in a function, but it would be neater (and conceivably more efficient) not to have to do this.


Most compilers I have used will optimize the function calls away and

the efficiency become a non-issue.


No doubt.

Thanks,
Jul 23 '05 #5
Lionel B wrote:

....
I
think that does what you were trying to do.

It does. BTW, the reason for what I was trying to do is simply to avoid
code duplication: my "real" ElementWise() function is rather lengthy and
complex, and needs to be parametrised by operator type (plus, minus,
etc.).

I suspect the "functional " approach you give above is probably
uneccessarily sophisticated for my needs... the following works
perfectly well for me.

typedef void optype(int&, const int&);

void plus_equal(int& a, int b)
{
a += b;
}

struct X
{
int a,b;

template<optype OP> void element_wise(X& x)
{
OP(a,x.a);
OP(b,x.b);
}

X& operator+=(X& x)
{
element_wise<pl us_equal>(x);
return *this;
}
};

If all you need is int support, you're OK, the solution I posted will
support any type.

In the example you posted,
there were numerous issues, I won't go into the details other than to
say, you can't pass a function as a template parameter in your case
since you don't know what the type of the parameters are

Not sure I follow you... I *do* know the parameter types of the function
I want to pass as template parameter: namely (int&, const int), as
above. My issue was whether I could pass the global operator+= function
as a parameter to my to element_wise() ...


OK - then you're all set.

I thought you were looking for a more generic solution. Good luck.

Jul 23 '05 #6

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

Similar topics

8
2241
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions. The reference should be 3.9.1 (Fundamental types), and 4.7 (Integral conversions). It seems to me that the Standard doesn't specify: 1) The "value representation" of any of these types, except that (3.9.1/3) "... The range of nonnegative...
9
2316
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the same type for a given instance of the class, but different types for different instances.) #include<set> using namespace std; template<class T>
5
3035
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a templatized conversion operator defined as follows:
4
3101
by: Dan Krantz | last post by:
I have the following template to ensure that a given number (val) falls into a range (between vmin & vmax): template<typename T> T ForceNumericRange( const T& val, const T& vmin, const T& vmax) { T retVal = val; if ( retVal < vmin ) retVal = vmin;
19
7928
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a macro: #define min(a,b) ((a<b)?a:b) I thought that another way could be to use a template function: template <class T> T min<T a, T b>
3
1586
by: =?gb2312?B?wfXquw==?= | last post by:
Hi, folks, I'm running into a question as below: template<typename T> class A { private: T _a; public: A(T t): _a(t) { }
4
1566
by: Stephan Rose | last post by:
Hi everyone, I have a little problem I'm not sure what to do about or if anything can even be done about it. Situation is as follows: I had previous implemented a class called Scalar that is essentially a wrapper around double that contains an additional field to store what unit type is represented by this double.
9
2202
by: neildferguson | last post by:
I am using templates with a little project I am working on. My compiler (GCC) is finding a particular construct ambiguous. Can anyone suggest something I might change in the declaration of class Length so that I can use operator+ the way I'd like? //========================================= // File lentest.h: #ifndef FDIMENS_LENGTH_INCL #define FDIMENS_LENGTH_INCL
36
3386
by: Julienne Walker | last post by:
Ignoring implementation details and strictly following the C99 standard in terms of semantics, is there anything fundamentally flawed with describing the use of a (non-inline) function as an address? I keep feeling like I'm missing something obvious. -Jul To keep things in context, this is in reference to describing functions to a beginner.
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9174
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9111
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4517
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
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 we have to send another system
3
2157
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.