Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8746579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:13:01+00:00 2026-06-13T12:13:01+00:00

It is legal in C++ to declare nested anonymous namespaces in the following manner:

  • 0

It is legal in C++ to declare nested anonymous namespaces in the following manner:

namespace {
    namespace {
        struct Foo
        {
        };
    }
}

namespace {    
    struct Foo // SAME IDENTIFIER AS <unnamed>::<unnamed>::Foo!!!
    {
    };
}

However, how would you declare an identifier using an explicitly typed Foo to avoid ambiguity?

EDITED — for all of you who do not read the question.

p.s. I have no intentions to use this sort of constructs, but I need to understand whether it is possible to disambiguate Foo in case someone finds a legitimate use for it. My compiler extension needs to handle all cases.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:13:01+00:00Added an answer on June 13, 2026 at 12:13 pm

    The C++ standard states it pretty clearly in 7.3.1.1p1:

    An unnamed-namespace-definition behaves as if it were replaced by

    inline(opt) namespace unique { /* empty body */ }
    using namespace unique ;
    namespace unique { namespace-body }
    

    where inline appears if and only if it appears in the
    unnamed-namespace-definition, 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.

    So from above, we know that your code actually translates to the following:

    namespace unique1 {}
    using namespace unique1;
    namespace unique1 {
        namespace unique2 {}
        using namespace unique2;
        namespace unique2 {
    
            struct Foo
            {
            };
    
        }
    }
    
    namespace unique3 {}
    using namespace unique3;
    namespace unique3 {
    
        struct Foo
        {
        };
    
    }
    

    Therefore, your first Foo can only be accessed within namespace unique1 and the second Foo can be accessed in the global namespace due to using namespace unique3;. I was wrong. Corrected by comments below.

    Then from 7.3.4p4:

    For unqualified lookup (3.4.1), the using-directive is transitive: if
    a scope contains a using-directive that nominates a second namespace
    that itself contains using-directives, the effect is as if the
    using-directives from the second namespace also appeared in the first.

    Therefore, when you refer to Foo, it can mean either unique1::unique2::Foo or unique3::Foo, which is an error. Note that the other answer says: has hidden unique name that cannot be accessed. This is incorrect, they can be accessed due to the using directives, it is just that both names are visible.

    However, by prepending the scope resolution operator :: to Foo you can access unique3::Foo because of the following:

    From 3.4.3.2p2:

    For a namespace X and name m, the namespace-qualified lookup set
    S(X,m) is defined as follows: Let S0(X,m) be the set of all
    declarations of m in X and the inline namespace set of X (7.3.1). If
    S0(X,m) is not empty, S(X,m) is S0(X,m); otherwise, S(X,m) is the
    union of S(Ni,m) for all namespaces Ni nominated by using-directives
    in X and its inline namespace set
    .

    The emphasized part says using-directives in X, which in your case means using namespace unique1; and using namespace unique3;, so the namespace-qualified lookup set looks like this: S(unique3::Foo), which means unique1::unique2::Foo is not included in the set and therefore is not an error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this legal C++? struct foo { int a[100]; int b[sizeof(a) / sizeof(a[0])]; };
Is following code legal C++ or not? class Foo { class Bar; void HaveADrink(Bar
I sometimes declare classes in nested namespaces and when it comes to defining their
Is this legal and/or good practice? #define SOFTWARE_VERSION_NUMBER 7.0v1.1 Want struct to always contain
Is the following code the case of legal forward referencing? if yes why? public
Is the following code legal? int add(int a, int b) { return a +
Is it valid and legal and to down-cast in the following case: public interface
Why It's not legal having int inside a namespace? int is a type. right?
I'm fighting the following proposal now, and I want to know legal and for
Is it legal to forward declare in another header's file? For example: #ifndef _MAIN_H_

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.