Unnamed namespace access rules

前端 未结 2 2146
离开以前
离开以前 2021-01-05 12:15

I was looking over section 7.3.1.1 in the C++03 standard expecting to find some description of the access rules for items defined in an unnamed namespace.

2条回答
  •  粉色の甜心
    2021-01-05 12:27

    An anonymous namespace is basically treated as:

    namespace unique_per_TU
    {
        // Stuff
    }
    using namespace unique_per_TU;
    

    I'll try to find the reference here in a minute.

    EDIT:

    It appears you already found it in 7.3.1.1/1

    An unnamed namespace definition behaves as if it were replaced by

    namespace unique { /* empty body */ }
    using namespace unique;
    namespace unique { namespacebody }
    

    where all occurrences of unique in a translation unit are replaced by the same identifier and this identifier differs from all other identifiers in the entire program.

    The "fake" using already brings the namespace members into the global namespace as you discovered.

提交回复
热议问题