gpt4 book ai didi

c++ - 无法满足 C++ 范围要求自定义容器

转载 作者:行者123 更新时间:2023-12-04 17:12:01 25 4
gpt4 key购买 nike

我一直在尝试编写一个带有自己的迭代器的自定义容器,该迭代器可用作范围并与 std::span 一起使用。我是范围的新手,所以请客气。

以下示例编译失败,因为我的容器类无法转换为 std::span


#include<span>
#include<vector>
#include<cstdint>


template<class T, class Allocator = std::allocator<T>>
class MyContainer
{
public:
class ConstIterator
{
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = T;
using difference_type = size_t;
using pointer = T* const;
using reference = const T&;

ConstIterator() = default;
ConstIterator(const ConstIterator &other) = default;


ConstIterator(pointer ptr)
: m_ptr(ptr)
{}

reference operator*() const
{
return *m_ptr;
}
pointer operator->()
{
return m_ptr;
}
// Prefix increment
ConstIterator& operator++()
{
m_ptr++;
return *this;
}
// Postfix increment
ConstIterator operator++(int)
{
Iterator tmp = *this;
++(*this);
return tmp;
}
// Prefix decrement
ConstIterator& operator--()
{
m_ptr--;
return *this;
}
// Postfix decrement
ConstIterator operator--(int)
{
Iterator tmp = *this;
--(*this);
return tmp;
}
ConstIterator& operator+=(const difference_type offset) noexcept
{
m_ptr += offset;
return *this;
}

ConstIterator operator+(const difference_type offset) const noexcept
{
ConstIterator tmp = *this;
tmp += offset;
return tmp;
}

ConstIterator& operator-=(const difference_type offset) noexcept
{
return *this += -offset;
}

ConstIterator operator-(const difference_type offset) const noexcept
{
ConstIterator tmp = *this;
tmp -= offset;
return tmp;
}

difference_type operator-(const ConstIterator& right) const noexcept
{
compatible(right);
return m_ptr - right.m_ptr;
}

reference operator[](const difference_type offset) const noexcept
{
return *(*this + offset);
}
bool operator==(const ConstIterator& right) const noexcept
{
return (*this == right);
}
bool operator!=(const ConstIterator& right) const noexcept
{
return !(*this == right);
}

bool operator<(const ConstIterator& right) const noexcept
{
compatible(right);
return m_ptr < right.m_ptr;
}

bool operator>(const ConstIterator& right) const noexcept
{
return right < *this;
}

bool operator<=(const ConstIterator& right) const noexcept
{
return !(right < *this);
}

bool operator>=(const ConstIterator& right) const noexcept
{
return !(*this < right);
}
protected:
T* m_ptr;
};
class Iterator : public ConstIterator
{
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = T;
using difference_type = size_t;
using pointer = T*;
using reference = T&;

Iterator() = default;
Iterator(const Iterator &other) = default;

Iterator(pointer ptr)
: ConstIterator(ptr)
{}

reference operator*() const
{
return *ConstIterator::m_ptr;
}
pointer operator->()
{
return ConstIterator::m_ptr;
}
// Prefix increment
Iterator& operator++()
{
ConstIterator::m_ptr++;
return *this;
}
// Postfix increment
Iterator operator++(int)
{
Iterator tmp = *this;
++(*this);
return tmp;
}
// Prefix decrement
Iterator& operator--()
{
ConstIterator::m_ptr--;
return *this;
}
// Postfix decrement
Iterator operator--(int)
{
Iterator tmp = *this;
--(*this);
return tmp;
}
Iterator& operator+=(const difference_type offset) noexcept
{
ConstIterator::_Verify_offset(offset);
ConstIterator::m_ptr += offset;
return *this;
}

Iterator operator+(const difference_type offset) const noexcept
{
Iterator tmp = *this;
tmp += offset;
return tmp;
}

Iterator& operator-=(const difference_type offset) noexcept
{
return *this += -offset;
}

Iterator operator-(const difference_type offset) const noexcept
{
Iterator tmp = *this;
tmp -= offset;
return tmp;
}

difference_type operator-(const ConstIterator& right) const noexcept
{
compatible(right);
return ConstIterator::m_ptr - right.m_ptr;
}

reference operator[](const difference_type offset) const noexcept
{
return *(*this + offset);
}
};
public:
using value_type = T;
using allocator_type = Allocator;
using pointer = typename std::allocator_traits<Allocator>::pointer;
using const_pointer = typename std::allocator_traits<Allocator>::const_pointer;
using reference = value_type &;
using const_reference = const value_type &;
using size_type = typename std::vector<T>::size_type;
using difference_type = typename std::vector<T>::difference_type;
using iterator = Iterator;
using const_iterator = ConstIterator;
using reverse_iterator = typename std::reverse_iterator<iterator>;
using const_reverse_iterator = typename std::reverse_iterator<const_iterator>;

MyContainer()
{
m_data.resize(10);
}
iterator begin()
{
return iterator(&m_data[0]);
}
iterator end()
{
return iterator(&m_data[0]+m_data.size());
}
const_iterator begin() const
{
return const_iterator(&m_data[0]);
}
const_iterator end() const
{
return const_iterator(&m_data[0]+m_data.size());
}
/*
//These versions of begin() and end() work
T* begin()
{
return &m_data[0];
}
T* end()
{
return &m_data[0]+m_data.size();
}
T* const begin() const
{
return &m_data[0];
}
T* const end() const
{
return &m_data[0]+m_data.size();
}*/
private:
std::vector<value_type> m_data;
};

