Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(void)r++ requirement of input iterator

Tags:

c++

iterator

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

like image 552
Praxeolitic Avatar asked Feb 10 '23 21:02

Praxeolitic


2 Answers

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.

like image 184
Wintermute Avatar answered Feb 13 '23 09:02

Wintermute


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 :).

like image 34
kuroi neko Avatar answered Feb 13 '23 11:02

kuroi neko