如何确定要包括哪些头文件? [英] How to determine which header files to include?

查看:106
本文介绍了如何确定要包括哪些头文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有下面的代码(非常简单).

  #include< iostream>int main(){std :: cout<<std :: stoi("12");} 

这在g ++和clang上都可以正常编译;但是,它无法在MSVC上编译,并显示以下错误:

错误C2039:"stoi":不是"std"的成员

错误C3861:"stoi":找不到标识符

我知道 std :: stoi < string> 标头的一部分,大概两个前编译器都将其作为< iostream> ,而后者则没有.根据C ++标准[res.on.headers]

C ++标头可能包括其他C ++标头.

对我来说,基本上说这三个编译器都是正确的.

当我的一个学生提交作业时,这个问题就出现了,TA将其标记为未编译;我当然去修理了.但是,我想防止将来发生此类事件.因此,有没有一种方法可以确定应包含哪些头文件,而无需在每次要检查的三个不同的编译器上进行编译?

我能想到的唯一方法是确保对于每个 std 函数调用,都存在一个适当的include.但是如果您现有的代码长数千行,则搜索起来可能很乏味.有没有更容易/更好的方法来确保交叉编译器的兼容性?

三个编译器的示例: https://godbolt.org/z/kJhS6U

解决方案

是否有更简便/更好的方法来确保交叉编译器兼容性?

如果您有一个庞大的代码库并且到目前为止还没有做过,这总是很麻烦的,但是一旦修复了包含内容,您就可以坚持一个简单的过程:/p>

编写使用标准功能的新代码时,例如 std:: stoi ,将该名称插入Google,转到cppreference.com文章,然后在顶部查看其定义在哪个标头中.

然后将其包括在内(如果尚未包含在内).工作完成!

(您可以可以使用该标准,但是访问起来不太方便.)

请不要试图以便宜,不可移植的骇客(例如< bits)将其全部解雇/stdc ++.h>


tl; dr:文档

Say I have the below (very simple) code.

#include <iostream>
int main() {
    std::cout << std::stoi("12");
}

This compiles fine on both g++ and clang; however, it fails to compile on MSVC with the following error:

error C2039: 'stoi': is not a member of 'std'

error C3861: 'stoi': identifier not found

I know that std::stoi is part of the <string> header, which presumably the two former compilers include as part of <iostream> and the latter does not. According to the C++ standard [res.on.headers]

A C++ header may include other C++ headers.

Which, to me, basically says that all three compilers are correct.

This issue arose when one of my students submitted work, which the TA marked as not compiling; I of course went and fixed it. However, I would like to prevent future incidents like this. So, is there a way to determine which header files should be included, short of compiling on three different compilers to check every time?

The only way I can think of is to ensure that for every std function call, an appropriate include exists; but if you have existing code which is thousands of lines long, this may be tedious to search through. Is there an easier/better way to ensure cross-compiler compatibility?

Example with the three compilers: https://godbolt.org/z/kJhS6U

解决方案

Is there an easier/better way to ensure cross-compiler compatibility?

This is always going to be a bit of a chore if you have a huge codebase and haven't been doing this so far, but once you've gone through fixing your includes, you can stick to a simple procedure:

When you write new code that uses a standard feature, like std::stoi, plug that name into Google, go to the cppreference.com article for it, then look at the top to see which header it's defined in.

Then include that, if it's not already included. Job done!

(You could use the standard for this, but that's not as accessible.)

Do not be tempted to sack it all off in favour of cheap, unportable hacks like <bits/stdc++.h>!


tl;dr: documentation

这篇关于如何确定要包括哪些头文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