带有变体和CRTP的“无效使用不完整类型”

问题描述

我一直在使用带有std::variant的奇怪重复模板模式(CRTP),如下所示:

#include <string>
#include <variant>
#include <vector>

template<typename T>
struct either {
    std::vector<T> arg;
};

template<typename T>
struct maybe_either: std::variant<T,either<maybe_either<T>>> {

    template<typename U>
    maybe_either(U&& v):
        std::variant<T,either<maybe_either<T>>>(std::forward<U>(v)) {
    }
};

struct var {
  std::string name;
};

int main(int,char**) {
    auto expression = maybe_either<var>(either<var>{});
    return 0;
}

使用g++ -c -std=c++17 show.cpp进行编译时,在尝试解析目标构造函数时会产生以下错误

/usr/include/c++/7/variant:953:2: note: candidate: template<class _Tp,class,class> constexpr std::variant<_Types>::variant(_Tp&&)
  variant(_Tp&& __t)
  ^~~~~~~
/usr/include/c++/7/variant:953:2: note:   template argument deduction/substitution Failed:
/usr/include/c++/7/variant:951:6: error: invalid use of incomplete type ‘struct std::variant<var,either<maybe_either<var> > >::__to_type_impl<18446744073709551615,false>’
         typename = enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      && is_constructible_v<__accepted_type<_Tp&&>,_Tp&&>>>
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here is the full output

我的GCC版本:

$ g++ --version
g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0

为什么编译失败?

您如何建议写等同的东西?

解决方法

错误消息尚不清楚,但实际上这一行有一个错误:

auto expression = maybe_either<var>(either<var>{});

either<var>不是该变体的可接受类型,但是either<maybe_either<var>>是该变量。

改为撰写此作品,

auto expression = maybe_either<var>(either<maybe_either<var>>{});

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...