g++:应该--std选项更改我的代码使用的STL/stdlib

g++ : should --std option change which STL/stdlib my code uses?

本文关键字:STL stdlib 代码 我的 应该 --std 选项 g++      更新时间:2023-10-16

我对最近的g++(比如4.8或4.9)是否应该根据所选的--std选项引用不同的STL感到困惑。具体来说,给定选项--std=c++98 vs--std=c++11,在这种情况下,我的代码不应该看到/使用两个不同的STL吗?然而,当我使用c++98选项进行编译时,我似乎仍然得到了最新的STL,它显然不起作用,因为它使用了许多只使用c++11的东西。我在我的系统上搜索了一下,只找到了STL头文件的一个副本(c++11)。如有任何关于该如何运作的澄清,我们将不胜感激。

如果您打开了其中一些标头,那么您会在bits/stl_algo.h 中看到类似这样的开关

#if __cplusplus >= 201103L
  /**
   *  @brief  Checks that a predicate is true for all the elements
   *          of a sequence.
   *  @ingroup non_mutating_algorithms
   *  @param  __first   An input iterator.
   *  @param  __last    An input iterator.
   *  @param  __pred    A predicate.
   *  @return  True if the check is true, false otherwise.
   *
   *  Returns true if @p __pred is true for each element in the range
   *  @p [__first,__last), and false otherwise.
  */
  template<typename _InputIterator, typename _Predicate>
    inline bool
    all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
    { return __last == std::find_if_not(__first, __last, __pred); }
...
#endif

对于一个实现来说,必须为不同的-std交换机设置维护这些文件的不同副本是没有意义的。

当然可以功能

但是,在实践中,您的实现可能会在它们之间共享相同的标准库实现代码,使用宏打开和关闭功能。GCC就是这么做的。