4

I have some mutually dependent template instances. Normally I'd just forward declare them but I don't see how this is possible. Here is an example

#include <tuple>
#include <memory>

using Tuple = std::tuple<int,TupleContainer>;
using TupleContainer = std::unique_ptr<Tuple>;

int main()
{
    return 0;
}

Can't write Tuple first due to needing TupleContainer, can't write TupleContainer first due to needing Tuple.

How can I forward declare one of the using definitions?

Bomaz
  • 1,871
  • 1
  • 17
  • 22

2 Answers2

2

I managed to do it by using a thin wrapper class around std::tuple and using a forward declaration.

#include <tuple>
#include <memory>

struct Tuple;
using TupleContainer = std::unique_ptr<Tuple>;

struct Tuple : public std::tuple<int,TupleContainer>{
    using std::tuple<int,TupleContainer>::tuple;
};

int main()
{
    return 0;
}
Bomaz
  • 1,871
  • 1
  • 17
  • 22
-3

You need a real pointer somewhere, just like if you were to try and have a recursive structure definition it fails because it wouldn't be able to stop.

using my_tuple = std::tuple<int, std::tuple<int>*>;
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23