1

I notice that the following code compiles with recent compilers:

int main()
{
    int x;
    struct x;
    x = 210;                  // ←
}

As I recall it didn't compile some years ago.

Were the lookup rules changed in C++11 or C++14 to make this code “work” (thus breaking use of struct variable_name; as a means to ensure no use of the variable in the following code)?


Update: Evidently I remembered incorrectly. I have verified that the code compiled OK even with Visual C++ 2010. However, when used for parameters the struct name is in an inner scope, and shadows, like in this code:

void foo( int x )
{
    struct x;
    x = 210;                  // ← Error
}

int main()
{
}

Accordingly I have selected as “solution” the answer that there was no change; the rules were always like this.

8
  • AFAIK variable names and class tags have different scopes, hence can be in same namespace Commented Jan 17, 2016 at 15:13
  • @AngelusMortis: Yes, the question concerns the assignment in the last statement. Thanks for pointing out the vagueness. Fixing. Commented Jan 17, 2016 at 15:14
  • 1
    "As I recall it didn't compile some years ago." Can you give some specific examples that we could check? Compiler version and flags? I don't share your recollection. Commented Jan 17, 2016 at 15:15
  • I couldn't get this not to compile on coliru, even at gcc 4.6 and c++98 standard it seems to be happy to compile it: coliru.stacked-crooked.com/a/bd8226b19cee26d0 Commented Jan 17, 2016 at 15:15
  • @LightnessRacesinOrbit: The only one that comes to mind was a long discussion in the C++ Lounge here on SO, involving me, Johannes Schaub (litb), I think the puppy and the Good Robot, creating ever more "perfect" and unreasonably complex versions of a macro that used this trick to declare a parameter unused and ensure it really was unused. Commented Jan 17, 2016 at 15:17

2 Answers 2

9

[basic.scope.hiding]/2 A class name (9.1) or enumeration name (7.2) can be hidden by the name of a variable, data member, function, or enumerator declared in the same scope. If a class or enumeration name and a variable, data member, function, or enumerator are declared in the same scope (in any order) with the same name, the class or enumeration name is hidden wherever the variable, data member, function, or enumerator name is visible.

This language has existed since C++98. If you've seen a compiler that worked differently, that compiler was pre-standard, or just plain buggy.

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

Comments

0

What you did was forward declarations of struct x. You did not declared and new variable called x. Example:

struct foo;
int foo;
struct foo {
    int foo;
};
struct foo thisisfoovariable;

Above are declarations of only two variables: foo (of type int) and thisisfoovariable, which type is struct foo.

Comments

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.