Des*_*old 7 c++ c++20 std-ranges
我创建了一个名为的自定义容器goldbox,它仅包含算术类型,并且我还实现了begin成员end函数来迭代元素。
我的完整源代码:
#include <algorithm>
#include <vector>
#include <initializer_list>
#include <iostream>
#include <type_traits>
#include <ranges>
template <typename T>
concept Arithmetic = std::is_arithmetic_v<T>;
template <Arithmetic T = int>
class goldbox {
private:
template <Arithmetic Base_t>
struct Node {
Base_t data;
Node<Base_t>* prev;
Node<Base_t>* next;
};
Node<T>* head;
Node<T>* current_node;
Node<T>*& __at_node(size_t index) {
auto temp = head;
size_t count {0};
while (count < index) {
temp = temp->next;
count++;
}
current_node = temp;
return current_node;
}
Node<T>*& __get_tail() {
return __at_node(length() - 1);
}
public:
using value_type = T;
goldbox() : head{nullptr}, current_node{nullptr} {}
goldbox(std::initializer_list<T>&& list_arg) : goldbox() {
decltype(auto) list_1 = std::forward<decltype(list_arg)>(list_arg);
T temp[list_1.size()];
std::copy(list_1.begin(), list_1.end(), temp);
std::reverse(temp, temp + list_1.size());
for (const auto& elem : temp)
push_front(elem);
}
class iterator {
private:
Node<T>* node;
public:
iterator(Node<T>* arg) noexcept : node{arg} {}
iterator& operator=(Node<T>* arg) {
node = arg;
return *this;
}
iterator operator++() {
if (node)
node = node->next;
return *this;
}
iterator operator++(int) {
iterator iter = *this;
++(*this);
return iter;
}
iterator operator--() {
if (node)
node = node->prev;
return *this;
}
iterator operator--(int) {
iterator iter = *this;
--(*this);
return iter;
}
bool operator==(const iterator& other) {
return (node == other.node);
}
bool operator!=(const iterator& other) {
return (node != other.node);
}
T& operator*() {
return node->data;
}
};
iterator begin() {
return iterator{head};
}
iterator end() {
return iterator{nullptr};
}
size_t length() const {
auto temp = head;
size_t count {0};
while (temp != nullptr) {
++count;
temp = temp->next;
}
return count;
}
goldbox& push_front(T arg) {
auto new_node = new Node<T>;
new_node->data = arg;
new_node->prev = nullptr;
new_node->next = head;
if (head != nullptr)
head->prev = new_node;
head = new_node;
return *this;
}
goldbox& push_back(T arg) {
auto new_node = new Node<T>;
auto last = head;
new_node->data = arg;
new_node->next = nullptr;
if (head == nullptr){
new_node->prev = nullptr;
head = new_node;
return *this;
}
while (last->next != nullptr)
last = last->next;
last->next = new_node;
new_node->prev = last;
return *this;
}
goldbox& clear() {
auto temp = head;
Node<T>* next_temp;
while (temp != nullptr) {
next_temp = temp->next;
delete temp;
temp = next_temp;
}
head = nullptr;
return *this;
}
goldbox& pop_back() {
if (head != nullptr) {
if (length() != 1) {
delete std::move(__get_tail());
__at_node(length() - 2)->next = nullptr;
} else {
this->clear();
}
}
return *this;
}
goldbox& pop_front() {
if (head != nullptr) {
auto temp = head;
head = head->next;
delete temp;
}
return *this;
}
};
int main() {
goldbox goldbox_1 {2, 3, 5, 6, 7, 9};
goldbox goldbox_2;
for (const auto& elem : goldbox_1) {
std::cout << elem << ' ';
} std::cout << '\n';
std::transform(goldbox_1.begin(), goldbox_1.end(),
std::back_inserter(goldbox_2),
[](auto x){return 2 * x - 1; }
);
for (const auto& elem : goldbox_2) {
std::cout << elem << ' ';
} std::cout << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
2 3 5 6 7 9
3 5 9 11 13 17
Run Code Online (Sandbox Code Playgroud)
但我想使用范围来使用它,这样我就不必创建新实例。
一旦我应用了goldbox基于范围的 for 循环内部:
for (const auto& elem: goldbox_1 | std::ranges::views::transform([](auto x){return 2 * x - 1;})) {
std::cout << elem << ' ';
} std::cout << '\n';
Run Code Online (Sandbox Code Playgroud)
它抛出一个错误,因为我没有提供operator|.
如果我使用非管道语法:
for (const auto& elem: std::ranges::views::transform(goldbox_1, [](auto x){return x + 1;})) {
std::cout << elem << ' ';
} std::cout << '\n';
Run Code Online (Sandbox Code Playgroud)
它仍然会抛出一个错误,因为 和begin都end没有在范围内声明。
总长DR
您的类不满足std::ranges::input_range,因为您的迭代器不满足std::ranges::input_iterator。在你的迭代器类中,你需要:
difference_type别名operator==常量value_type别名T& operator*() const它仍然会抛出一个错误,表明 begin 和 end 都没有在范围内声明。
我不知道您使用的是什么编译器会给出此误导性错误消息。如果使用 gcc 编译,您会得到正确的错误诊断:
注意:所需的表达式 'std::ranges::__cust::begin(__t)' 无效
Run Code Online (Sandbox Code Playgroud)581 | ranges::begin(__t); | ~~~~~~~~~~~~~^~~~~cc1plus:注意:将“-fconcepts-diagnostics-depth=”设置为至少 2 以获取更多详细信息
设置较高-fconcepts-diagnostics-depth=我们可以看到根本原因:
注意:不满足或操作数
Run Code Online (Sandbox Code Playgroud)114 | requires is_array_v<remove_reference_t<_Tp>> || __member_begin<_Tp> | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 115 | || __adl_begin<_Tp> | ^~~~~~~~~~~~~~~~~~~
您的容器不是数组,并且您没有使用 adl begin 而是使用 member begin,因此我们需要调查为什么您的类不满足__member_begin:
注意: 'std::__detail::__decay_copy(__t.begin())' 不满足返回类型要求,因为
Run Code Online (Sandbox Code Playgroud)939 | { __detail::__decay_copy(__t.begin()) } -> input_or_output_iterator; | ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
问题是你的迭代器类实际上不是一个合适的迭代器。让我们看看为什么:
注意:表达式 'is_constructible_v<_Tp, _Args ...> [with _Tp = goldbox::iterator; _Args = {}]' 评估为“假”
Run Code Online (Sandbox Code Playgroud)139 | = destructible<_Tp> && is_constructible_v<_Tp, _Args...>; | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第一个修复是使迭代器默认可构造。这样做然后重新编译我们发现您的类进一步不满足迭代器的概念:
注意:所需的类型 'std::iter_difference_t<_Iter>' 无效,因为
Run Code Online (Sandbox Code Playgroud)601 | typename iter_difference_t<_Iter>; | ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
和
注意:“++ __i”不满足返回类型要求,因为
Run Code Online (Sandbox Code Playgroud)603 | { ++__i } -> same_as<_Iter&>; | ^~~~~
您需要添加difference_type公共别名并使预自增和预自减运算符返回对迭代器的引用。
修复此问题然后重新编译,我们发现您的类进一步不满足迭代器的概念:
注意: 'std::__detail::__decay_copy(__t.end())' 不满足返回类型要求,因为
Run Code Online (Sandbox Code Playgroud)136 | { __decay_copy(__t.end()) } | ~~~~~~~~~~~~^~~~~~~~~~~错误:推导的表达式类型不满足占位符约束
Run Code Online (Sandbox Code Playgroud)136 | { __decay_copy(__t.end()) } | ~~^~~~~~~~~~~~~~~~~~~~~~~~~ 137 | -> sentinel_for<decltype(_Begin{}(std::forward<_Tp>(__t)))>; | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~注意:所需的表达式 '(__t == __u)' 无效,因为
Run Code Online (Sandbox Code Playgroud)282 | { __t == __u } -> __boolean_testable; | ~~~~^~~~~~
这意味着 返回的迭代器begin()与 返回的迭代器不可比较end。向下查看诊断深度,您可以看到您的内容operator==未被考虑,因为它不是 const。
修复此问题然后重新编译,我们发现您的类进一步不满足 input_iterator 的概念:
注意:所需的类型 'std::iter_value_t<_In>' 无效,因为
Run Code Online (Sandbox Code Playgroud)514 | typename iter_value_t<_In>; | ~~~~~~~~~^~~~~~~~~~~~~~~~~~
通过添加公共value_type别名可以解决此问题。
下一个:
注意:嵌套要求 'same_as<std::iter_reference_t, std::iter_reference_t<_Tp> >' 不满足,因为
Run Code Online (Sandbox Code Playgroud)517 | requires same_as<iter_reference_t<const _In>, | ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 518 | iter_reference_t<_In>>; | ~~~~~~~~~~~~~~~~~~~~~~
这意味着和operator*应该返回相同的引用类型。通过添加 const 可以解决此问题:iteratorconst iteratoroperator*
T& operator*();
T& operator*() const;
Run Code Online (Sandbox Code Playgroud)
现在所有编译错误都已修复,并且两个版本(管道和非管道)都可以编译。请注意,我已经修复了编译错误,尚未检查您的语义。