"invalid use of incomplete type" 有变体和 CRTP

"invalid use of incomplete type" with variants and 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, 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>>{});