5

Under 24.2.3 Input iterators, the C++ standard specifies one of the requirements of input iterator as the expression (void)r++ being equivalent to (void)++r.

You can also see this at cppreference.

What is that expression? What is the significance of this requirement? Why is it needed?

It looks like a C style cast to void of the result of r++ or ++r but I don't think that's what it really is. That said, to digress a bit, it looks like I can define a void conversion operator inside a class. Both gcc and clang compile it but clang gives the warning:

warning: conversion function converting 'C' to 'void' will never be used

Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
  • 1
    Re the warning, an ordinary conversion to `void` creates a *discarded value* expression, where an `operator void` is not invoked. C++11 footnote 116 in §12.3.2/1 states that "A conversion to void does not invoke any conversion function", which is more clear than figuring out the consequences of discarded value, but footnotes are not normative. One way of understanding this is to remember that `void` is an incomplete type, and one can't ordinarily create an instance of an incomplete type, hence this must be a special case. – Cheers and hth. - Alf Feb 22 '15 at 10:35
  • @Cheersandhth.-Alf Interesting! I wonder why that is so. http://stackoverflow.com/questions/28657392/why-are-void-conversion-functions-weird – Praxeolitic Feb 22 '15 at 11:43

2 Answers2

5

It is a C-style cast to void. What the standard means to say is that r++ and ++r must be equivalent if their return value is not used, i.e., that they have the same side effect.

Wintermute
  • 42,983
  • 5
  • 77
  • 80
3

It basically means that the pre- or post-incrementation of an iterator shall produce the same effect, though returned values might (and will in the general case) differ.

That's just the way of C++ specs to state the obvious in the most obfuscated possible way :).

kuroi neko
  • 8,479
  • 1
  • 19
  • 43
  • Obfuscated indeed! It's a very unobvious meaning of "equivalent". I took it to mean equal value. – Praxeolitic Feb 22 '15 at 10:33
  • 3
    It's terse and correct, not obfuscated. The standard is not a document for learners. It's a document for compiler writers, like a blueprint: not designed for ease of grokking. That said, Anders Hejlsberg did a much better job of using clear language in the C# language specification. – Cheers and hth. - Alf Feb 22 '15 at 10:37
  • 1
    Doesn't it strike you as impractical to have a language whose definition needs gloss and annotations to be understood by mere mortals, like some kind of ancient holly book? What are programmers stupid enough to find such "terse and correct" statements not so obvious supposed to use to learn C++? Another 1000 pages exegesis that will be rendered obsolete by the next yearly evolution? Or maybe a catalog of "good practices" will be quite good enough for these commoners? – kuroi neko Feb 22 '15 at 10:56