-1

In C++20, concepts were added, and so were constrained algorithms such as std::ranges::for_each.

Why is the range-based for loop error message has plain errors like:

  • begin and end are not declared in a scope.

Why not just:

  • a given expression doesn't satisfy std::ranges::range

It will still confuse some of the beginners and maybe just create a specific concept name (hidden) so that they will get what is missing.

In structured binding compiler error, the message is very clear.

Is it the compiler's responsibility to address this problem or the standard committee?

Desmond Gold
  • 1,517
  • 1
  • 7
  • 19
  • Your question doesn't make sense. Range-based for is constrained - it operates over a range marked by `begin` and `end` iterators so works over standard containers AND, in C++20, ranges (which both provide `begin()` and `end()` end iterators/sentinels). The `range` requirement is to provide a `begin` and `end` iterator, so it is a moot point whether the compiler reports absence of `begin` and `end` iterators, or not meeting range requirements in a construct that predates concepts. Depending on how the compiler (or its library) are implemented, it might be easier to provide one over the other. – Peter Jul 25 '21 at 09:48
  • The first tells you what is wrong and how to fix it. The 2nd just points you to the need to do some research. I find the 1st more helpful. – Richard Critten Jul 25 '21 at 10:55

1 Answers1

2

std::ranges::for_each is new function and it uses C++20 concepts. The regular "range based for loop" is not a function and it predates C++20 concepts (it was added in C++11) thus it doesn't use concepts. It would be up to the standard committee to amend the definition of range based for loop to be in terms of some standard concepts instead of begin and end functions.

I don't know if that will ever happen though, considering it's not a massive improvement and standardization effort could be used elsewhere more effectively.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93