我一直在使用奇怪的循环模板模式(CRTP)和一个std::variant,如下所示:
#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&&>>>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~我的版本的GCC:
$ g++ --version
g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0为什么编译失败?
你如何推荐写一些等价的东西?
发布于 2020-08-27 18:48:17
错误消息不清楚,但实际上这一行有一个错误:
auto expression = maybe_either<var>(either<var>{});对于该变体,either<var>不是可接受的类型,但either<maybe_either<var>>是。
相反,写这段代码是可行的:
auto expression = maybe_either<var>(either<maybe_either<var>>{});https://stackoverflow.com/questions/63614268
复制相似问题