Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

c++ - Why is the std::derived_from concept implemented with an additional convertibility test that adds cv-qualifiers?

In the GCC C++20 concepts lib, it has

template<typename _Derived, typename _Base>
    concept derived_from = __is_base_of(_Base, _Derived)
    && is_convertible_v<const volatile _Derived*, const volatile _Base*>;
  1. Why is requiring just __is_base_of(_Base, _Derived) not enough?
  2. What is the need to use const volatile in the test?
question from:https://stackoverflow.com/questions/65915059/why-is-the-stdderived-from-concept-implemented-with-an-additional-convertibili

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The behavior of std::derived_from is specified in terms of unambiguous public inheritance

The concept derived_from<Derived, Base> is satisfied if and only if Base is a class type that is either Derived or a public and unambiguous base of Derived, ignoring cv-qualifiers.

Note that this behaviour is different to std::is_base_of when Base is a private or protected base of Derived.

__is_base_of is the compiler intrinsic that is used to implement std::is_base_of. So using it alone would not produce the desired behavior.

So to check the "unambiguous public" part of the requirement, we may check if a pointer to a derived object is implicitly convertible into a pointer to a base object. That's just standard procedure with C++ classes. The pointers are convertible if the classes model an "is-a" relationship, public inheritance and not from multiple bases.

The const volatile addition is to handle the requirement of "ignoring cv-qualifiers". By always adding them, the conversion is legal even if for example _Derived is B const to some A (non-const) _Base. Comparing the pointers as-is would try to convert B const* to A*, and would fail on account of discarded cv-qualifiers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...