小编M. *_*tri的帖子

有C++标准库ifdef或ifndef预处理器指令吗?

我正在用C++构建自己的终端应用程序项目,我问自己标准库是否有ifdef或ifndef预处理器指令.我想知道,因为我需要创建不同的头文件,需要一些标准库头文件,如"字符串"和其他一些,我不想包括相同的库3次或更多次,因为它使程序更重.
例如,我在我的头文件上写了这样的东西,以防止.h文件被包含多次:

#ifndef myheader_h
#define myheader_h
    // my file code here
#endif
Run Code Online (Sandbox Code Playgroud)

我尝试编译,但编译器没有说错误或警告.
我还尝试阅读标准库源代码(https://en.cppreference.com/w/cpp/header),但我没有找到任何预处理器规则,如ifdef或ifndef.
我应该包括这样的标准库标题吗?

#ifndef string_h
#define string_h
    #include <string>
#endif
Run Code Online (Sandbox Code Playgroud)

我希望我的问题还没有被问到,因为我在搜索时没有找到它.

更新

对于那些说"你不在你需要担心的位置"的人,以及谁说"如果它有适当的包括警卫就花费很少",我的意思是:程序的沉重很重要,我想让它变得更轻松所以我不想多次完全包含同一个文件.有std lib文件正确包含警卫吗?(我的头文件有它们,不知道std lib文件)

c++ preprocessor standard-library

0
推荐指数
1
解决办法
699
查看次数

C++如何检查char变量是否未定义(未初始化)

如何检查char变量是否为空?
我的意思是使用类似empty()方法检查字符串,如果字符串不包含任何字符stringVar.empty()将导致true.如何检查char变量是否不包含字符?
例如,我有一个这样的代码:

// all libraries are included before and code is simplified because it is very long
std::fstream file;
char mychar;

file.open("userselectedfile.txt", std::fstream::in);
if (file.is_open() == true) {
   while (file.eof() != true) {
      // check if mychar is initialized yet (first time that the while execute)
      if (mychar == '') {
         mychar = file.get();
         // do something special beacuse mychar wasn't initialized
         // do something with other files
      } else {
         mychar = file.get();
         // …
Run Code Online (Sandbox Code Playgroud)

c++ undefined

0
推荐指数
1
解决办法
1647
查看次数

标签 统计

c++ ×2

preprocessor ×1

standard-library ×1

undefined ×1