每个具有自定义函数的编译时间 [英] Compiletime for each with custom functions

查看:51
本文介绍了每个具有自定义函数的编译时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下以下形式的问题:必须在函子列表上调用具有相同参数的多个特定成员函数。这是使用接口解决的一个好问题(runtime_interface,换句话说,这些函子必须实现的功能要求)。我想讨论的问题是函子列表在编译时已知的情况,但是在进一步的开发过程中可能会发生变化。因为在这种情况下,即使在编译时就知道所有要调用的函数,如果以这种方式实现,也要付出运行时开销。

Imagine a problem of the following form: One has to invoke multiple specific member functions with the same parameters on a list of functors. That makes a good problem to solve with an interface (runtime_interface, in other words a requirement of functions that those functors have to implement). The Problem I would like to discuss is the case where the list of functors is known at compile time, but might be subject to change during the further development process. Because in this case if implemented like that one is paying the runtime overhead even though all the functions to be called are known at compile time.

解决给定问题的方法有哪些,这些问题没有或只有很小的运行时开销。不放弃模块化结构。
我认为真正有趣的是,它只是

What are ways of solving Problems like the given one that come with no or just a small runtime overhead. without giving up the modularized structure. I think what is really intresting about this is that its just

template <class data_t, class... type_list_t>
struct compile_time_for_each_ref_impl;

template <class data_t, class first_t, class... type_list_t>
struct compile_time_for_each_ref_impl<data_t, first_t, type_list_t...> {
    static void eval(const data_t& data, first_t& object, type_list_t... object_list)
    {
        std::apply(object, data);

        compile_time_for_each_ref_impl<data_t, type_list_t...>::eval(data, object_list...);
    }
};
template <class data_t>
struct compile_time_for_each_ref_impl<data_t> {
    static void eval(const data_t& data) {}
};

template <class data_t, class... type_list_t>
void compile_time_for_each(const data_t& data, type_list_t&... objects)
{
    compile_time_for_each_ref_impl<data_t, type_list_t...>::eval(data, objects...);
}

template <class data_t, class... type_list_t>
void compile_time_for_each(const data_t& data, std::tuple<type_list_t...>& objects)
{
    std::apply(
        [&data] (type_list_t... params) {
            compile_time_for_each_ref_impl<data_t, type_list_t...>::eval(data, params...);
        },
        objects);
}



我能做什么:



What I am able to:


int data = 42

auto functor_1 = [] (int data) {std::cout << data;};
auto functor_2 = [] (int data) {data++; std::cout << data;};

compile_time_for_each(std::make_tuple(data), functor1, functor2);



我要编写的代码如下:



What the code i would like to write looks like::

struct functor1{
    void method1(int);

    int method2(double);
};


struct functor1{
    void method1(int);

    int method2(double);
};

template <class... functors_t>
struct main_mod{
    std::tuple<functors_t...> functors;

    void method1(int some_data){
        compile_time_for_each<method1, functors_t...>(some_data,functors);
    }

    void method2(int some_data){
        compile_time_for_each<method2, functors_t...>(some_data,functors);
    }
};



我的方法存在的问题:



我没有办法将应该在函子上调用的函数的名称传递给compile_time_for_each调用。我能做的就是更改硬编码的函数名称(示例实现采用了operator(),因为它使代码更简单,但可以对任何函数名称进行硬编码),因此对于每个要使用的函数名称,我都会得到一个compile_time_for_each函数我想使用。

The problem with my approach:

I dont see a way to pass the name of the function that is supposed to be called on the functor to the compile_time_for_each call. What i could do is to change the hardcoded function name (the example implementation takes the operator() because it makes the code simpler the code but one could hardcode any funtion name) so i would end up with one compile_time_for_each function for every function name that i would like to use.

有效的解决方案

最后,对我而言,这并不是真正的开销,但不是能够正确表达这些东西。

At the end for me it is not really about the overhead but not beeing able to express theese things properly.

它包含了@Aconcagua的解析器思想以及使用的fold表达式还建议使用@ max66。在这种状态下,我没有做任何优化,但是我喜欢Interface,这是我的主要目标。即使我认为它应该没有任何开销也是可行的。如果您看到此消息,并且有任何想法或建议使我震惊。

It incorporates @Aconcagua's idea of the resolver and the usage of fold expressions that @max66 suggested aswell. In this state I have not done any optimizations but I like the Interface and that was my main goal. Even though I think it should be doable without any overhead. If you are seeing this and have any ideas or suggestions hit me up.

https://godbolt.org/z/LfmSSb

推荐答案

使用我设法实现的Lambda即使我没有提供完全匹配的内容,也非常接近您的预期目标:

Using a lambda I managed to get pretty close to what you intend, even though I failed to provide an exact match:

template<typename Executor, typename Data, typename ... Functors>
void for_each(Executor executor, Data const& data, Functors ... functors)
{
    // C++17 fold expression:
    (executor(functors, data), ...);
}

class C0
{
public:
    void test0(int) const { std::cout << "00" << std::endl; }
    void test1(int) const { std::cout << "01" << std::endl; }
};
class C1
{
public:
    void test0(int) const { std::cout << "10" << std::endl; }
    void test1(int) const { std::cout << "11" << std::endl; }
};

int main()
{
    for_each([](auto const& c, int data) { c.test0(data); }, 7, C0(), C1());
    for_each([](auto const& c, int data) { c.test1(data); }, 7, C0(), C1());
    return 0;
}

这篇关于每个具有自定义函数的编译时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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