https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107525
Bug ID: 107525
Summary: propagate_const should not be using SFINAE on its
conversion operators
Product: gcc
Version: 12.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: dangelog at gmail dot com
Target Milestone: ---
propagate_const in the LFTSv3 has implicit conversion operators which have
constraints on them:
https://cplusplus.github.io/fundamentals-ts/v3.html#propagate_const.const_observers
> constexpr operator const element_type*() const;
>
> Constraints:
> T is an object pointer type or has an implicit conversion to const
> element_type*.
> Returns:
> get().
libstdc++ implements these constraints by means of SFINAE on the operators.
This is user-hostile: using SFINAE means that the conversion operator is now a
function template, and that means that https://eel.is/c++draft/over.ics.user#3
kicks in:
> If the user-defined conversion is specified by a specialization of a
> conversion function template, the second standard conversion sequence shall
> have exact match rank.
Concretely, this means that for instance we lose implicit conversions towards
base classes of the pointed-to type:
std::experimental::propagate_const<Derived *> ptr;
Derived *d1 = ptr; // Convert precisely to Derived *: OK
Base *b1 = ptr; // Convert to a pointer to a base: ERROR
Base *b2 = static_cast<Derived *>(ptr); // OK
Base *b3 = static_cast<Base *>(ptr); // ERROR
Base *b4 = ptr.get(); // OK
But these should all work. The design of propagate_const is for it to be
"drop-in replacement", maximizing compatibility with existing code.
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4388.html says
explictly
"When T is an object pointer type operator value* exists and allows implicit
conversion to a pointer. This avoids using get to access the pointer in
contexts where it was unnecesary before addition of the propagate_const
wrapper."
--
So. ideally, the conversion operators should be using C++20 constraints, but of
course that's not possible. I guess that a reasonable alternative would be to
isolate them in a base class, and apply SFINAE on that base class instead?