What is the meaning of (void) first2 ++ here?

On the this page on the en.cppreference page, there are examples of possible lexicographic comparisons. Here is the main one:

template<class InputIt1, class InputIt2> bool lexicographical_compare(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) { for ( ; (first1 != last1) && (first2 != last2); first1++, (void) first2++ ) { if (*first1 < *first2) return true; if (*first2 < *first1) return false; } return (first1 == last1) && (first2 != last2); } 

IIRC lines such as (void)some_argument; are commonly used to suppress compiler warnings about unused parameters. But in this function, all parameters, including first2, are used by - , so what is the write point (void) first2++ in the for?

Is this some syntax workaround in case of overloading the InputIt1 operator, ,?

+8
c ++ expression
source share
1 answer

This ensures that the built-in comma operator is used - just in case there is a user-defined overload that does something unexpected. A value of type void never be passed to a function, so this overload cannot be selected here.

Imagine what would happen if this function existed:

 void operator , (InputIt1 const &, InputIt2 const &) { launch_missiles(); } 

EDIT . This is actually mentioned on the cppreference related page.

+5
source share

All Articles