0

I recently learned about c++ concepts and I am trying to understand them better. Here is the concept of an std::input_or_output_iterator from cppreference.

template <class I>
concept input_or_output_iterator =
  requires(I i) {
    { *i } -> /*can-reference*/;
  } &&
  std::weakly_incrementable<I>;

I think I understand the {*i} part which means that the every i which satisfies the concept has to be able to dereference. However, what is the point of the -> /*can-reference*/? What is meant by The exposition-only concept and also isn't this the same as

template <class I>
concept input_or_output_iterator =
  requires(I i) {
    { *i };
} &&
std::weakly_incrementable<I>;

or as

concept input_or_output_iterator =
requires(I i) {
*i ;
} &&
std::weakly_incrementable<I>;

?

Actually, I tried it on MSVC compiler and

template <class I>
concept foo = requires(I i) {
    {*i};
};

or

template <class I>
concept foo = requires(I i) {
    *i;
};

does compile but

template <class I>
concept bar = requires(I i) {
    {*i} -> /*can-reference*/;
};

does not.

康桓瑋
  • 33,481
  • 5
  • 40
  • 90
edamondo
  • 121
  • 3
  • "exposition only" means it's not real code, just an "illustration". The real implementation is up to the compiler/library writer. – Mat Jul 24 '23 at 14:17
  • Have you seen this sentence in the article you cite? "The exposition-only concept `/*can-reference*/` is satisfied if and only if the type is referenceable (in particular, not `void`)." This explains why it's not the same as `requires(I i) { { *i }; }` - the latter permits `I` to be `void*` – Igor Tandetnik Jul 24 '23 at 14:21
  • @Mat, sorry, what does it mean "not real code"? I don't get it – edamondo Jul 24 '23 at 15:06
  • 1
    @edamondo `/*can-reference*/` is, literally, only a comment (`/*` starts a comment and `*/` ends it). The cppreference authors put it there because there is no specification in the standard what exactly the implementation needs to put there instead. Each implementation of the standard library can choose what to put there itself as long as it works in such a way that it behaves as described on the rest of the page. – user17732522 Jul 24 '23 at 15:57

0 Answers0