STL iota 包含文件更改

STL iota include file change

iota 函数以前在 <algorithm> header 中。它已更改为 <numeric>

我需要保留旧方法以实现向后兼容性,所以我想使用预处理器选项 select 正确 header 来包含。

这什么时候改变的,我应该使用哪个预处理器选项?

iota (from the Greek ι) was re-introduced in <numeric> when arrived, as you can see in the ref。所以你检查你是否在没有它的环境中,并包括旧的 header,否则,包括新的 header.

这样的事情可能会成功:

#if __cplusplus <= 199711L
  #include <algorithm>
#else
  #include <numeric>
#endif

__cplusplus 的值设置为 201103L。断言完全符合 2011 标准;它不会告诉您有关部分一致性或编译器扩展的信息。如果 __cplusplus 设置为 201103L,那么编译器要么完全符合,要么在骗你。如果不是,那么您无法真正判断它支持哪些功能。

阅读更多内容answer

此外,这个 Quora post 也同意 std::iota 发生的变化(或者说实话;re-introduce) .


如果你负担得起,就包括这两个库。

您要做的是检查 __cplusplus 变量是否低于某个点,如果低于某个点,则 #include <algorithm> 如果不是,则 #include <numeric>

#if __cplusplus <= 199711L
  #include <algorithm>
#else
  #include <numeric>
#endif

这应该适用于您需要使用的几乎所有库,请注意,您可能需要将 199711L 更改为适当的数字。

iota 从来不是 "moved" 来自 <algorithm>。 C++03 在任何 header 中都没有 std::iota,并且没有一致的实现可以提供它,因为 iota 不是保留名称,这是工作所必需的:

#define iota arbitrary preprocessing token sequence like this and *that and "!" and \
             -13833rfa231fn7or.other.line.noise.that.happens.to.be.a.pp.number
#include <every-standard-header-here>

它在 C++11 中被添加到 <numeric> 中,并且作为扩展也出现在 the SGI STL<numeric> 中。它从未在 <algorithm> 中,因此不可能从中移动。

现在,因为允许标准库 header 以任意方式相互包含,所以在 C++11 模式中 GCC <= 5 的 <algorithm> 被包含 <random>偶然,其中包括 <numeric> 偶然。这只是一个您不应该依赖的实现细节,GCC 6 不再那样做。解决方法是简单地包含正确的 header,即并且一直是 <numeric>.