问题描述
我一直在使用带有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&&>>>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我的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>>{});