我正在模板元程序中实现合并排序。 (信不信由你,我们在生产中有一个真实的用例。)
我的代码正在运行,我的测试正在通过,但我意识到在Merge
函数中,当我这样做时:
using type = typename std::conditional<Compare<L1, R1>::value,
...,
...>::type;
它将实例化分支的两侧,而不仅仅是一侧。这将使时间复杂度变为二次方(或更差?gulp),而不是n log n
。
如何在模板元程序中模仿三元运算符? :
的短路行为,以便只完成分支一侧的工作?
可悲的是,我不能在这里使用C ++ 17 if constexpr
,这将是完美的。它必须在C ++ 14中工作,或者更确切地说,是由gcc-5.4
我最初的想法是像这样使用SFINAE:
template <typename L1, typename R1,
typename <typename, typename> typename Compare,
typename TL, typename TR,
std::enable_if_t<Compare<L1, R1>::value> * dummy = nullptr>
Concat<TypeList<L1>, Merge_s<TL, Concat<TypeList<R1>, TR>, C> merge_branch();
template <typename L1, typename R1,
typename <typename, typename> typename Compare,
typename TL, typename TR,
std::enable_if_t<!Compare<L1, R1>::value> * dummy = nullptr>
Concat<TypeList<R1>, Merge_s<Concat<TypeList<L1>, TL>, TR, C> merge_branch();
但是,我不确定这实际上是否会按预期工作 - 当模板参数扣除在dummy
上失败时,是否会阻止编译器实例化返回类型?我应该使用额外的间接级别(甚至会有帮助吗?)
有人建议我可以使用标签发送代替SFINAE。
模板实例化是作为重载解析的副产品发生的,还是仅在重载解析完成后才会发生?
我担心答案是,作为重载解析的副产品。
当参数dummy
在上面失败时,gcc和clang是否会在实例化模板时提前解救,或者它们是否总是实例化返回类型?
这是我的MVCE:
#include <cstddef>
#include <type_traits>
#include <utility>
template <typename ... Ts>
struct TypeList {
static constexpr size_t size = sizeof...(Ts);
};
// Metafunction First: Get first type from a typelist
template<typename T>
struct First_s;
template<typename T, typename... TL>
struct First_s <TypeList<T, TL...>> {
using type = T;
};
template<typename T>
using First = typename First_s<T>::type;
// Metafunction Concat: Concatenate two typelists
template<typename L, typename R>
struct Concat_s;
template<typename... TL, typename... TR>
struct Concat_s <TypeList<TL...>, TypeList<TR...>> {
using type = TypeList<TL..., TR...>;
};
template<typename L, typename R>
using Concat = typename Concat_s<L,R>::type;
// Metafunction Split: Split a typelist at a particular index
template<int i, typename TL>
struct Split;
template<int k, typename... TL>
struct Split<k, TypeList<TL...>> {
private:
using FirstSplit = Split<k/2, TypeList<TL...>>;
using SecondSplit = Split<k-k/2, typename FirstSplit::R>;
public:
using L = Concat<typename FirstSplit::L, typename SecondSplit::L>;
using R = typename SecondSplit::R;
};
template<typename T, typename... TL>
struct Split<0, TypeList<T, TL...>> {
using L = TypeList<>;
using R = TypeList<T, TL...>;
};
template<typename T, typename... TL>
struct Split<1, TypeList<T, TL...>> {
using L = TypeList<T>;
using R = TypeList<TL...>;
};
template<int k>
struct Split<k, TypeList<>> {
using L = TypeList<>;
using R = TypeList<>;
};
// Metafunction Subdivide: Split a typelist into two roughly equal typelists
template<typename TL>
struct Subdivide : Split<TL::size / 2, TL> {};
// Metafunction Reverse: Reverse a typelist
template <typename TL>
struct Reverse_s {
using S = Subdivide<TL>;
using type = Concat<typename Reverse_s<typename S::R>::type,
typename Reverse_s<typename S::L>::type>;
};
template <typename T>
struct Reverse_s<TypeList<T>> {
using type = TypeList<T>;
};
template <>
struct Reverse_s<TypeList<>> {
using type = TypeList<>;
};
template <typename TL>
using Reverse = typename Reverse_s<TL>::type;
// Metafunction MergeSort: Mergesort a typelist, using a comparator C
// Merge takes two type lists, and a comparator metafunction.
// The comparator should take two type parameters and declare `static constexpr bool value = ...`
template <typename TL, typename TR, template <typename, typename> class C>
struct Merge_s;
// TODO: Use SFINAE for the branch here because std::conditional does not short circuit :(
/*
template <typename L1, typename R1, typename <typename, typename> typename C, typename TL, typename TR, std::enable_if_t<C<L1, R1>::value> * dummy = nullptr>
Concat<TypeList<L1>, Merge_s<TL, Concat<TypeList<R1>, TR>, C> merge_branch();
template <typename L1, typename R1, typename <typename, typename> typename C, typename TL, typename TR, std::enable_if_t<!C<L1, R1>::value> * dummy = nullptr>
Concat<TypeList<R1>, Merge_s<Concat<TypeList<L1>, TL>, TR, C> merge_branch();
*/
template <template <typename, typename> class C>
struct Merge_s<TypeList<>, TypeList<>, C> {
using type = TypeList<>;
};
template <typename L1, typename ... Ls, template <typename, typename> class C>
struct Merge_s<TypeList<L1, Ls...>, TypeList<>, C> {
using type = TypeList<L1, Ls...>;
};
template <typename R1, typename ... Rs, template <typename, typename> class C>
struct Merge_s<TypeList<>, TypeList<R1, Rs...>, C> {
using type = TypeList<R1, Rs...>;
};
template <typename L1, typename R1, template <typename, typename> class C, typename TL, typename TR>
using merge_branch = typename std::conditional<C<L1, R1>::value,
Concat<TypeList<L1>, typename Merge_s<TL, Concat<TypeList<R1>, TR>, C>::type>,
Concat<TypeList<R1>, typename Merge_s<Concat<TypeList<L1>, TL>, TR, C>::type>>::type;
template <typename L1, typename... Ls, typename R1, typename ... Rs, template <typename, typename> class C>
struct Merge_s<TypeList<L1, Ls...>, TypeList<R1, Rs...>, C> {
using type = merge_branch<L1, R1, C, TypeList<Ls...>, TypeList<Rs...>>;
};
template <typename TL, typename TR, template <typename, typename> class C>
using Merge = typename Merge_s<TL, TR, C>::type;
// Here is merge sort
template <typename T, template <typename, typename> class C>
struct MergeSort_s;
template <template <typename, typename> class C>
struct MergeSort_s<TypeList<>, C> {
using type = TypeList<>;
};
template <typename T, template <typename, typename> class C>
struct MergeSort_s<TypeList<T>, C> {
using type = TypeList<T>;
};
template <typename T, typename... Ts, template <typename, typename> class C>
struct MergeSort_s <TypeList<T, Ts...>, C>{
using S = Subdivide<TypeList<T, Ts...>>;
using L = typename MergeSort_s<typename S::L, C>::type;
using R = typename MergeSort_s<typename S::R, C>::type;
using type = Merge<L, R, C>;
};
template <typename T, template <typename, typename> class C>
using MergeSort = typename MergeSort_s<T, C>::type;
// Tests
struct A{};
struct B{};
struct C{};
// Concat tests
static_assert(std::is_same<TypeList<A, B, C>, //
Concat<TypeList<>, TypeList<A, B, C>>>::value, ""); //
static_assert(std::is_same<TypeList<A, B, C>, //
Concat<TypeList<A>, TypeList<B, C>>>::value, ""); //
static_assert(std::is_same<TypeList<A, B, C>, //
Concat<TypeList<A, B>, TypeList<C>>>::value, ""); //
static_assert(std::is_same<TypeList<A, B, C>, //
Concat<TypeList<A, B, C>, TypeList<>>>::value, ""); //
// Split tests
static_assert(std::is_same<TypeList<A>, //
typename Split<1, TypeList<A, B, C>>::L>::value, ""); //
static_assert(std::is_same<TypeList<B, C>, //
typename Split<1, TypeList<A, B, C>>::R>::value, ""); //
static_assert(std::is_same<TypeList<A, B>, //
typename Split<2, TypeList<A, B, C>>::L>::value, ""); //
static_assert(std::is_same<TypeList<C>, //
typename Split<2, TypeList<A, B, C>>::R>::value, ""); //
// Reverse tests
static_assert(std::is_same<TypeList<B, A>, //
Reverse<TypeList<A, B>>>::value, ""); //
static_assert(std::is_same<TypeList<C, B, A>,//
Reverse<TypeList<A, B, C>>>::value, ""); //
// Sorting tests
template <typename T1, typename T2>
struct IntCmp;
template <int a, int b>
struct IntCmp<std::integral_constant<int, a>, std::integral_constant<int, b>> {
static constexpr bool value = (a < b);
};
template <int x>
using IntC = std::integral_constant<int, x>;
static_assert(std::is_same<TypeList<IntC<1>, IntC<2>>, //
MergeSort<TypeList<IntC<1>, IntC<2>>, IntCmp>>::value, ""); //
static_assert(std::is_same<TypeList<IntC<1>, IntC<2>>,//
MergeSort<TypeList<IntC<2>, IntC<1>>, IntCmp>>::value, "");//
static_assert(std::is_same<TypeList<IntC<1>, IntC<2>, IntC<3>>,//
MergeSort<TypeList<IntC<3>, IntC<1>, IntC<2>>, IntCmp>>::value, "");//
static_assert(std::is_same<TypeList<IntC<1>, IntC<2>, IntC<3>>,//
MergeSort<TypeList<IntC<1>, IntC<3>, IntC<2>>, IntCmp>>::value, "");//
static_assert(std::is_same<TypeList<IntC<1>, IntC<2>, IntC<3>>,//
MergeSort<TypeList<IntC<2>, IntC<3>, IntC<1>>, IntCmp>>::value, "");//
static_assert(std::is_same<TypeList<IntC<1>, IntC<2>, IntC<3>>,//
MergeSort<TypeList<IntC<1>, IntC<2>, IntC<3>>, IntCmp>>::value, "");//
static_assert(std::is_same<TypeList<IntC<1>, IntC<2>, IntC<3>>,//
MergeSort<TypeList<IntC<2>, IntC<1>, IntC<3>>, IntCmp>>::value, "");//
static_assert(std::is_same<TypeList<IntC<1>, IntC<2>, IntC<3>>,//
MergeSort<TypeList<IntC<1>, IntC<2>, IntC<3>>, IntCmp>>::value, "");//
static_assert(std::is_same<TypeList<IntC<1>, IntC<2>, IntC<3>, IntC<4>>,//
MergeSort<TypeList<IntC<1>, IntC<2>, IntC<3>, IntC<4>>, IntCmp>>::value, "");//
static_assert(std::is_same<TypeList<IntC<1>, IntC<2>, IntC<3>, IntC<4>>,//
MergeSort<TypeList<IntC<3>, IntC<4>, IntC<2>, IntC<1>>, IntCmp>>::value, "");//
归因:上面的一些细节由Yakk's comments在另一个答案中提供
答案 0 :(得分:1)
我建议通过一个带有部分特化的辅助结构
template <typename L1, typename R1, template <typename, typename> class C,
typename TL, typename TR, bool = C<L1, R1>::value>
struct merge_branch_h
{ using type = Concat<TypeList<L1>,
typename Merge_s<TL, Concat<TypeList<R1>, TR>, C>::type>; };
template <typename L1, typename R1, template <typename, typename> class C,
typename TL, typename TR>
struct merge_branch_h<L1, R1, C, TL, TR, false>
{ using type = Concat<TypeList<R1>,
typename Merge_s<Concat<TypeList<L1>, TL>, TR, C>::type>; };
template <typename L1, typename R1, template <typename, typename> class C,
typename TL, typename TR>
using merge_branch = typename merge_branch_h<L1, R1, C, TL, TR>::type;
答案 1 :(得分:1)
添加一个额外的间接层。 Boost.MPL有一个名为eval_if
的元函数,它与conditional
类似,除了取两种类型之外,它需要两个无效的元函数并对其中一个进行求值。它非常容易实现:
template <bool B, typename T1, typename T2>
using eval_if = typename std::conditional<B, T1, T2>::type::type;
所以让我们添加一个元函数来进行连接/合并:
template <typename T>
struct identity {
using type = T;
};
template <typename L, typename R>
struct delay_concat {
using type = Concat<typename L::type, typename R::type>;
};
然后你可以交换你的:
typename std::conditional<C<L1, R1>::value,
Concat<TypeList<L1>, typename Merge_s<TL, Concat<TypeList<R1>, TR>, C>::type>,
Concat<TypeList<R1>, typename Merge_s<Concat<TypeList<L1>, TL>, TR, C>::type>
使用:
eval_if<C<L1, R1>::value,
delay_concat<identity<TypeList<L1>>, Merge_s<TL, Concat<TypeList<R1>, TR>, C>>,
delay_concat<identity<TypeList<R1>>, Merge_s<Concat<TypeList<L1>, TL>, TR, C>>>
短路。
哪个应该概括为:
template <template <typename...> class Z, typename... Ts>
struct delay_eval {
using type = Z<typename Ts::type...>;
};
然后让TypeList
成为一个自我产生的元函数,因此我们不必将它们包装在identity
中。这允许:
eval_if<C<L1, R1>::value,
delay_eval<Concat, TypeList<L1>, delay_eval<Merge_s, TL, delay_eval<Concat, TypeList<R1>, TR>, C>>,
delay_eval<Concat, TypeList<R1>, delay_eval<Merge_s, delay_eval<Concat, TypeList<L1>, TL>, TR, C>>>