3

I came across this odd forward declaration (or something) where instead of having a normal:

class A;
class B{
    A* var;
}

You can do

class B{
   class A* var;
}

Now this might be an elaborated type specifier, but I'm not sure about that. I've tried it myself and I had no issues, even made my code a lot more cleaner, but I'm afraid it may cause scoping issues that I'm currently unaware of.

Does anyone have an insight on this? is this a valid forward deceleration?

example: https://youtu.be/EIptJ0YrYg0?t=412

1

3 Answers 3

2

Note that the doc says:

Note that a new class name may also be introduced by an elaborated type specifier which appears as part of another declaration, but only if name lookup can't find a previously declared class with the same name.

Thus, if class B is enclosed in a namespace and no previous forward declaration of A is performed, then it refers to n2::A that will have to be defined.

3
  • No, it is not B::A Aug 20, 2020 at 9:47
  • @StoryTeller-UnslanderMonica Ok. It is just about enclosing namespace?
    – nop666
    Aug 20, 2020 at 9:49
  • No, block scope also plays an effect here. If you are inside a block in a function, it will introduce a local class. Aug 20, 2020 at 9:51
1

Now this might be an elaborated type specifier, but I'm not sure about that.

It is. In fact, class A; is a declaration to uses an elaborate class specifier.

but I'm afraid it may cause scoping issues that I'm currently unaware of

The only scoping related point you should be aware of is that if the elaborate type specifier (that is not a declaration by itself) is the first time class is referenced, the behavior is the same as if you introduced a forward declaration to the nearest namespace or block scope. So for instance

namespace N {
  class foo {
     class bar {
       void func(class baz*);
     };
  };
};

... is the same as

namespace N {
  class baz;
  class foo {
     class bar {
       void func(baz*);
     };
  };
};
1

Yes, it's a valid forward declaration. It's called an elaborate type specifier.

To quote from elaborated type specifier from cppreference.

If the name lookup does not find a previously declared type name, the elaborated-type-specifier is introduced by class, struct, or union (i.e. not by enum), and class-name is an unqualified identifier, then the elaborated-type-specifier is a class declaration of the class-name.

It can also be used to refer to an already declared class even if it's name is hidden by non-type declaration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.