Ordering of using namespace std; and includes?

前端 未结 6 936
眼角桃花
眼角桃花 2020-12-02 00:04

I recently saw this code being used in a source file in a C++ project:

using namespace std;
#include 

Ignoring all issues o

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-02 00:47

    This code is undefined behavior [lib.using.headers]:

    A translation unit shall include a header only outside of any external declaration or definition, and shall include the header lexically before the first reference to any of the entities it declares or first defines in that translation unit.

    You reference std and then include a header that declares it. Even this is still undefined behavior:

    #include 
    using namespace std;
    #include 
    

提交回复
热议问题