c++ - 具有可变模板参数的函数指针

标签 c++ templates c++11 variadic

引用下面的代码,有人能想出如何适配吗

template <typename RET, typename... ARGS1, typename... ARGS2>
RET Mediator::change (Object* o, RET (Object::*f)(ARGS1...), ARGS2&&... args) {
    const std::tuple<ARGS2...> t(args...);
    for (Object* x : objects)
        (x->*f)(std::get<0>(t), o->rating, std::get<1>(t), o->str);
}

这样我就不必每次更改 ARGS2... 时都重写不同的版本。我不介意在参数仅包含 4 个参数的情况下这样做,但您可以想象,如果参数远大于 4,则需要泛化。ARGS1 中的类型...应包含不同的类型,所以应该有一种方法 get std::get<0>(t), std::get<1>(t), ... 正确放置,这样就不需要像上面那样手动操作(即使有是重复类型,那么它们可以简单地放在重复类型的第一个槽中)。下面是完整的代码(上下文是当一个 Mediator 的每个 Object 订阅者发生变化时,该 Mediator 的其他 Object 订阅者也应相应地改变):

#include <iostream>
#include <string>
#include <vector>
#include <tuple>

struct Mediator {
    std::vector<struct Object*> objects;

    void registerObject (Object* o) {objects.emplace_back(o);}

    template <typename RET, typename... ARGS1, typename... ARGS2>
    RET change (Object*, RET (Object::*)(ARGS1...), ARGS2&&...);
};

struct Object {
    int value;
    double rating;
    char letter;
    std::string str;
    Mediator& mediator;

    Object (int v, double r, char l, const std::string& s, Mediator& m) :
    value(v), rating(r), letter(l), str(s), mediator(m) {mediator.registerObject(this);}

    virtual void adjust (int, double, char, const std::string&) = 0;

    template <typename RET, typename... ARGS1, typename... ARGS2>
    RET change (RET (Object::*f)(ARGS1...), ARGS2&&... args) {
        return mediator.change(this, f, std::forward<ARGS2>(args)...);
    }
};

struct A : Object {
    using Object::Object;
    virtual void adjust (int a, double b, char c, const std::string& s) override {
        std::cout << "Type A adjusted using values " << a << ", " << b << ", " << c << ", and " << s << "." << std::endl;
    }
};

struct B : Object {
    using Object::Object;
    virtual void adjust (int a, double b, char c, const std::string& s) override {
        std::cout << "Type B adjusted using values " << a << ", " << b << ", " << c << ", and " << s << "." << std::endl;
    }
};

struct C : Object {
    using Object::Object;
    virtual void adjust (int a, double b, char c, const std::string& s) override {
        std::cout << "Type C adjusted using values " << a << ", " << b << ", " << c << ", and " << s << "." << std::endl;
    }
};

template <typename RET, typename... ARGS1, typename... ARGS2>
RET Mediator::change (Object* o, RET (Object::*f)(ARGS1...), ARGS2&&... args) {
    const std::tuple<ARGS2...> t(args...);
    for (Object* x : objects)
        (x->*f)(std::get<0>(t), o->rating, std::get<1>(t), o->str);
}

int main() {
    Mediator mediator;
    Object *a = new A(6, 1.2, 'a', "alan", mediator);
    Object *b = new B(2, 6.5, 'b', "bob", mediator);
    Object *c = new C(4, 0.8, 'c', "craig", mediator);

    c->change (&Object::adjust, 8, 'k');
}

输出:

Type A adjusted using values 8, 0.8, k, and craig.
Type B adjusted using values 8, 0.8, k, and craig.
Type C adjusted using values 8, 0.8, k, and craig.

这就是我的解决方案。它提供相同的输出,但标记为 //Here! 的行是我需要自动生成的。

#include <iostream>
#include <string>
#include <vector>
#include <tuple>

template <std::size_t...> struct index_sequence {};

template <std::size_t N, std::size_t... Is>
struct make_index_sequence_helper : make_index_sequence_helper<N-1, N-1, Is...> {};

template <std::size_t... Is>
struct make_index_sequence_helper<0, Is...> {
    using type = index_sequence<Is...>;
};

template <std::size_t N>
using make_index_sequence = typename make_index_sequence_helper<N>::type;

struct Mediator {
    std::vector<struct Object*> objects;

    void registerObject (Object* o) {objects.emplace_back(o);}

    template <typename RET, typename... ARGS1, typename... ARGS2>
    RET change (Object*, RET (Object::*)(ARGS1...), ARGS2&&...);

    template <typename RET, typename... ARGS, std::size_t... Is>
    RET changeHelper (RET (Object::*)(ARGS...), const std::tuple<ARGS...>&, index_sequence<Is...>);
};

struct Object {
    int value;
    double rating;
    char letter;
    std::string str;
    Mediator& mediator;

    Object (int v, double r, char l, const std::string& s, Mediator& m) :
    value(v), rating(r), letter(l), str(s), mediator(m) {mediator.registerObject(this);}

    virtual void adjust (int, double, char, const std::string&) = 0;

    template <typename RET, typename... ARGS1, typename... ARGS2>
    RET change (RET (Object::*f)(ARGS1...), ARGS2&&... args) {
        return mediator.change(this, f, std::forward<ARGS2>(args)...);
    }
};

