I tried to adapt this article to my context where the recursive variant should be defined within the scope of a template class.
Simplifying everything to the extreme, this gives me the following code snippet:
#include <variant>
struct LiteralExpr
{
int value;
};
template <typename T>
struct BaseSpace
{
struct AddExpr;
struct AddExprBox
{
const AddExpr &_impl;
public:
AddExprBox(const AddExpr &obj) {}
};
using Expr = std::variant<AddExprBox, LiteralExpr>;
struct AddExpr
{
std::variant<AddExprBox, LiteralExpr> lhs;
};
};
auto main() -> int
{
auto expr = BaseSpace<int>::Expr(LiteralExpr{2});
}
Sadly, this fails to compile on clang with the following error:
main.cpp:22:47: error: field has incomplete type 'std::variant<AddExprBox, LiteralExpr>' std::variant<AddExprBox, LiteralExpr> lhs;
If:
- I remove the copy constructor within
AddExprBox, it compiles; - I make Basespace a "non-template" class, it compiles;
- I explicitly instantiate `BaseSpace, it compiles;
Any idea of why this behavior occurs? And how to fix it?