通过改变推导出的模板值来自组成函数
Self-composing a function with changing deduced template value
如何执行self_composeN(func)(x)如果func有一个导出的模板值,需要每次更改?举个例子吧?这里我写了rotate_tuple它将给定元组的所有元素向左旋转N,我想多次应用它(我知道我可以用N乘以任何东西来实现这一点,但我想知道它的通用解,因为会有其他这样的函数没有这样的捷径)。下面是rotate_tuple的代码,它已经经过了彻底的测试(它也提取了结果类型)。你不需要担心细节,我不认为,除了struct rotate_tuple本身,它很短。
#include <tuple>
#include <string>
#include <functional>
template <int NUM_LEFT, int SHIFT>
class rotate_tuple_helper {
private:
static constexpr int positiveModulo (int i, int n) {return (i % n + n) % n;}
template <typename TUPLE>
static constexpr int shift() {return positiveModulo (SHIFT, std::tuple_size<TUPLE>::value);}
public:
template <typename TUPLE>
auto operator() (const TUPLE& t) const -> decltype (std::tuple_cat (std::make_tuple (std::get<shift<TUPLE>()>(t)), rotate_tuple_helper<NUM_LEFT-1, SHIFT+1>()(t))) {
return std::tuple_cat (std::make_tuple (std::get<shift<TUPLE>()>(t)), rotate_tuple_helper<NUM_LEFT-1, SHIFT+1>()(t));
}
};
template <int SHIFT>
class rotate_tuple_helper<0, SHIFT> {
public:
template <typename TUPLE>
std::tuple<> operator() (const TUPLE& t) const {return std::tuple<>();}
};
template <typename TUPLE, int SHIFT>
struct rotate_tuple_type {
using type = decltype (rotate_tuple_helper<std::tuple_size<TUPLE>::value, SHIFT>()(std::declval<TUPLE>()));
};
template <int SHIFT>
struct rotate_tuple {
template <typename TUPLE>
typename rotate_tuple_type<TUPLE, SHIFT>::type operator() (const TUPLE& t) const {
return rotate_tuple_helper<std::tuple_size<TUPLE>::value, SHIFT>()(t);
}
};
int main() {
using tuple_type = std::tuple<int, std::string, double, char, std::string, int, double, char>;
const tuple_type t (8, "house", 3.14, 'b', "apple", 6, 1.5, '!');
using rotate_tuple_type = rotate_tuple_type<tuple_type, 3>::type;
const rotate_tuple_type result = rotate_tuple<3>()(t); //(b, apple, 6, 1.5, !, 8, house, 3.14)
}
无论如何,我想做一些事情来达到self_compose<10> (rotate_tuple<3> ()) (t);(不用直接把3改成30)。有人能帮忙吗?c++/c++ 11中函数组合的例子在这里不太适用,因为它实际上不是被重用的同一个函数。rotate_tuple<3>()的导出模板值T不断变化(因为生成的元组类型不断变化),因此我们实际上没有自我组合。我想self_compose不是个好名字,应该是self_repeat。无论如何,关于如何编写self_repeat(不特定于rotate_tuple的通用解决方案)的任何想法?
您可以使用以下命令:
template <std::size_t N, typename Functor> struct self_composeN
{
template <typename T>
constexpr auto operator () (T t) const -> Return((self_composeN<N - 1, Functor>()(Functor()(t))))
};
template <typename Functor> struct self_composeN<0u, Functor>
{
template <typename T>
constexpr T operator () (T t) const { return t; }
};
生活例子。
相关文章:
- 变量没有改变?通过向量的函数调用
- 为什么提升图库的 read_graphviz() 函数会改变节点的索引
- 为什么我的函数接受"std::string"进行排序不会改变它?
- 通过从构造函数中的'this'复制的指针改变常量对象
- 为什么我可以改变常量对象中的成员变量,这是返回常量对象函数的结果?
- 有没有办法创建一个不改变程序中参数的函数
- 为什么我的函数不改变对象的属性
- 类中的枚举在调用自己的函数时不会改变
- C - 如何将各处的静态函数称为改变其私有变量
- 基于继承的类型参数改变父虚拟函数的行为
- 对列表中的每个对象调用成员函数,以改变对象所在的列表
- 为什么调用 CUDA 内核函数时这个类成员变量没有改变
- 当函数应该改变对象的状态时,是选择成员函数还是朋友函数
- 一个什么都不做的函数可以改变一个对象吗?
- ifstream 函数"get"如何改变其字符参数?
- 为什么一个函数的末尾有常量这个词,即使它确实改变了一个变量
- visual C++未执行的代码会改变函数行为
- 不被类函数改变的私有int值
- 谷歌模拟函数改变参数值
- 被非const成员函数改变的可变变量