struct A : Object {
    using Object::Object;
    virtual void adjust (int a, double b, char c, const std::string& s) override {
        std::cout << "Type A adjusted using values " << a << ", " << b << ", " << c << ", and " << s << "." << std::endl;
    }
};

struct B : Object {
    using Object::Object;
    virtual void adjust (int a, double b, char c, const std::string& s) override {
        std::cout << "Type B adjusted using values " << a << ", " << b << ", " << c << ", and " << s << "." << std::endl;
    }
};

struct C : Object {
    using Object::Object;
    virtual void adjust (int a, double b, char c, const std::string& s) override {
        std::cout << "Type C adjusted using values " << a << ", " << b << ", " << c << ", and " << s << "." << std::endl;
    }
};

template <typename RET, typename... ARGS1, typename... ARGS2>
RET Mediator::change (Object* o, RET (Object::*f)(ARGS1...), ARGS2&&... args) {
    const std::tuple<ARGS2...> t(args...);
      // Here!
    const std::tuple<ARGS1...> tuple(std::get<0>(t), o->rating, std::get<1>(t), o->str);
    changeHelper (f, tuple, make_index_sequence<sizeof...(ARGS1)>());
}

template <typename RET, typename... ARGS, std::size_t... Is>
RET Mediator::changeHelper (RET (Object::*f)(ARGS...),
        const std::tuple<ARGS...>& tuple, index_sequence<Is...>) {
    for (Object* x : objects)
        (x->*f) (std::get<Is>(tuple)...);   
}

int main() {
    Mediator mediator;
    Object *a = new A(6, 1.2, 'a', "alan", mediator);
    Object *b = new B(2, 6.5, 'b', "bob", mediator);
    Object *c = new C(4, 0.8, 'c', "craig", mediator);

    c->change (&Object::adjust, 8, 'k');
}

如何自动生成元组

const std::tuple<ARGS1...> tuple(std::get<0>(t), o->rating, std::get<1>(t), o->str);

使用类似的东西

template <typename... ARGS1, typename... ARGS2>
std::tuple<ARGS1...> extractTuple (Object* o, ARGS2&&... args);

这样新版本的 Mediator::change 将不需要不同的(可能很多,如果 ARGS1... 很大)ARGS2... 的选择?我目前的想法是使用递归辅助方法,std::is_same、std::tuple_cat 等...但我遇到了问题(我认为我们正在解包 ARGS2...在解包 ARGS1...检查期间类型)。

最佳答案

首先,我们需要一个标签和一系列函数来根据对象的类型从对象中获取值。很简单。

template<class T> struct typetag {};

const int& get_type_from_class(const Object* o, typetag<int>) {return o->value;}
const double& get_type_from_class(const Object* o, typetag<double>) {return o->rating;}
const char& get_type_from_class(const Object* o, typetag<char>) {return o->letter;}
const long& get_type_from_class(const Object* o, typetag<long>) {return o->tag;}

下一部分是我们需要根据类型从参数列表中获取类型,第一个参数是如果没有参数匹配则默认返回。也不是非常困难。有递归不匹配情况、递归匹配情况和最后匹配情况。虽然这似乎有相当多的递归,但即使是最简单的优化器也应该能够将其内联到最佳装配。由于我不明白的原因,这些必须按照这个确切的顺序。

template<class T> 
const T& get_T_by_type(const T& def) 
{return def;}

template<class T, class...pRest> 
const T& get_T_by_type(const T& def, const T& returnme, const pRest&...rest) 
{return returnme;}

template<class T, class p0, class...pRest> 
const T& get_T_by_type(const T& def, const p0& discard, const pRest&...rest) 
{return get_T_by_type(def, rest...);}

最后,我们调用函数。对于每个 ARGS1,我们调用 get_T_by_type 来获取相同类型的 ARGS2,默认情况下我们使用 get_type_from_class 传递类中的现有值。

template <typename RET, typename... ARGS1, typename... ARGS2>
void Mediator::change (Object* o, RET (Object::*f)(ARGS1...), const ARGS2&... args) {
    for (Object* x : objects) {
        (x->*f)(
            get_T_by_type(get_type_from_class(o, typetag<ARGS1>{}),args...) //pass all args2
            ... //pass one of that for each args1
            );
    }
}

请注意,我已将返回类型更改为 void,因为您要委托(delegate)调用多个函数。或者,您可以返回返回结果的 vector

http://coliru.stacked-crooked.com/a/36afa072711b0655

关于c++ - 具有可变模板参数的函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27489808/

相关文章:

c++ - has_equal_operator 在 C++11 中的实现

c++ - 如何加速返回指向 C++ 对象指针的函数?

php - OpenSSL C++ 加密问题

c++编译错误在调试但不是在发布

C++ 模板和继承

c++ - 令人费解的非尾随参数包行为

c++ - 模板元编程 :why flat type is failure

c++ - 在 C++ 中搜索 vector 的一部分

c++ - 理解 C/C++ 中无括号的 for/if 组合

c++ - 具有 VirtualProtect 的 PAGE_GUARD 在执行访问时不会引发异常