 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Erik Wikström Guest
|
Posted: Fri Dec 15, 2006 10:11 am Post subject: 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 |
|
| Back to top |
|
 |
Ondra Holub Guest
|
Posted: Fri Dec 15, 2006 10:11 am Post subject: Re: Pointer to operator for builtin types |
|
|
Erik Wikström napsal:
| Quote: | 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. |
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Fri Dec 15, 2006 10:11 am Post subject: Re: Pointer to operator for builtin types |
|
|
"Erik Wikström" <eriwik (AT) student (DOT) chalmers.se> wrote in message
news:1166174015.343152.236160 (AT) 80g2000cwy (DOT) googlegroups.com
| Quote: | Is there some way to get a function-pointer to the operators of the
builtin types?
|
No.
| Quote: | 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 |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|