c++ 是否从其他库中隐含地包含诸如 <locale> 之类的 stl 库?

Does c++ include stl library such as <locale> implicitly from other libraries?

我正在查看 c++ 参考中为 std::tolower 给出的示例,即使我将库 替换为

,方法 std::tolower 似乎仍然有效

如果包含,是否间接导入了

示例:

// tolower example (C++)
#include <iostream> 
#include <string>
#include <locale>         // <-- works for std::locale, std::tolower
#include <algorithm>      // <-- also works for std::tolower

int main ()
{
  std::locale loc;
  std::string str="Test String.\n";
  for (std::string::size_type i=0; i<str.length(); ++i)
    std::cout << std::tolower(str[i],loc);
  return 0;
}

Is <locale> imported indirectly if <algorithm> is included?

没有导入(在 C++20 and modules) in C++11, just includes. Read n3337 (or some newer C++ standard), the documentation of your compiler (e.g. GCC) and preprocessor (e.g. GNU cpp 之前)。

如果您使用 GCC,将您的 C++ 代码 mathworker.cc 编译为 g++ -Wall -Wextra -g -H -c mathworker.cc,您将看到哪些文件是 #include-d.

某些 实现中,可能会包含 <locale>

但是,如果您关心 可移植性 到其他 C++ 编译器(如 Clang,或更新版本的 GCC)您应该明确编码 #includes 记录在例如此 C++ 参考