Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2003-12-03 14:18:35


Rob Stewart <stewart_at_[hidden]> writes:

> From: Douglas Paul Gregor <gregod_at_[hidden]>
>>
>> struct some_safe_bool_nested_class {
>> int my_data_member;
>> };
>>
>> typedef int some_safe_bool_nested_class::*safe_bool_type;
>>
>> You don't ever need an instance of some_safe_bool_nested_class.
>
> Here's the context for what you're proposing. It's code that
> Dave posted in an earlier message.
>
> template <class T>
> struct bool_testable
> {
> private:
> struct impl { int x; };
> typedef int impl::*safe_bool;
> public:
> operator safe_bool() const
> {
> return !*static_cast<T const*>(this) ? 0 : &impl::x;
> }
> };
>
> I'm clearly missing something. How can there be an address for
> impl::x if there's no instance of impl?
>
> Were impl::x declared static, there could be an instance --
> though you'd need to define it somewhere -- so you could take its
> address. Lacking that, "impl::x" must refer to something and so
> it must either come from a dummy dm in bool_testable or from the
> derived class (through a template parameter).

A pointer to non-static member exists independently of an object. You
can bind it to an object and dereference it with

    object.*pointer_to_member

HTH,

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

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk