From: Vladimir Grigoriev on
I do that only for my experiance.

Vladimir Grigoriev

"Bo Persson" <bop(a)gmb.dk> wrote in message
news:7ops3gF3qi946U1(a)mid.individual.net...
> Vladimir Grigoriev wrote:
>> I am sorry. The initial code is nor complete. I have found that the
>> error occurs when I add the following template operator
>>
>> template <typename T>
>>
>> inline const T operator -( const T &lhs, const T &rhs )
>>
>> {
>>
>> return ( T( lhs ) -= rhs );
>>
>> }
>>
>
> This defines an operator- for ANY type. Why do you want to do that?
>
>
>
> Bo Persson
>
>


From: Vladimir Grigoriev on
So the question now looks the following way: Why is the user template
operator-() used instead of defined in the class _Vector_iterator member
operator
difference_type operator-(const _Mybase& _Right) const

{ // return difference of iterators

return (*(_Mybase *)this - _Right);

}



Vladimir Grigoriev


From: Vladimir Grigoriev on
I have simulated the situation

#include "stdafx.h"

#include <iostream>



template <typename T>

inline const T operator -( const T &lhs, const T &rhs )

{

// return ( T( lhs ) -= rhs );

std::cout << "template operator - is used.\n";

return ( T() );

}

class A

{

public:

size_t operator -( const A &rhs ) const

{

std::cout << "A::operator - is used.\n";



return ( 0 );

}

};

class B: public A

{

public:

size_t operator -( const A &rhs ) const

{

std::cout << "B::operator - is used.\n";

return ( 1 );

}

};


int _tmain(int argc, _TCHAR* argv[])

{




B b1, b2;


b1 - b2;


return 0;

}



The output is

template operator - is used.
Press any key to continue . . .



Vladimir Grigoriev


From: Victor Bazarov on
Vladimir Grigoriev wrote:
> I have simulated the situation
>
> #include "stdafx.h"
> #include <iostream>
>
> template <typename T>
> inline const T operator -( const T &lhs, const T &rhs )
> {
> // return ( T( lhs ) -= rhs );
> std::cout << "template operator - is used.\n";
> return ( T() );
> }
>
> class A
> {
> public:
> size_t operator -( const A &rhs ) const
> {
> std::cout << "A::operator - is used.\n";
> return ( 0 );
> }
> };
> class B: public A
> {
> public:
> size_t operator -( const A &rhs ) const
> {
> std::cout << "B::operator - is used.\n";
> return ( 1 );
> }
> };
>
> int _tmain(int argc, _TCHAR* argv[])
> {
> B b1, b2;
> b1 - b2;
> return 0;
> }
>
>
>
> The output is
>
> template operator - is used.
> Press any key to continue . . .

Well, of course! The template ::op- has the two operands that are of
the same type. The B::op- requires conversion (albeit standard) for the
second operand, and the A::op- requires conversion for both operands.
Of course the function that does NOT require conversions (only reference
binding) is picked -- its rank is higher for overload resolution.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
From: Igor Tandetnik on
Vladimir Grigoriev wrote:
> No, you are mistaken! The problem is in Microsoft VC++ code. It breaks the
> main principle of OOP that a server code should not depend on a client code.

This principle cannot be readily applied to STL - all it does is manipulate user-provided classes and naturally depends on their behavior. However, the implementation should avoid _unintended_ points of customization. Unfortunately, C++ makes it difficult to do so.

For discussion of a similar set of issues, see

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#225
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#226
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#229
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1387.htm

These defect reports attempt to plug a particular hole - that of picking up unintended user-defined functions by Koenig lookup. Your case doesn't involve Koenig lookup, so it's different in the letter though similar in spirit. It's not clear to me whether VC2005's implementation is outright non-conforming, but from a quality-of-implementation standpoint it could certainly be improved (which, apparently, it was in later versions).

Having said that, defining a generally-applicable templated overloaded operator in global scope is probably not the best idea. It's too easy to pick it up by accident.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925