从 C++23 开始,views 不再需要是default_constructible. views::filter对于和等范围适配器views::transform,它们的默认构造函数被重新定义为:
template<input_range V, indirect_unary_predicate<iterator_t<V>> Pred>
requires view<V> && is_object_v<Pred>
class filter_view : public view_interface<filter_view<V, Pred>> {
private:
V base_ = V(); // exposition only
copyable-box<Pred> pred_; // exposition only
public:
filter_view() requires default_initializable<V> && default_initializable<Pred> = default;
};
并且由于p2325r3中的默认构造函数ref_view已被删除,这表明应用于 lvalue的范围适配器不再是:rangestd::vector default_constructible
std::vector v{1, 2, 3};
auto r = v | std::views::filter([](auto) { return true; });
decltype(r){}; // ok in C++20, error in C++23
只有当范围适配器被应用到default_constructible views 中时,例如std::string_viewor views::iota(0),返回的视图才可以是default_constructible:
auto r = std::views::iota(0) | std::views::filter([](auto) { return true; });
decltype(r){}; // ok in C++20 and C++23
但是对于这些情况,即使可行,我也真的想不出制作这些views的用例。default_constructible如果我们view默认构造a,就意味着我们构造了一个empty view,对empty s应用范围适配器显然是没有意义view的:
constexpr auto r = std::views::iota(0, 10)
| std::views::transform([](auto x) { return x; });
static_assert(!std::ranges::empty(r));
// views::iota is default-constructible, so r is also default-constructible
static_assert( std::ranges::empty(decltype(r){}));
在我看来,C++20 中范围适配器的默认构造函数只是为了满足view,由于view不再需要default_constructible在 C++23 中,因此这些适配器的默认构造函数不需要存在。
为什么这些范围适配器的默认构造函数在 C++23 中没有被删除,而是变成了约束函数?真的有需要它的情况default_constructible吗?这背后的考虑是什么?