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?

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
secretgenes
  • 1,291
  • 1
  • 19
  • 39
  • https://stackoverflow.com/questions/34602334/how-to-access-to-anonymous-namespace-variable-if-the-same-variable-exists-in-glo This link helps a bit... no definite answer again though. – blake Aug 25 '17 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._ – user0042 Aug 25 '17 at 18:56
  • @user0042 : then you should probably explain why not !! – secretgenes Aug 25 '17 at 19:00
  • @secretgenes Well, I'm all in. Needs some more research. – user0042 Aug 25 '17 at 19:19

2 Answers2

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.

user0042
  • 7,917
  • 3
  • 24
  • 39
  • 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 Aug 25 '17 at 19:35
  • @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? – user0042 Aug 25 '17 at 19:37
  • 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. – secretgenes Aug 25 '17 at 19:56
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

}
9Breaker
  • 724
  • 6
  • 16
  • 1
    This is true, but it's a hack(flaw in c++), you can even access all the private members of class. – secretgenes Aug 30 '17 at 16:53
  • 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. – 9Breaker Aug 30 '17 at 17:00
  • 2
    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. – Alexis Wilke Sep 25 '18 at 00:26