Search code examples
c++templatesvisual-c++usingc++17

Mutually dependant using definitions


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?


Solution

  • 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;
    }