0

Consider this situation:

struct Foo
{
    struct Bar
    {
    };
};

How do I forward-declare Foo::Bar?


What I tried:

struct Foo::Bar;

error: 'Foo' has not been declared

struct Foo;
struct Foo::Bar;

error: 'Bar' in 'struct Foo' does not name a type

namespace Foo
{
    struct Bar;
}
// ...
struct Foo
{
    struct Bar
    {
    };
};

error: 'struct Foo' redeclared as different kind of symbol

struct Foo
{
    struct Bar;
};
// ...
struct Foo
{
    struct Bar
    {
    };
};

error: redefinition of 'struct Foo'

Ivan Rubinson
  • 3,001
  • 4
  • 19
  • 48
  • `struct Foo { struct Bar; };` then `struct Foo::Bar { ... };` – BeyelerStudios Nov 07 '16 at 13:10
  • @BoPersson of course you can forward declare a nested type, which is not the topic of http://stackoverflow.com/questions/951234/forward-declaration-of-nested-types-classes-in-c – BeyelerStudios Nov 07 '16 at 13:15
  • @BoPersson [this](http://stackoverflow.com/questions/11872728/forward-declaration-of-a-nested-class) is actually an exact duplicate of this question – BeyelerStudios Nov 07 '16 at 13:17
  • @BeyelerStudios - One problem with `struct Foo { struct Bar; };` is that although you have forward declared `Bar`, you have also fully declared `Foo` without any other members. I can't see that this is useful very often, or preferred over `namespace Foo { struct Bar; };` – Bo Persson Nov 07 '16 at 13:29
  • 1
    @BoPersson this is very common in the PIMPL idiom (and yes, you have to fully specify `Foo`) – BeyelerStudios Nov 07 '16 at 13:32
  • @Ivan can you specify your usecase? – BeyelerStudios Nov 07 '16 at 13:36

0 Answers0