Boost logo

Boost :

From: Angus Leeming (angus.leeming_at_[hidden])
Date: 2005-01-21 11:43:57


Jason Hise wrote:

> Darren Cook wrote:
>
>>>> Heh. Have you read what Scott Meyers has to say about this?
>>>
>>>
>>> Indeed I have not. Where could I find what he has to say about this?
>>
>>
>> Item 25 of Effective C++. Definitely worth reading all three of his
>> Effective books. Not just lots of useful advice but also a pleasure to
>> read.
>>
> Looks like I'll be buying them then :)
> In the meantime, since I'm anxious to know, may I ask whether what he
> had to say about such a null was positive or negative?
>
> -Jason

You had:

class null {
public:
    explicit null (void *) {}
    ~null () {}
    template <typename T>
    operator T*() const { return 0; }
};

Meyers has:

const class { // This is a const object
public:
    // convertible to any type of null non-member pointer...
    template <typename T>
    operator T*() const { return 0; }

    // ...or any type of null member-pointer...
    template <typename C, typename T>
    operator T C::*() const { return 0; }
private:
    // whose address can't be taken...
    void operator&() const;

} NULL; // and whose name is NULL.

Getting there takes three pages of explanations of the pitfalls and the
bottom line conclusion is that whilst this is a workable NULL, you still
can't prevent your uses from writing code that results in the problems
that led you to try and write this space age solution in the first place.

    void f(int);
    void f(string *);

    f(0); // which function is called?

"As a designer of overloaded functions, then, the bottom line is that
you're best off avoiding overloading on a numerical and a pointer type if
you can possibly avoid it."

Ie, your solution works, but its solving the wrong problem.

Now go buy the book ;-)
Angus


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