473,588 Members | 2,452 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer to operator for builtin types

Is there some way to get a function-pointer to the operators of the
builtin types? In the following example I have a generic function which
applies an operator on two values, however it does not work on builtin
types, is there a way to make this work?

struct Test
{
int i;
Test(int i_) : i(i_) { }
Test operator+(Test& t) { return Test(i + t.i); }
Test operator-(Test& t) { return Test(i - t.i); }
operator int() { return i; }
};

template<class T>
T operate(T& t1, T& t2, T (T::*func)(T&))
{
return (t1.*func)(t2);
}
int main() {
Test test1(1);
Test test2(2);
Test test3 = operate(test1, test2, &Test::operator +);
Test test4 = operate(test1, test2, &Test::opera tor-);

/*
int i1 = 1;
int i2 = 2;
int i3 = operate(i1, i2, ???);
int i4 = operate(i1, i2, ???)
*/
}

--
Erik Wikström

Dec 15 '06 #1
6 2548
"Erik Wikström" <er****@student .chalmers.sewro te in message
news:11******** **************@ 80g2000cwy.goog legroups.com
Is there some way to get a function-pointer to the operators of the
builtin types?
No.
In the following example I have a generic function
which applies an operator on two values, however it does not work on
builtin types, is there a way to make this work?
struct Test
{
int i;
Test(int i_) : i(i_) { }
Test operator+(Test& t) { return Test(i + t.i); }
Test operator-(Test& t) { return Test(i - t.i); }
operator int() { return i; }
};
template<class T>
T operate(T& t1, T& t2, T (T::*func)(T&))
{
return (t1.*func)(t2);
}
int main() {
Test test1(1);
Test test2(2);
Test test3 = operate(test1, test2, &Test::operator +);
Test test4 = operate(test1, test2, &Test::opera tor-);

/*
int i1 = 1;
int i2 = 2;
int i3 = operate(i1, i2, ???);
int i4 = operate(i1, i2, ???)
*/
}
Why do you want to call operator+? Why not just use +. This works for both
built-in and class objects. The trick is to make class objects work like
built-in ones, not the reverse.

--
John Carson
Dec 15 '06 #2

Erik Wikström napsal:
Is there some way to get a function-pointer to the operators of the
builtin types? In the following example I have a generic function which
applies an operator on two values, however it does not work on builtin
types, is there a way to make this work?

struct Test
{
int i;
Test(int i_) : i(i_) { }
Test operator+(Test& t) { return Test(i + t.i); }
Test operator-(Test& t) { return Test(i - t.i); }
operator int() { return i; }
};

template<class T>
T operate(T& t1, T& t2, T (T::*func)(T&))
{
return (t1.*func)(t2);
}
int main() {
Test test1(1);
Test test2(2);
Test test3 = operate(test1, test2, &Test::operator +);
Test test4 = operate(test1, test2, &Test::opera tor-);

/*
int i1 = 1;
int i2 = 2;
int i3 = operate(i1, i2, ???);
int i4 = operate(i1, i2, ???)
*/
}

--
Erik Wikström
AFAIK you cannot get pointer to operator+ for built-in types. But you
can wrap such type in some class:

#include <iostream>

struct Test
{
int i;
Test(int i_) : i(i_)
{ }
Test operator+(const Test& t)
{
return Test(i + t.i);
}
Test operator-(const Test& t)
{
return Test(i - t.i);
}
operator int()
{
return i;
}

};

template<typena me T>
class WrapType
{
public:
WrapType(const T& data)
: data_(data)
{
}

T operator+(const T& value)
{
return data_ + value;
}

T operator-(const T& value)
{
return data_ - value;
}

private:
T data_;
};

template<typena me T, typename U>
T operate(const T& t1, const T& t2, T (U::*func)(cons t T&))
{
return (U(t1).*func)(t 2);
}

int main()
{
Test test1(1);
Test test2(2);
Test test3 = operate(test1, test2, &Test::operator +);
Test test4 = operate(test1, test2, &Test::opera tor-);

int i1 = 1;
int i2 = 2;
int i3 = operate(i1, i2, &WrapType<int>: :operator+);
int i4 = operate(i1, i2, &WrapType<int>: :operator-);

std::cout << i3 << " " << i4 << "\n";
}

If you need to call operate from some other template, there is no
problem to use WrapType always:

Test test3 = operate(test1, test2, &Wrap<Test>::op erator+);

Of course, you would need to define also other operators in a WrapType
template.

Dec 15 '06 #3
On Dec 15, 10:51 am, "John Carson" <jcarson_n_o_sp _...@netspace.n et.au>
wrote:
"Erik Wikström" <eri...@student .chalmers.sewro te in messagenews:11* *************** ******@80g2000c wy.googlegroups .com
Is there some way to get a function-pointer to the operators of the
builtin types?

No.
In the following example I have a generic function
which applies an operator on two values, however it does not work on
builtin types, is there a way to make this work?
struct Test
{
int i;
Test(int i_) : i(i_) { }
Test operator+(Test& t) { return Test(i + t.i); }
Test operator-(Test& t) { return Test(i - t.i); }
operator int() { return i; }
};
template<class T>
T operate(T& t1, T& t2, T (T::*func)(T&))
{
return (t1.*func)(t2);
}
int main() {
Test test1(1);
Test test2(2);
Test test3 = operate(test1, test2, &Test::operator +);
Test test4 = operate(test1, test2, &Test::opera tor-);
/*
int i1 = 1;
int i2 = 2;
int i3 = operate(i1, i2, ???);
int i4 = operate(i1, i2, ???)
*/
}

Why do you want to call operator+? Why not just use +. This works for both
built-in and class objects. The trick is to make class objects work like
built-in ones, not the reverse.

Because then I can have a generic function which can operate on any
type performing common operations,such as adding all elements in two
vectors/lists/whatever, and if I instead want another operation
performed I just change the function-pointer passed to the function. So
I can easily add, multiply or whatever.

I suppose that I could use functors, but then I would have to write a
functor for each operation I'd like to perform, which is a bit more
hassle than I'd like. Lambda-function would also have worked I guess.

--
Erik Wikström

Dec 15 '06 #4

Erik Wikström napsal:
On Dec 15, 10:51 am, "John Carson" <jcarson_n_o_sp _...@netspace.n et.au>
wrote:
"Erik Wikström" <eri...@student .chalmers.sewro te in messagenews:11* *************** ******@80g2000c wy.googlegroups .com
Is there some way to get a function-pointer to the operators of the
builtin types?
No.
In the following example I have a generic function
which applies an operator on two values, however it does not work on
builtin types, is there a way to make this work?
struct Test
{
int i;
Test(int i_) : i(i_) { }
Test operator+(Test& t) { return Test(i + t.i); }
Test operator-(Test& t) { return Test(i - t.i); }
operator int() { return i; }
};
template<class T>
T operate(T& t1, T& t2, T (T::*func)(T&))
{
return (t1.*func)(t2);
}
int main() {
Test test1(1);
Test test2(2);
Test test3 = operate(test1, test2, &Test::operator +);
Test test4 = operate(test1, test2, &Test::opera tor-);
/*
int i1 = 1;
int i2 = 2;
int i3 = operate(i1, i2, ???);
int i4 = operate(i1, i2, ???)
*/
}
Why do you want to call operator+? Why not just use +. This works for both
built-in and class objects. The trick is to make class objects work like
built-in ones, not the reverse.


Because then I can have a generic function which can operate on any
type performing common operations,such as adding all elements in two
vectors/lists/whatever, and if I instead want another operation
performed I just change the function-pointer passed to the function. So
I can easily add, multiply or whatever.

I suppose that I could use functors, but then I would have to write a
functor for each operation I'd like to perform, which is a bit more
hassle than I'd like. Lambda-function would also have worked I guess.

--
Erik Wikström
You do not have to write functors for all operations, they are already
written. You can redefine operate this way:

template<typena me T, typename F>
T operate2(const T& t1, const T& t2, const F& f)
{
return f(t1, t2);
}

And then use it so:

int i5 = operate2(i1, i2, std::plus<int>( ));

Test test5 = operate2(test1, test2, std::plus<Test> ());

One more headeris necessary: #include <functional>

