Search code examples
c++structusing

Use a using-definition containing a struct inside that struct


I have struct similar to this one:

typedef struct foo {
      std::shared_ptr<std::vector<foo>> data;
} foo;

And I have a using definition to shorten the type (the original type is much longer, I know in this case it's not that useful):

using fooVec = std::vector<foo>;

Now, I would like to use that using inside the struct:

typedef struct foo {
      std::shared_ptr<fooVec> data;
} foo;

But... where to place the using? Before the struct, using has not enough information, because the struct is defined later. After the struct, the struct misses the using definition, because it occurs later... So - is the following valid?

struct foo;
using fooVec = std::vector<foo>;
typedef struct foo {
      std::shared_ptr<fooVec> data;
} foo;

I know a similar behaviour for classes, but structs? Not sure if that causes some trouble...

Thanks in advance!


Solution

  • Since typedef is only a type alias, and is unnecessary in C++ (struct tags can be used directly as type names), just drop it.

    struct foo;
    using fooVec = std::vector<struct foo>;
    struct foo {
          std::shared_ptr<fooVec> data;
    };