Why should I not #include ?

2018-12-30 23:15发布

I posted a question with my code whose only #include directive was the following:

#include <bits/stdc++.h>

My teacher told me to do this, but in the comments section I was informed that I shouldn't.

Why?

2条回答
美炸的是我
2楼-- · 2018-12-30 23:24

Why? Because it is used as if it was supposed to be a C++ standard header, but no standard mentions it. So your code is non-portable by construction. You won't find any documentation for it on http://cppreference.com So it might as well not exist. It's a figment of someone's imagination :)

查看更多
裙下三千臣
3楼-- · 2018-12-30 23:46

Including <bits/stdc++.h> appears to be an increasingly common thing to see on Stack Overflow, perhaps something newly added to a national curriculum in the current academic year.

I imagine the advantages are vaguely given thus:

  • You only need write one #include line
  • You do not need to look up which standard header everything is in

Unfortunately, this is a lazy hack, naming a GCC internal header directly instead of individual standard headers like <string>, <iostream> and <vector>. It ruins portability and fosters terrible habits.

The disadvantages include:

  • It will probably only work on that compiler
  • You have no idea what it'll do when you use it, because its contents are not set by a standard
  • Even just upgrading your compiler to its own next version may break your program
  • Every single standard header must be parsed and compiled along with your source code, which is slow and results in a bulky executable under certain compilation settings

Don't do it!


More information:

Example of why Quora is bad:

查看更多
登录 后发表回答