相关疑难解决方法(0)

C++风格问题:#include是什么?

考虑这个翻译单位:

#include <map>
#include <string>
int main()
{
   std::map<std::string, std::size_t> mp;
   mp.insert(std::make_pair("hello", 42)); 
}
Run Code Online (Sandbox Code Playgroud)

这个翻译单元有两件事困扰着我,他们是

  • 的std ::为size_t
  • 的std :: make_pair

我刚才假设<cstddef><utility>必须已#include被D <string><map>.
这个假设有多合适?至少make_pair我认为有一个非常强大的保证,因为地图成员接口使用std::pair.因为std::size_t没有正式的保证,但是一旦你包括map或者,它很可能是可用的string.第一个风格问题是,您是否明确包含<cstddef><utility>在此翻译单元中?

这部分部分处理了已经包含的一些标题的不确定性.但是,问题的第二部分.假设我们有这个

//f.h
#ifndef BIG_F_GUARD
#define BIG_F_GUARD
#include <string>
std::string f();
#endif   

//f.cpp
#include "f.h"
std::string f()
{
    std::string s;
    return s;
}
Run Code Online (Sandbox Code Playgroud)

第二个问题是:你明确#include <string>到f.cpp吗?

我想我的问题很明确.顺便说一句.这两个问题后面都是一个很大的WHY:)谢谢.

c++ coding-style include

9
推荐指数
1
解决办法
751
查看次数

标签 统计

c++ ×1

coding-style ×1

include ×1