Consider the following code.
#include <iterator>
struct Node {
static Node mNode;
};
Node Node::mNode;
struct DeepNodeRange {};
class DeepNodeIter
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = Node*;
using difference_type = std::ptrdiff_t;
DeepNodeIter() = default;
DeepNodeIter(DeepNodeRange& deepNodeRange, bool end = false) :
mDeepNodeRange(&deepNodeRange), mEnd(end) {}
auto operator*() const { return &Node::mNode; }
auto& operator ()
{
mIdx ;
mEnd = (mIdx > 10);
return *this;
}
auto operator ([[maybe_unused]] int val)
{
auto tmp(*this);
operator ();
return tmp;
}
auto operator==(const DeepNodeIter& iter) const
{ return iter.mEnd == mEnd; }
protected:
DeepNodeRange* mDeepNodeRange;
int mIdx;
bool mEnd;
static_assert(std::forward_iterator<DeepNodeIter>);
};
int main() {
}
I'm getting the following error.
b.cpp:51:22: error: static assertion failed
51 | static_assert(std::forward_iterator<DeepNodeIter>);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
b.cpp:51:22: note: constraints not satisfied
In file included from include/c /11.1.0/bits/stl_iterator_base_types.h:71,
from include/c /11.1.0/iterator:61,
from b.cpp:1:
include/c /11.1.0/bits/iterator_concepts.h:612:13: required for the satisfaction of 'input_or_output_iterator<_Iter>' [with _Iter = DeepNodeIter]
include/c /11.1.0/bits/iterator_concepts.h:634:13: required for the satisfaction of 'input_iterator<_Iter>' [with _Iter = DeepNodeIter]
include/c /11.1.0/bits/iterator_concepts.h:613:9: in requirements with '_Iter __i' [with _Iter = DeepNodeIter]
include/c /11.1.0/bits/iterator_concepts.h:613:33: note: the required expression '* __i' is invalid
613 | = requires(_Iter __i) { { *__i } -> __detail::__can_reference; }
| ^~~~
cc1plus: note: set '-fconcepts-diagnostics-depth=' to at least 2 for more detail
What is causing this error?
CodePudding user response:
Move the static_assert
after the class body.
You cannot pass an incomplete type to std::forward_iterator
and the class isn't complete yet at the point where you put the assertion.