5

This question has alreday discussed in the link unnamed namespace within named namespace but no perfect answers were provided on how to access the variables of unnamed namespace nested under named namespace in case both variables are same

Consider This Code

namespace apple {   
    namespace {
                 int a=10;
                 int b=10;
              }
   int a=20;
}


int main()
{
cout<<apple::b; //prints 10
cout<<apple::a; // prints 20
}

Unnamed namespace "variable a" is always hidden. How to access "variable a" of unnamed namespace?

Is it even legal to declare unnamed namespaces inside named namespaces?

4
  • stackoverflow.com/questions/34602334/… This link helps a bit... no definite answer again though. Commented Aug 25, 2017 at 18:52
  • @secretgenes Well, the answer might be: It's actually not possible to do what you want, but legal to write that code. Commented Aug 25, 2017 at 18:56
  • @user0042 : then you should probably explain why not !! Commented Aug 25, 2017 at 19:00
  • @secretgenes Well, I'm all in. Needs some more research. Commented Aug 25, 2017 at 19:19

2 Answers 2

1

unnamed namespace "variable a" is always hidden. How to access "variable a" of unnamed namespace?

It looks like you simply cannot qualify an unnamed namespace outside of the enclosing namespace.

Well, here's how to fix the ambiguity:

namespace apple {   
    namespace {
        int a=10;
    }

    int getPrivateA() {
        return a;
    }

    int a=20;
}

int main() {
    cout<<apple::getPrivateA() << endl;
    cout<<apple::a << endl;
}

See the Live Demo.


Though I'm aware that doesn't fully answer your question (besides if it's legal to nest unnamed namespaces inside another namespace).
I'll have to investigate what the c++ standard specification with chapters 3.4 and 7.3 a bit more to give you a definite answer why it's not possible what you want to do.

Sign up to request clarification or add additional context in comments.

3 Comments

I know it can be accessed by making different function names in namespaces, but what if the function name is also same? This is a workaround of accessing the variable of unnamed namespace. If there is no way to access it, shouldn't there be a warning or error message that it is inaccessible? Because it doesn't serve any purpose .
@secretgenes Not all cases of shadowing will raise a warning. Also that's compiler implementation specific. And why should you give these (hypothetical) functions the same name?
unnamed namespace has its own scope , so it might be possible to give same name to 2 functions. And if I've to define function to access the unnamed namespace variable then i could've done it separately, without using nested unnamed namespace (by declaring int a inside 'getPrivateA function'). In that case unnamed namespace is of no use.
0

I read this the other day and have an answer for "How to access "variable a" of unnamed namespace?"

I answer this knowing fully that it isn't a perfect answer, but it is a way to access the "a" from the unnamed namespace.

#include <iostream>
#include <stdio.h>

namespace apple {
        namespace {
                     int a=257;
                     int b=10;
                  }
       int a=20;
    }

using namespace std;

int main() {

int* theForgottenA;

// pointer arithmetic would need to be more modified if the variable before 
// apple::b was of a different type, but since both are int then it works here to subtract 1
theForgottenA = &apple::b - 1; 

cout << *theForgottenA; //output is 257

}

3 Comments

This is true, but it's a hack(flaw in c++), you can even access all the private members of class.
Yes, gotta love pointer arithmetic. Anyways I was thinking about your question and thought I would add that it is technically a way to access it.
Although this is likely to work with all C++ compilers, the fact that a is just before b is not something you should expect... also someone who makes edit unaware that they should stay together... it's not a good idea to use such hacks in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.