0

I need to resolve this thing somehow:

  class MyType; // this thing doesn't help here
  typedef std::stack<boost::variant<int, std::function<shared_ptr<MyType>()>>> MyType;

I get an error like this one

 error C2371: 'MyType': redefinition; different basic types

Any help will be appreciate.

Edit:

This can be done easily with using structure as proxy:

struct MyStruct;
typedef std::stack<boost::variant<int, std::function<shared_ptr<MyStruct>()>>> MyType;
struct MyStruct {
    MyType data;
};

But must be more handy way to do this.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Hakuhonoo
  • 33
  • 1
  • 7
  • I'm not sure that it is possible with either `typedef` or `using`. However, in the abstract, this question doesn't necessarily concern boost. A generic template could be constructed to cause this problem. Posing the question in a more general way may help attract more attention to the problem. – Robert Prévost Oct 19 '16 at 22:41
  • Yes, you right, Robert. I have made it more general. – Hakuhonoo Oct 20 '16 at 08:24

1 Answers1

0

You can't do this, because what you are asking for causes an infinite recursion:

typedef std::stack<boost::variant<int, std::function<shared_ptr<MyType>()>>> MyType;
MyType a;

would expand to:

std::stack<boost::variant<int, std::function<shared_ptr<
    std::stack<boost::variant<int, std::function<shared_ptr<
        std::stack<boost::variant<int, std::function<shared_ptr<
            *Infinitely many more here*
        ()>>>>
    ()>>>>
()>>> a;
midor
  • 5,487
  • 2
  • 23
  • 52
  • Yes, this is what I need. Potentially infinite deep structure. – Hakuhonoo Oct 20 '16 at 09:55
  • Yes, an potentially infinitely deep *structure*, but not a infinitely deep *type*. Without a better explanation I can only guess, but I would expect that what you really want is a stack of variants which are either `int` or `function`-pointers. – midor Oct 20 '16 at 09:59
  • Perhaps I should do it with struct proxy (as I add to the head of this post), but still it looks odd for me. Or... I can return from function not `MyType`, but `void*` and then cast in to `MyType`. Perversion from any side. – Hakuhonoo Oct 20 '16 at 10:08
  • I guess, I'll stop on struct proxy for now – Hakuhonoo Oct 21 '16 at 14:58