Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2005-02-15 23:29:06


Ben Hutchings <ben.hutchings_at_[hidden]> writes:

> David Abrahams wrote:
>> Ben Hutchings <ben.hutchings_at_[hidden]> writes:
>>
>>>Actually it can:
>>>
>>> struct D : C { void foo(std::string); };
>>>
>>> mem_fun mf = static_cast<mem_fun>(&D::foo);
>>>
>>>Not that I'd recommend doing that, as it invites type errors.
>> Creating an instance by breaking the type system is cheating.
>
> That doesn't break the type system. The result is well-defined; see
> 5.2.9/9 in the standard.

If class B contains the original member, or is a base or derived class
of the class containing the original member, the resulting pointer to
member points to the original member. Otherwise, the result of the
cast is undefined. [Note: although class B need not contain the
original member, the dynamic type of the object on which the pointer
to member is dereferenced must contain the original member; see
expr.mptr.oper. ]

That part in brackets contradicts you in this case. But then, it's
non-normativve and the section it references seems to actually only
apply when the pointer is dereferenced. I'm slightly confused.

>> Why not reinterpret_cast<mem_fun>("foo"), for that matter?
>
> That's ill-formed.

Where is that in the standard? I see no restrictions on the use of
reinterpret_cast.

>> A simpler instance of the type is:
>> mem_fun mf = 0;
>
> Yes, and as you know that's what the type trait relies on.

No, I didn't.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net