3

I realize that the following is horrible style, but for the sake of argument, assume I have the following code:

struct parent
{
   virtual ~parent() {}
};

struct child : public parent
{
   child() {}
   virtual ~child() {}
};

struct anotherClass
{
   static parent& anyName;
};

child anyName; // create an instance of 'child'
parent& anotherClass::anyName = anyName; // create a parent-class ref to the child object

When I initialize the anotherClass::anyName reference above with anyName, which anyName am I initializing it with? The child-class object, or with itself? (To which entity does the last anyName in the last line refer? Is it ambiguous?) And where in the C++ spec would something like this be addressed?

(BTW this question has absolutely nothing to do with my other question posted a few minutes earlier.)

xskxzr
  • 12,442
  • 12
  • 37
  • 77
phonetagger
  • 7,701
  • 3
  • 31
  • 55
  • 1
    @phonetagger what makes you think it is initializing `parent& anotherClass::anyName = anyName` with itself? If it compiles, it is pretty clear that `anyName` is the `child` object. – Ghasem Naddaf Aug 23 '18 at 23:37
  • @GhasemNaddaf If you are correct, then apparently this is another compiler bug. I tried this code out (well not this exact version) on my embedded system, and it crashed during initialization of `anotherClass:anyName` when they were the same name (jumped to address 0x00000000). When the only change was the name, suddenly it no longer crashed. (BTW this cannot be the same issue as my other question referred to at the end of my question above; that's an entirely different issue.) – phonetagger Aug 23 '18 at 23:42
  • Definitions of class members looks up names from class scope, this is not a bug. You can generally trust your compiler. BTW you should write what you have tried in the post. – Passer By Aug 24 '18 at 00:35

1 Answers1

5

With itself.

Unlike your other question, this one can be solved through a bit of sample code. I plugged the code from your question into an online compiler (clang) ...

http://rextester.com/EDW85421

The compiler's response is pretty clear:

warning: reference 'anyName' is not yet bound to a value when used within its own initialization

(Try commenting-out your child anyName; line; note that the compilation result doesn't change, because the compiler wasn't finding that object anyway.)

The rules from the C++ Standard that apply are in [basic.lookup.unqual]:

A name used in the definition of a static data member of class X (after the qualified-id of the static member) is looked up as if the name was used in a member function of X.

and from [basic.scope.pdecl]

The point of declaration for a name is immediately after its complete declarator and before its initializer (if any), except as noted below.

These two rules together ensure that the initializer's unqualified lookup of anyName finds anotherClass::anyName.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • I think you mean to start your answer with "with the child-class object". – phonetagger Aug 24 '18 at 01:10
  • @phonetagger: No, I don't. That name is hidden, the initialization finds `anotherClass::anyName`. – Ben Voigt Aug 24 '18 at 01:11
  • Ahhh, I misunderstood based on the first line of your original answer. I thought that commenting-out that line was what caused the warning to appear; in fact it appears whether the `child anyName` is commented-out or not. I wish my compiler gave the same warning, it would have saved me a lot of troubleshooting! Thank you for your insight. – phonetagger Aug 24 '18 at 01:28
  • @BenVoigt I assume the possible fix is `parent& anotherClass::anyName = ::anyName;`. Unfortunately, our company's firewall (the damn thing) blocks your `rextester` link. Could you expose the sample code in your answer? – Scheff's Cat Aug 24 '18 at 06:00
  • @phonetagger I just reproduced your issue on wandbox.org: `gcc HEAD` - no warning. `clang HEAD`: `warning: reference 'anyName' is not yet bound to a value when used within its own initialization [-Wuninitialized]`. So, Ben seems to use clang. (And, I bet it's not available for your embedded system (too sad), or is it?) – Scheff's Cat Aug 24 '18 at 06:08
  • @Scheff No, we use the IAR compiler. And you're right, Ben selected "C++ (clang)" as his compiler on rextester.com. If I change the compiler to "C++ (gcc)", the warning disappears. – phonetagger Aug 24 '18 at 13:36