在C ++ 11或C ++ 1y中排序非类型模板参数包? [英] Sorting non-type template parameter pack in C++11 or C++1y?

查看:103
本文介绍了在C ++ 11或C ++ 1y中排序非类型模板参数包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11和/或C ++ 1y中:

In C++11 and/or C++1y:

假设我得到一个带有非类型参数包的模板:

Suppose I am given a template with a non-type parameter pack:

template<int...>
void f();

我正在编写另一个模板来实例化它:

And I'm writing another template that will instantiate it:

template<int... x>
void g()
{
    ???

    f<???>();
}



我希望g按顺序实例化x。

I want g to instantiate f with x in sorted order.

这是:

g<4,7,2,9,3,7>();

应该调用:

f<2,3,4,7,7,9>();

可以这样做吗?

推荐答案

我想你可以使用Boost MPL的排序函数: http://www.boost.org/doc/libs /1_51_0/libs/mpl/doc/refmanual/sort.html

I suppose you can use Boost MPL's sort "function": http://www.boost.org/doc/libs/1_51_0/libs/mpl/doc/refmanual/sort.html

将值列表作为模板参数,加上一个谓词(默认为 less 作为习惯),它将按排序顺序产生一个副本。所要求的复杂度是平均的O(n log(n)),O(n ^ 2)最坏情况;使它类似于Quicksort(事实上,它似乎实际上使用Quicksort)。

Given a list of values as template parameters, plus a predicate (which defaults to less as is customary), it will produce a "copy" in sorted order. The claimed complexity is O(n log(n)) "on average", O(n^2) worst-case; making it similar to Quicksort (and in fact, it appears to actually use Quicksort).

你问这个函数的内部架构。关于这一点,我当然不知道,但鉴于Boost MPL的成熟度和我以前的经验使用它,我想说一试,如果它做你需要的,你可能会发现它和你一样令人满意找到任何其他C ++模板元程序设计。

You asked about this function's "internal architecture." About that, I surely have no idea, but given the maturity of Boost MPL and my prior experience using it, I'd say give it a try and if it does what you need, you'll probably find it about as satisfying as you find any other C++ template meta-programming.

这篇关于在C ++ 11或C ++ 1y中排序非类型模板参数包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