问题描述
在工作中,我遇到一种情况,描述一个函数返回的结果的最佳类型是tidyr::extract(myTibble,Label,c('First','Second'),'([A-Z][a-z]+)([A-Z].*)',remove= FALSE)
# A tibble: 5 x 3
# Label First Second
# <chr> <chr> <chr>
#1 ProjectCrash Project Crash
#2 ProjectNoCrash Project NoCrash
#3 TreatmentFed Treatment Fed
#4 TreatmentPre Treatment Pre
#5 TreatmentStarve Treatment Starve
-当然,这不是有效的C ++,因为您不能有两种变体相同的类型。我可以将其表示为std::variant<uint64_t,uint64_t>
,或者该对中的第一个元素是枚举,但这是一种特殊情况。 std::pair<bool,uint64_t>
并不是那么整洁,而且我的函数式编程背景确实使我想要std::variant<uint64_t,uint64_t,bool>
-因此,我尝试使用Visitor模式来实现它,就像我在其他模式中所做的那样。不支持求和类型的本机语言:
Either
C ++拒绝这样做,因为模板方法template <typename A,typename B,typename C>
class EitherVisitor {
virtual C onLeft(const A& left) = 0;
virtual C onRight(const B& right) = 0;
};
template <typename A,typename B>
class Either {
template <typename C>
virtual C Accept(EitherVisitor<A,B,C> visitor) = 0;
};
template <typename A,typename B>
class Left: Either<A,B> {
private:
A value;
public:
Left(const A& valueIn): value(valueIn) {}
template <typename C>
virtual C Accept(EitherVisitor<A,C> visitor) {
return visitor.onLeft(value);
}
};
template <typename A,typename B>
class Right: Either<A,B> {
private:
B value;
public:
Right(const B& valueIn): value(valueIn) {}
template <typename C>
virtual C Accept(EitherVisitor<A,C> visitor) {
return visitor.onRight(value);
}
};
不能是虚拟的。有没有针对此限制的解决方法,可以让我根据其f代数和同化来正确表示基本和类型?
解决方法
也许最简单的解决方案是将T
和Right
的{{1}}周围的轻量级包装?
基本上是一个强类型别名(也可以使用Boost的strong typedef)
Left
然后我们可以区分它们以进行访问:
template<class T>
struct Left
{
T val;
};
template<class T>
struct Right
{
T val;
};
,
std::variant<X,X>
是有效的C ++。
使用起来有点尴尬,因为std::visit
不能为您提供索引,而std::get<X>
也无法使用。
解决此问题的方法是创建一个索引变体,就像一个强大的枚举。
template<std::size_t i>
using index_t = std::integral_constant<std::size_t,i>;
template<std::size_t i>
constexpr index_t<i> index = {};
template<std::size_t...Is>
using number = std::variant< index_t<Is>... >;
namespace helpers {
template<class X>
struct number_helper;
template<std::size_t...Is>
struct number_helper<std::index_sequence<Is...>> {
using type=number<Is...>;
};
}
template<std::size_t N>
using alternative = typename helpers::number_helper<std::make_index_sequence<N>>::type;
然后我们可以从变体中提取替代项:
namespace helpers {
template<class...Ts,std::size_t...Is,class R=alternative<sizeof...(Ts)>>
constexpr R get_alternative( std::variant<Ts...> const& v,std::index_sequence<Is...> ) {
constexpr R retvals[] = {
R(index<Is>)...
};
return retvals[v.index()];
}
}
template<class...Ts>
constexpr alternative<sizeof...(Ts)> get_alternative( std::variant<Ts...> const& v )
{
return helpers::get_alternative(v,std::make_index_sequence<sizeof...(Ts)>{});
}
现在您有了一个std::variant<int,int>
,您可以
auto which = get_alternative( var );
和which
是一个变体,在运行时由整数表示,该整数是var
中活动类型的索引。您可以:
std::variant<int,int> var( std::in_place_index_t<1>{},7 );
auto which = get_alternative( var );
std::visit( [&var](auto I) {
std::cout << std::get<I>(var) << "\n";
},get_alternative(var) );
并使用编译时间常数访问var
中哪个替代可能性处于活动状态。
我发现get_alternative(variant)
使variant<X,X,X>
更加有用,并填补了我认为您可能会遇到的漏洞。
现在,如果您不需要一个活动索引,则只需调用var.index()
,然后通过visit( lambda,var )
进行访问即可。
构造变量时,确实需要编译时索引来进行variant<int,int> var( std::in_place_index_t<0>{},7 )
。措辞有点尴尬,因为C ++支持相同类型的倍数变体,但它认为它们的可能性要比通用代码之外的“标准”不相交变体少。
但是我之前使用过这种alternative
和get_alternative
这样的代码来支持像数据粘贴代码这样的功能编程。