STL 是否为其 header 指定了 header 守卫?

Does the STL specify the header guards for its headers?

我想在我的 header 中提供一些使用 std::string 的内联便利函数以及使用 const char * 的库函数,但我不想包括 <string>.我想检查 #ifdef 是否包含 <string> 并在这种情况下提供便利功能。

问题:STL header 中 header 守卫的名称对于所有 STL 实现都相同吗? Visual Studio 2010年,<string>的header后卫是_STRING_

这是个坏主意™。

通常,您不能也不应该依赖编译器/库的实现细节1。最重要的是,正如 Fire Lancer 在评论中所述,"having includes have different effects based on order will confuse people" 并添加到您的图书馆的 wtf/line。

您可以(应该?)做的是记录一个宏供用户定义以启用您的 std::string 功能:

#ifdef MY_LIBRARY_ENABLE_STRING_FUNCTIONS
void print(std::string message);
#endif // MY_LIBRARY_ENABLE_STRING_FUNCTIONS

如果用户想要它们,他们必须:

#define MY_LIBRARY_ENABLE_STRING_FUNCTIONS
#include <my_library>

1) C++17 有 __has_include(<filename>) macro (credit to acraig5075 用于向我学习这个)这没有帮助,因为它 returns 是否包含可用,如果已包含则不可用。

最可靠的检查方法可能是使用 技术。

也就是说,不要这样做,当您想区分是否包含 header 时,一切都会尖叫 anti-pattern ,或包含的顺序。