https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106837
Bug ID: 106837 Summary: False compilation error "inconsistent begin/end types in range-based 'for' statement" Product: gcc Version: 12.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: ofekshilon at gmail dot com Target Milestone: --- The following fails to build with all gcc versions at least since 6.1, when built with "-std=c++17" or (for supporting versions) with "-std=c++20". struct myDat { myDat() {} struct Iter { Iter &operator++(); int operator*(); bool operator!=(int& other); }; Iter begin() ; int end() ; }; int main() { for (int pos : myDat()) ; } Here's a godbolt link, with comparison to a successful build by clang: https://godbolt.org/z/1fsfvxzb7 Note this patch by Jason Merrill: https://gcc.gnu.org/legacy-ml/gcc-patches/2016-03/msg00792.html + { + if (cxx_dialect >= cxx1z + && (build_x_binary_op (input_location, NE_EXPR, + *begin, ERROR_MARK, + *end, ERROR_MARK, + NULL, tf_none) + != error_mark_node)) + /* P08184R0 allows __begin and __end to have different types, + but make sure they are comparable so we can give a better + diagnostic. */; + else + error ("inconsistent begin/end types in range-based %<for%> " + "statement: %qT and %qT", + TREE_TYPE (*begin), TREE_TYPE (*end)); + } In this case the begin/end iter types *are* comparable, but the build_x_binary_op test fails.