Why is ranges::ostream_iterator default-constructible?

后端 未结 2 2066
滥情空心
滥情空心 2020-12-11 03:07

This question follows a discussion in the comments here.

In Eric Niebler\'s ranges-v3 library (which is sort-of becoming part of the standard for C++20),

2条回答
  •  青春惊慌失措
    2020-12-11 03:20

    This follows the Elements of Programming design philosophy of how types should behave. If you've heard the phrase "do as the ints do", that is that philosophy -- types should be Regular. And the EoP definition of Regular is:

    T’s computational basis includes equality, assignment, destructor, default constructor, copy constructor, total ordering (or default total ordering) and underlying type

    which translates to real C++20 concepts as:

    template
      concept Movable = is_object_v && MoveConstructible && Assignable
        && Swappable;
    template
      concept Copyable = CopyConstructible && Movable && Assignable;
    template
      concept Semiregular = Copyable && DefaultConstructible;
    template
      concept Regular = Semiregular && EqualityComparable;
    

    We've lost the total ordering part in favor of simply EqualityComparable, and even then a lot of the library requirements via Ranges actually only require Semiregular - not Regular. But still, this is the foundation of the idea.

    Note that if a type is movable, it already kind of makes sense for it to be default constructible. The moved-from state is very conceptually similar to a default-constructed state. Can't do much from there, but it's a state.

提交回复
热议问题