是什么导致 __detail::__can_reference 失败?

What causes __detail::__can_reference to fail?

考虑以下代码。

#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() {
}

我收到以下错误。

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

导致此错误的原因是什么?

static_assert 移到 class 正文之后。

您不能将不完整的类型传递给 std::forward_iterator,并且 class 在您提出断言的地方尚未完成。