Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2023-11-30 19:37:47


Andrey Semashev wrote:
> > That's
> >
> > template<class R, auto Def, auto Del> class resource;
> >
> >> This would also prohibit resource types with non-constexpr constructors,
> >> as well as non-default-constructible resources. I don't like this tradeoff.
> >
> > It doesn't. The two operations required from R are construction from Def
> > and comparing to Def.
> >
> > https://godbolt.org/z/h7z3qGzvs
> >
> > Note that the type of Def doesn't have to be R.
>
> What I meant is something like this won't work:
>
> https://godbolt.org/z/hczoP4WMx

That's a common objection but the fix is simple:

https://godbolt.org/z/KTM9eb5r8

> And if you're suggesting to define some magic placeholder type that can
> be used as Def and automatically converts to R then that looks like a
> more contrived usage to me than just defining resource traits.

That's typically not that hard either, although for std::string specifically
it doesn't work very well because operator!= is templated. So we're hitting
worst case:

constexpr struct invalid_t
{
    operator std::string() const noexcept { return "invalid"; }

    friend bool operator!=( std::string const& s, invalid_t )
    {
        return s != "invalid";
    }
}
invalid;

Most of the time it's much simpler, though.


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