int getSum(std::span<int const>s)
{
int result =0;
for (int val : s)
result += val;
return result;
}

int main()
{
MyContainer<int> data;
int sum = getSum(data);
}

抱歉,这是一个很长的最小示例,但我的理解是自定义迭代器需要提供所有适当的功能才能与 std::span 一起使用。

我从 clang 得到的编译错误是

<source>:282:12: error: no matching function for call to 'getSum'
int sum = getSum(data);
^~~~~~
<source>:271:5: note: candidate function not viable: no known conversion from 'MyContainer<int>' to 'std::span<const int>' for 1st argument
int getSum(std::span<int const>s)
^
1 error generated.

或者来自VS2019

<source>(282): error C2664: 'int getSum(std::span<const int,18446744073709551615>)': cannot convert argument 1 from 'MyContainer<int,std::allocator<int>>' to 'std::span<const int,18446744073709551615>'
<source>(282): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
<source>(271): note: see declaration of 'getSum'

Clang 最初给我的错误是不满足所有各种范围类型的要求,包括输入和输出范围,它引用了 _Begin。但是当我将代码剥离回这个示例时,消息变得不那么详细了。

这让我觉得我的迭代器有问题。如果我不返回我的自定义迭代器,而是只返回一个原始指针,那么代码可以正常编译。

有什么方法可以解决我的代码无法正常工作的原因吗?我什至在努力寻找一个适合新手理解的所有这一切的指南。

最佳答案

对于以后再做的任何人,我调试迭代器的方法是设置一组静态断言来检查我的迭代器在哪个点不符合要求。因为迭代器类型形成了一个层次结构,其中每个迭代器还必须满足层次结构中更下方的迭代器的要求(除了同等的输入和输出),我从

    using cType = sci::MyContainer<int>;
using iType = cType::Iterator;
static_assert(std::input_iterator<iType>, "failed input iterator");
static_assert(std::output_iterator<iType, int>, "failed output iterator");
static_assert(std::forward_iterator<iType>, "failed forward iterator");
static_assert(std::input_iterator<iType>, "failed input iterator");
static_assert(std::bidirectional_iterator<iType>, "failed bidirectional iterator");
static_assert(std::random_access_iterator<iType>, "failed random access iterator");
static_assert(std::contiguous_iterator<iType>, "failed contiguous iterator");

然后我从第一个错误开始,找到该概念的代码并为子概念设置 static_assert。例如,当我收到“输入迭代器失败”错误时,我设置了静态断言

    static_assert(std::weakly_incrementable<iType>, "Failed the weakly incrementable test");
static_assert(std::movable<iType>, "Failed the moveable test");
static_assert(std::default_initializable<iType>, "Failed the default initializable test");

这让我发现(如@Unslander Monica 所述)我有一个未签名的 difference_type 当它需要被签名时,我没有 Iterator operator+(const difference_type offset, const Iterator &iter),我需要一个 element_type,对于我的非 const 迭代器,我需要一个 const pointer operator->() const

关于c++ - 无法满足 C++ 范围要求自定义容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69254654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com