Ordering of using namespace std; and includes?

前端 未结 6 938
眼角桃花
眼角桃花 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:29

    From SO/IEC 14882:2003

    [7.3.3.9] The entity declared by a using-declaration shall be known in the context using it according to its definition at the point of the using-declaration. Definitions added to the namespace after the using-declaration are not considered when a use of the name is made.

    [3.4.3.2.2] Given X::m (where X is a user-declared namespace), or given ::m (where X is the global namespace), let S be the set of all declarations of m in X and in the transitive closure of all namespaces nominated by using-directives in X and its used namespaces, except that using-directives are ignored in any namespace, including X, directly containing one or more declarations of m. No namespace is searched more than once in the lookup of a name. If S is the empty set, the program is ill-formed. Otherwise, if S has exactly one member, or if the context of the reference is a using-declaration (7.3.3), S is the required set of declarations of m. Otherwise if the use of m is not one that allows a unique declaration to be chosen from S, the program is ill-formed

    So if it happens to work, it's a fluke and not portable.

提交回复
热议问题