Search code examples
c++compiler-errorsredhatnumeric

Missing include gives no compile error on RedHat 6


This snippet can not be compiled, since std::accumulate is found in the header numeric, which is not included.

#include <algorithm>
#include <vector>

int main () {
    std::vector<int> vec{ 1, 2, 3, 4 };
    return std::accumulate(vec.begin(), vec.end(),0);
}

The compiler explorer gives me the correct error message

<source>(6): error: namespace "std" has no member "accumulate"
      return std::accumulate(vec.begin(), vec.end(),0);

I am using RedHat 6 and the intel compiler version 18.0.3. If I compile it with this setting, I get no error and the result is fine. No warning is shown, even if -Wall is used.

My question is, why don't I get an appropriate error message?


Solution

  • why don't I get an appropriate error message?

    Because one of the standard library headers <algorithm> or <vector> that you use for compilation do include <numeric> themselves. This is a common portability issue; your code happens to compile with a particular standard library implementation, but fails to build with another one. Library implementations are free to include standard headers in standard headers. Maybe some functionality in your <algorithm> was implemented using any of the <numeric> algorithms, and there you are.

    The compiler error you encounter is the reason tools like include-what-you-use exist. Using iwyu her would add #include <numeric> to your snippet. Note also that no warning flags will influence the result of the compilation. Either you get a hard compiler error or nothing.