Re: has_member help
On Aug 12, 11:17 am, Vladimir Jovic <vladasp...@gmail.com> wrote:
Stefan van Kessel wrote:
template< class Type >
BigInt operator+( BigInt const& a, Type b )
{
static_cast< BigInt& (BigInt::*)( Type ) >( &BigInt::operator+=
= );
BigInt result = a;
result += b;
return result;
}
Very nice. This is the first time I see something like this :)
So, what exactly happens there?
Is the signature
BigInt& operator+=(unsigned long);
static_cast-ed to this
BigInt& operator+=( Type );
?
Sort of, but not really.
The pointer to a member function, that results from the expression
&BigInt::operator+= is type-cast to a specific type of pointer to
member function, which accepts a Type argument and returns a BigInt
reference.
If the BigInt class does not have an operator+= overload that matches
the signature, then the conversion fails and the source code is ill-
formed (i.e. the compiler must give a diagnostic).
What happens when Type is a type which doesn't have conversion operator
to long or unsigned long? Compile error?
Yes, but also when Type does not match exactly with either long or
unsigned long.
So, you will also get a compile error if Type=int.
Bart v Ingen Schenau