Interdependent using definitions

I have several dependent template instances. I usually just post an ad, but I don’t understand 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;
}

It is impossible to record Tuplefirst because of the need TupleContainer, it cannot record TupleContainerfirst because of the need Tuple.

How can I forward the declaration of one of the definitions used?

+4
source share
2 answers

I managed to do this by using a thin wrapper class around std :: tuple and using the 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;
}
+2
source

You need a real pointer somewhere, as if you were trying to find a definition of a recursive structure, this would not work, because it could not stop.

using my_tuple = std::tuple<int, std::tuple<int>*>;
-3

Source: https://habr.com/ru/post/1692340/


All Articles