给定的
auto empty_line = [](auto& str){ return str.size() == 0; };
对于这一事实ranges::getlines
返回一个拥有 view_facade
它拥有一个用于其正面迭代器的缓冲区。因此,在传递给算法之前,我们有义务将这种范围设为左值。
auto line_range1 = ranges::getlines(std::cin);
auto iter1 = ranges::find_if_not(line_range1,empty_line);
auto input1 = std::stoi(*iter1);
而且还有一个很酷的保护机制,可以防止对迭代器的所有取消引用在时间已经被破坏的数据,并使这些尝试发生编译时错误。所以当拥有
view_facade
作为右值传递到算法中,保护在取消引用时进行。这不会编译。
auto iter2 = ranges::find_if_not(ranges::getlines(std::cin),empty_line);
// at this point the `owning` range destroyed,
// so as the buffer we (should've) hold (before!).
// So this won't compile
// auto input2 = std::stoi(*iter2);
错误为:<source>:19:29: error: no match for 'operator*' (operand type is 'ranges::v3::dangling<ranges::v3::_basic_iterator_::basic_iterator<ranges::v3::getlines_range::cursor> >')
auto input2 = std::stoi(*iter2);
^~~~~~
这也不会编译。// Won't compile
// auto input3 = std::stoi(*ranges::find_if_not(ranges::getlines(std::cin),
// empty_line)
// );
错误为:<source>:22:29: error: no match for 'operator*' (operand type is 'ranges::v3::safe_iterator_t<ranges::v3::getlines_range> {aka ranges::v3::dangling<ranges::v3::_basic_iterator_::basic_iterator<ranges::v3::getlines_range::cursor> >}')
auto input3 = std::stoi(*ranges::find_if_not(ranges::getlines(std::cin),empty_line));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
godbolt.org/g/gF6nYx我的问题是,除了文档之外,是否有任何特征或任何类型的范围约定来检查范围类型是否拥有?
也许,类似
constexpr bool is_owning_v(Rng&&)
最佳答案
std::ranges::enable_borrowed_range 会告诉你范围类型是否为 不是 拥有。见 P2017R1 .要实现您描述的功能,您可以这样做:
template <typename Rng>
constexpr bool is_owning_v(Rng&&)
{
return !std::ranges::enable_borrowed_range<std::remove_cvref_t<Rng>>;
}
关于c++ - 是否有特征或任何约定来检查范围或 `view_facade` 是否拥有事物? (例如 getlines),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48984165/