[Insight-users] operator definition conflicts with ITK?
Luis Ibanez
luis.ibanez at kitware.com
Wed Aug 11 21:38:07 EDT 2004
Kexiang,
You are walking a dangerous path here...
by overloading the comma operator as a global
function you are spilling gasoline in the floor
lightning up a match and waiting for the wind
to blow in the right direction...
Here is what seems to happen with your code:
1) MS VC++ has a header file <xstring> where
a templated class is defined.
2) Your flammable global comma function happens
to match a signature plus a conversion for
one of the methods of a templated class
in <xstring>.
Just for the record,
A) ITK has nothing to do with this problem.
If you replace "itkImage.h" with <string>
you will get the exact same error.
ITK is simply including <string> and therefore
indirectly helping the wind to blow in the right
direction...
B) Funny enough, your flammable code compiles fine
with GCC 3.3
The comma operator is probably the most ambiguous
operator to overload, since when you find it syntax
it will almost always appear to be doing something
else.
For example, have some coffee and tell us what
the following code is doing:
A a;
A b;
callFunction( a, b );
Overloading the comma operator is a delicate and
risky operation,... but Life is Short and there
are so many other things are are worth to spend
time on that it is probably not wise on your part
to waste your valuable time in dealing with these
programming gadgets.
Leave such sophistications to Perl programmers who
can write a web browser in a single line of code,
and enjoy the pleasure of writing code that nobody
else can read. :-)
Follow the ITK way and express yourself freely !!
In ITK we would have called that method without
hesitation:
class ThisIsTheFlammableClassA
{
public:
ThisIsTheFlammableClassA() {}
int OperatorToCombineTwoFlammableObjects(
const ThisIsTheFlammableClassA &)
{
return 1;
}
};
int main()
{
ThisIsTheFlammableClassA flammable1;
ThisIsTheFlammableClassA flammable2;
flammable1.OperatorToCombineTwoFlammableObjects( flammable2 );
}
There is no room for ambiguities when your code is
intended to be used for detecting tumors, planning
radiation treatement, assisting minimally invasive
surgery or monitoring brain aneurysms.
Enjoy ITK !
Luis
----------------------
Kexiang Wang wrote:
> Look at this example
>
> #include "itkImage.h"
> class A
> {
> public:
> A(){}
> friend int operator , (const A&, const A&);
> };
>
> int operator , (const A& a, const A& b)
> {
> return 1;
> }
>
>
> int main()
> {
> return 0;
> }
>
> this error is reported
> d:\microsoft visual studio\vc98\include\xstring(44) : error C2678:
> binary ',' : no operator defined which takes a left-hand operand of
> type 'void' (or there is no acceptable conversion)
> d:\microsoft visual studio\vc98\include\xstring(42) : while compiling
> class-template member function '__thiscall
> std::basic_string<char,struct std::char_traits<char>,class
> std::allocator<char> >::std::basic_string<char,struct std::char_trait
> s<char>,class std::allocator<char> >(const class
> std::basic_string<char,struct std::char_traits<char>,class
> std::allocator<char> > &)'
>
> why can't i define operator , along with ITK?
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users
>
More information about the Insight-users
mailing list