From: Andrei Alexandrescu (andrewalex_at_[hidden])
Date: 2003-05-01 18:55:30


"David Abrahams" <dave_at_[hidden]> wrote in message
news:uvfx18bew.fsf_at_boost-consulting.com...
> BTW, this:
>
> Enforce(cout) <<
> Enforce(auto_ptr(MakeWidget()))->ToString();
>
> Doesn't actually compile, assuming you mean "auto_ptr<Widget>":
>
> "ComeauTest.c", line 6: error: class "std::auto_ptr<Widget>" has no
suitable copy
> constructor

The portion "<Widget>" is there but the folks at CUJ forgot to escape the
less-than sign.

The thing is, you'd need to call get() because auto_ptr doesn't implement
operator!(). So I guess a better implementation of the default Predicate
policy (which currently is return !obj) is to simply say:

return obj ? false : true;

That would still not work with auto_ptr, so what's needed is:

 ENFORCE(std::cerr) <<
    (ENFORCE(std::auto_ptr<Widget>(MakeWidget()).get()))->toString() <<
    std::endl;

If MakeWidget fails, a nice error message is passed:

Expression 'std::auto_ptr<Widget>(makeWidget()).get()' failed in
'/development/tests/enforce/main.cpp', line: 43

which is quite compelling. Of course, when reporting the error to the user
this info doesn't have to appear, but it can be logged instead.

To write some user-defined message:

ENFORCE(std::cout) <<
    (ENFORCE(std::auto_ptr<Widget>(MakeWidget()).get())
    ("The creation of the widget number ")(2)(" failed."))->toString();

For i18n, this should (and could) be improved to accept positional
parameters and string tables.

Andrei