473,395 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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::operator-);

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

--
Erik Wikström

Dec 15 '06 #1
6 2534
"Erik Wikström" <er****@student.chalmers.sewrote in message
news:11**********************@80g2000cwy.googlegro ups.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::operator-);

/*
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::operator-);

/*
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<typename 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<typename T, typename U>
T operate(const T& t1, const T& t2, T (U::*func)(const T&))
{
return (U(t1).*func)(t2);
}

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

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>::operator+);

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.net.au>
wrote:
"Erik Wikström" <eri...@student.chalmers.sewrote in messagenews:11**********************@80g2000cwy.go oglegroups.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::operator-);
/*
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.net.au>
wrote:
"Erik Wikström" <eri...@student.chalmers.sewrote in messagenews:11**********************@80g2000cwy.go oglegroups.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::operator-);
/*
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<typename 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.sewrote in message
news:11**********************@16g2000cwy.googlegro ups.com
On Dec 15, 10:51 am, "John Carson"
<jcarson_n_o_sp_...@netspace.net.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...@post.czwrote:
Erik Wikström napsal:
On Dec 15, 10:51 am, "John Carson" <jcarson_n_o_sp_...@netspace.net.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<typename 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
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...
0
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...
9
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....
7
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...
8
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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,...
0
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...

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.