6

I have following code and I don't know how can I access the x inside the anonymous namespace in this setting. Please tell me how?

#include <iostream>

int x = 10;

namespace
{
    int x = 20;
}

int main(int x, char* y[])
{
    {
        int x = 30; // most recently defined
        std::cout << x << std::endl; // 30, local
        std::cout << ::x << std::endl; // 10, global
        // how can I access the x inside the anonymous namespace?
    }

    return 0;
}
RyanC
  • 189
  • 1
  • 9

2 Answers2

3

You can't!

You cannot access the namespace's members by its name, because it doesn't have one.
It's anonymous.

You can only access those members by virtue of their having been pulled into scope already.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Thanks, then why does the people made such an inaccessible variable inside the namespace? What does it mean by accessing by virtue of their having been pulled into scope already? Could you please tell me more specific? :) – RyanC Sep 16 '16 at 13:13
  • 2
    @DongkyuChoi: It means the object cannot be directly referenced outside that translation unit. It makes the object "file-local", in a sense. It's been pulled into that "file"'s scope, so you can use it there (as you show in your question), but nowhere else. – Lightness Races in Orbit Sep 16 '16 at 14:01
0

You'll have to access it from a function within the anonymous same scope:

#include <iostream>

int x = 10;

namespace
{
    int x = 20;
    int X() { return x; }
}

int main(int x, char* y[])
{
    {
        int x = 30; // most recently defined
        std::cout << x << std::endl; // 30, local
        std::cout << ::x << std::endl; // 10, global
        std::cout << X() << std::endl; // 20, anonymous
        // how can I access the x inside the anonymous namespace?
    }

    return 0;
}
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
  • no no no no! because X() is unique so if you add an outer function X() then this one will hide your inner X(). – Raindrop7 Sep 16 '16 at 13:23
  • 1
    to be able to access data inside an anonymous namespace consider: Never declare any data outside with the same name. – Raindrop7 Sep 16 '16 at 13:24
  • @Raindrop7 My answer isn't wrong. This gives you 3 identifiers named 'x', in 3 different scopes, with 3 different ways of accessing them. That's what OP asked for. Whether its the right thing to do for the sake of code quality is irrelevant to the question. – Trevor Hickey Sep 16 '16 at 13:28
  • @TrevorHickey you have a unique X() which you depend on its return type which is the value of x. it matters when you have another X() outside. what will you do to get to the inner one?? – Raindrop7 Sep 16 '16 at 13:32
  • You're talking hypotheticals. There is no other X(). I agree that its a dangerous situation that could arise, but you don't know OP's intentions. Maybe they are generating code. Maybe they are going to use a unique enough function name. Maybe they don't care. – Trevor Hickey Sep 16 '16 at 13:34
  • This doesn't answer the question. You just exchanged `x` for `X()`. Okay so the OP doesn't have a global `int X()`. But this is really just shifting the goalpoasts. Now the question might as well be _"how do I write `::X()`?"_ Adding a layer of "indirection" doesn't change anything as far as access to that anonymous namespace goes. – Lightness Races in Orbit Sep 16 '16 at 14:02