COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Pointer to operator for builtin types

• Email
• Follow

```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 =3D operate(test1, test2, &Test::operator+);
Test test4 =3D operate(test1, test2, &Test::operator-);

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

--
Erik Wikstr=F6m

```
 0
Reply eriwik (511) 12/15/2006 9:13:35 AM

See related articles to this posting

```"Erik Wikstr�m" <eriwik@student.chalmers.se> wrote in message
> 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

```
 0
Reply jcarson_n_o_sp_am_ (646) 12/15/2006 9:51:26 AM

```Erik Wikstr=F6m 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 =3D operate(test1, test2, &Test::operator+);
> 	Test test4 =3D operate(test1, test2, &Test::operator-);
>
> 	/*
> 	int i1 =3D 1;
> 	int i2 =3D 2;
> 	int i3 =3D operate(i1, i2, ???);
> 	int i4 =3D operate(i1, i2, ???)
> 	*/
> }
>
> --
> Erik Wikstr=F6m

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 =3D operate(test1, test2, &Test::operator+);
Test test4 =3D operate(test1, test2, &Test::operator-);

int i1 =3D 1;
int i2 =3D 2;
int i3 =3D operate(i1, i2, &WrapType<int>::operator+);
int i4 =3D 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 =3D operate(test1, test2, &Wrap<Test>::operator+);

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

```
 0
Reply ondra.holub (260) 12/15/2006 10:08:30 AM

```On Dec 15, 10:51 am, "John Carson" <jcarson_n_o_sp_...@netspace.net.au>
wrote:
> "Erik Wikstr=F6m" <eri...@student.chalmers.se> wrote in messagenews:11661=
>
> > 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 =3D operate(test1, test2, &Test::operator+);
> > Test test4 =3D operate(test1, test2, &Test::operator-);
>
> > /*
> > int i1 =3D 1;
> > int i2 =3D 2;
> > int i3 =3D operate(i1, i2, ???);
> > int i4 =3D 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=F6m

```
 0
Reply eriwik (511) 12/15/2006 10:11:27 AM

```Erik Wikstr=F6m napsal:
> On Dec 15, 10:51 am, "John Carson" <jcarson_n_o_sp_...@netspace.net.au>
> wrote:
> > "Erik Wikstr=F6m" <eri...@student.chalmers.se> wrote in messagenews:116=
> >
> > > 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 =3D operate(test1, test2, &Test::operator+);
> > > Test test4 =3D operate(test1, test2, &Test::operator-);
> >
> > > /*
> > > int i1 =3D 1;
> > > int i2 =3D 2;
> > > int i3 =3D operate(i1, i2, ???);
> > > int i4 =3D operate(i1, i2, ???)
> > > */
> > > }
> >
> > Why do you want to call operator+? Why not just use +. This works for b=
oth
> > 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=F6m

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 =3D operate2(i1, i2, std::plus<int>());

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

One more headeris necessary: #include <functional>

```
 0
Reply ondra.holub (260) 12/15/2006 10:30:39 AM

```"Erik Wikstr�m" <eriwik@student.chalmers.se> wrote in message
> 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

```
 0
Reply jcarson_n_o_sp_am_ (646) 12/15/2006 10:31:34 AM

```On Dec 15, 11:30 am, "Ondra Holub" <ondra.ho...@post.cz> wrote:
> Erik Wikstr=F6m 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 for=
both
> > > built-in and class objects. The trick is to make class objects work l=
ike
> > > 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 =3D operate2(i1, i2, std::plus<int>());
>
>     Test test5 =3D 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=F6m

```
 0
Reply eriwik (511) 12/15/2006 11:39:51 AM

6 Replies
33 Views

Similar Articles

12/7/2013 7:50:27 PM
page loaded in 57346 ms. (0)