Why does the following main.cpp:
#include <vector>
int main() {
std::vector<size_t> order;
reverse(order.begin(), order.end());
return EXIT_SUCCESS;
}
compile without errors with g++ -std=c++20 main.cpp but fails (as expected) without C++20 enabled (g++ main.cpp)?
main.cpp: In function ‘int main()’:
main.cpp:5:17: error: ‘size_t’ was not declared in this scope; did you mean ‘std::size_t’?
5 | std::vector<size_t> order;
| ^~~~~~
| std::size_t
In file included from /usr/include/c++/11/bits/stl_algobase.h:59,
from /usr/include/c++/11/vector:60,
from main.cpp:1:
/usr/include/x86_64-linux-gnu/c++/11/bits/c++config.h:280:33: note: ‘std::size_t’ declared here
280 | typedef __SIZE_TYPE__ size_t;
| ^~~~~~
main.cpp:5:23: error: template argument 1 is invalid
5 | std::vector<size_t> order;
| ^
main.cpp:5:23: error: template argument 2 is invalid
main.cpp:6:19: error: request for member ‘begin’ in ‘order’, which is of non-class type ‘int’
6 | reverse(order.begin(), order.end());
| ^~~~~
main.cpp:6:34: error: request for member ‘end’ in ‘order’, which is of non-class type ‘int’
6 | reverse(order.begin(), order.end());
| ^~~
main.cpp:6:5: error: ‘reverse’ was not declared in this scope
6 | reverse(order.begin(), order.end());
| ^~~~~~~
main.cpp:8:12: error: ‘EXIT_SUCCESS’ was not declared in this scope
8 | return EXIT_SUCCESS;
| ^~~~~~~~~~~~
For the record, I'm using g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0.
EDIT: There are similar questions about size_t vs std::size_t, but I'm more puzzled about the reverse function.
std::reverseis found through argument dependent lookup asstd::vector::iteratoris in the same namespace.Presumably the missing
<algorithm>include is found indirectly through<vector>, adding c++20 will add more features to<vector>which presumably requires the inclusion of<algorithm>.Note that this compiles with MSVC and clang with libc++ without needing c++20: https://godbolt.org/z/hGoj4shr3 and GCC compiles with the missing include: https://godbolt.org/z/b4jKWEo1f