Is it correct to have a variable with the same name as the namespace?

Is this right to do?

namespace name {
    int name;
}

void proc(int name)
{
    name::name = name;
}

int main()
{
    int name = name::name;   
    return 0;
}

He works at GCC. But is this normal with standard and other compilers?

+4
source share
2 answers

Yes, this is normal, we need to look at how the scope resolution operator works in this context. If we look at a draft C ++ project, a 3.4.3 qualified name lookup actually has a very similar example, it says (my attention):

n:: decltype, , : , , . , , . [:

class A {
public:
    static int n;
};

int main() {
    int A;
    A::n = 42; // OK
    A b; // ill-formed: A does not name a type
}

-end ]

+4

, . /.

- , , ++.

+1

Source: https://habr.com/ru/post/1526555/


All Articles