I'm confused as to why the following code compiles in some cases, but not others.
#include <iostream>
#include <vector>
#include <algorithm>
int main(){
std::vector<int> v(3);
int a[] = {3, 6, 2};
std::copy(a, a+3, v.begin());
#define CASE 2
#if CASE == 0
std::cout << *max_element(a, a+3) << "\n";
#elif CASE == 1
std::cout << *std::max_element(a, a+3) << "\n";
#else
std::cout << *max_element(v.begin(), v.end()) << "\n";
#endif
return 0;
}
I have placed in three cases: CASE 0 fails to compile because there's no such thing as "max_element". I fix this in CASE 1 by changing to "std::max_element" instead, and it does compile and works as expected.
However, interestingly for CASE 2 (technically anything but 0 or 1), it also compiles and works. But CASE 2 has the same problem as CASE 0, so why does it work?