缺少include不会在RedHat 6上产生编译错误

sch*_*312 1 c++ redhat compiler-errors numeric

由于无法在标头中找到std :: accumulate,因此无法编译此代码段numeric

#include <algorithm>
#include <vector>

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

编译器的探险家给了我正确的错误消息

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

我正在使用RedHat 6和Intel编译器版本18.0.3。如果使用此设置进行编译,则不会出现错误,并且结果很好。即使-Wall使用警告也不会显示。

我的问题是,为什么我没有收到适当的错误消息?

lub*_*bgr 5

为什么我没有收到适当的错误消息?

因为标准库标头之一<algorithm><vector>您用于编译的标头确实包含<numeric>它们自己。这是一个常见的可移植性问题。您的代码恰好可以与特定的标准库实现一起编译,但是无法与其他标准库一起编译。库实现可以免费在标准标头中包含标准标头。也许您<algorithm>中的某些功能是使用任何<numeric>算法实现的,您就可以了。

您遇到的编译器错误是诸如“ 包括什么”之类的工具存在的原因。使用iwyu她会增加#include <numeric>您的代码段。还要注意,没有警告标志将影响编译结果。您会收到硬编译器错误,或者什么也没有。