Dec 15 '06 #5
"Erik Wikström" <er****@student .chalmers.sewro te in message
news:11******** **************@ 16g2000cwy.goog legroups.com
On Dec 15, 10:51 am, "John Carson"
<jcarson_n_o_sp _...@netspace.n et.au>
wrote:
>Why do you want to call operator+? Why not just use +. This works
for both
built-in and class objects. The trick is to make class objects work
like
built-in ones, not the reverse.


Because then I can have a generic function which can operate on any
type performing common operations,such as adding all elements in two
vectors/lists/whatever, and if I instead want another operation
performed I just change the function-pointer passed to the function.
So
I can easily add, multiply or whatever.

I suppose that I could use functors, but then I would have to write a
functor for each operation I'd like to perform, which is a bit more
hassle than I'd like. Lambda-function would also have worked I guess.

Have a look at the function objects available in the functional header.
These include templates for plus, minus and various other things.

--
John Carson
Dec 15 '06 #6
On Dec 15, 11:30 am, "Ondra Holub" <ondra.ho...@po st.czwrote:
Erik Wikström napsal:
On Dec 15, 10:51 am, "John Carson" <jcarson_n_o_sp _...@netspace.n et.au>
Why do you want to call operator+? Why not just use +. This works forboth
built-in and class objects. The trick is to make class objects work like
built-in ones, not the reverse.
Because then I can have a generic function which can operate on any
type performing common operations,such as adding all elements in two
vectors/lists/whatever, and if I instead want another operation
performed I just change the function-pointer passed to the function. So
I can easily add, multiply or whatever.
I suppose that I could use functors, but then I would have to write a
functor for each operation I'd like to perform, which is a bit more
hassle than I'd like. Lambda-function would also have worked I guess.

You do not have to write functors for all operations, they are already
written. You can redefine operate this way:

template<typena me T, typename F>
T operate2(const T& t1, const T& t2, const F& f)
{
return f(t1, t2);

}

And then use it so:

int i5 = operate2(i1, i2, std::plus<int>( ));

Test test5 = operate2(test1, test2, std::plus<Test> ());

One more headeris necessary: #include <functional>
Thank you, and John Carson, this solves my problem quite nicely.

--
Erik Wikström

Dec 15 '06 #7

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

Similar topics

2
5151
by: BJörn Lindqvist | last post by:
A problem I have occured recently is that I want to subclass builtin types. Especially subclassing list is very troublesome to me. But I can't find the right syntax to use. Take for example this class which is supposed to be a representation of a genome: class Genome(list): def __init__(self): list.__init__(self) self = ....
0
1537
by: Mike Leahy | last post by:
Okay.I'm following the documentation that came with the PostgreSQL source code (located in /usr/doc/postgresql-7.3.4-2/html/arrays.hmtl in my cygwin root). I created have a table with a varchar array in it, kind of like the following: CREATE TABLE tbl_db_usuario_detalles (NOMBRE varchar(50), COD_USE varchar(6));
9
4932
by: Koster | last post by:
Something I've been puzzled over for a while. This really has no consequence at all, but I'm curious for the _reason_ behind where we commonly place the asterisk when declaring pointer variables. When defining a variable which is of pointer-to-foo type, it seems the 'convention' is to put the asterisk before the variable's identifier, like this: foo *ptr; The C standards all seem to use this conventions, but it is equally valid (AFAIK)...
7
8660
by: Michael Birkmose | last post by:
Hi everyone!, Are pointers to incomplete types allowed in ANSI C? I can see that pointer arithmic on pointers to incomple types is impossible, however there are situations where it can be useful: consider this:
8
1579
by: Alef.Veld | last post by:
Just wondering, I sometimes use or see &ptr when passing it through a function so that this specific function works with the actual pointer passed. But if we just allocate memory to this pointer before, and then pass it as func(ptr) instead of func(&ptr), what's the use of the & operator ? Or is this just extra functionality of c. I can ofcourse imagine that you would want to allocate memory within the function itself, in which...
4
1539
by: George Exarchakos | last post by:
Hi there, this is, I guess, very easy question but I cannot find a specific well documented answer. When I use: int *x = new int(8); it obviously allocates memory for a new int. But do I have to explicitly deallocate (delete) it as with any object dynamically
0
7929
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7862
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
8228
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...
0
8357
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7987
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,...
1
5729
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
5398
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1196
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.