带有可变参数模板参数的函数指针

时间:2014-12-15 17:38:15

标签: 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中的类型......应该由不同的类型组成,所以应该有一种方法获取std :: get&lt; 0&gt;(t),std :: get&lt; 1&gt;(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);

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

3 个答案:

答案 0 :(得分:2)

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

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,因为您已委托调用多个函数。或者,您可以返回vector返回结果。

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

答案 1 :(得分:0)

好的,我有一份初稿。它仍然需要通过解压缩ARGS1来进一步推广......而不是我在下面的方式。但至少,第一个解决方案表明问题可以完全解决。

#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;
    long tag;
    Mediator& mediator;

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

    virtual void adjust (int, double, char, long) = 0;
    virtual void transform (char, double, int) = 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, long s) override {
        std::cout << "Type A adjusted using values " << a << ", " << b << ", " << c << ", and " << s << "." << std::endl;
    }
    virtual void transform (char a, double b, int c) override {
        std::cout << "Type A transformed using values " << a << ", " << b << ", and " << c << "." << std::endl;
    }
};

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

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

template <typename T, typename TUPLE> struct Concatenate;

template <typename FIRST, typename ...REST>
struct Concatenate<FIRST, std::tuple<REST...>> {
    using type = typename std::tuple<FIRST, REST...>;
};

template <typename HEAD, typename TUPLE>
typename Concatenate<HEAD, TUPLE>::type nextTuple (Object* o, const TUPLE& tuple) {
    if (std::is_same<HEAD, int>::value)
        return std::tuple_cat (tuple, std::tuple<int>(o->value));
    else if (std::is_same<HEAD, double>::value)
        return std::tuple_cat (tuple, std::tuple<double>(o->rating));
    else if (std::is_same<HEAD, char>::value)
        return std::tuple_cat (tuple, std::tuple<char>(o->letter));
    else if (std::is_same<HEAD, long>::value)
        return std::tuple_cat (tuple, std::tuple<long>(o->tag));
}

template <typename HEAD, typename TUPLE, typename FIRST, typename... REST>
typename Concatenate<HEAD, TUPLE>::type nextTuple (Object* o, const TUPLE& tuple, FIRST first, REST... rest) {
    if (std::is_same<HEAD, FIRST>::value)
        return std::tuple_cat (tuple, std::tuple<FIRST>(first));
    return nextTuple<HEAD, TUPLE, REST...> (o, tuple, rest...);
}

template <typename RET, typename... ARGS1, typename... ARGS2>
std::tuple<ARGS1...> extractTuple (Object* o, RET (Object::*)(ARGS1...), ARGS2&&... args) {
// Function pointer parameter needed to maintain ARGS1..., else it will become an empty pack.
    std::tuple<> t0;
    const auto t1 = nextTuple<int> (o, t0, args...);
        // In general, unpack ARGS1...
    const auto t2 = nextTuple<double> (o, t1, args...);
    const auto t3 = nextTuple<char> (o, t2, args...);
    return nextTuple<long> (o, t3, args...);
}

template <typename RET, typename... ARGS1, typename... ARGS2>
RET Mediator::change (Object* o, RET (Object::*f)(ARGS1...), ARGS2&&... args) {
    const std::tuple<ARGS1...> tuple = extractTuple (o, f, args...);  // The key function.
    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', 1111, mediator);
    Object *b = new B(2, 6.5, 'b', 2222, mediator);
    Object *c = new C(4, 0.8, 'c', 3333, mediator);

    c->change (&Object::adjust, 8, 'k');
//  c->change (&Object::transform, 'z', 4);  // This does not work though.
}

输出:

Type A adjusted using values 8, 0, k, and 3333.
Type B adjusted using values 8, 0, k, and 3333.
Type C adjusted using values 8, 0, k, and 3333.

出于某种原因,std :: string存在问题,所以我用long替换了第4个参数。所以这个解决方案仍然需要改进。我还引入了一个新的Object成员函数transform,以显示main()中的行c->change (&Object::transform, 'z', 4);不起作用(也不适用于Mooing Duck的解决方案),以及因此,这个解决方案需要更多的概括,通过ARGS1的某种递归......可能是完整的。我上面的解决方案只处理特定的ARGS1 ...格式(int,double,char,long)。

我怀疑Mooing Duck的解决方案也可以推广到处理传递给Mediator :: change的Object的任何新成员函数。

更新:好的,我已用

替换extractTuple
template <typename TUPLE, typename... ARGS2>
TUPLE extractTuple (Object*, const TUPLE& tuple, ARGS2&&...) {return tuple;}

template <typename TUPLE, typename FIRST, typename... REST, typename... ARGS2>
auto extractTuple (Object* o, const TUPLE& current, ARGS2&&... args)
-> decltype (extractTuple<typename Concatenate<FIRST, TUPLE>::type, REST...>
(o, nextTuple<FIRST> (o, current, args...), args...)) {
    const typename Concatenate<FIRST, TUPLE>::type next = nextTuple<FIRST> (o, current, args...);
    return extractTuple<typename Concatenate<FIRST, TUPLE>::type, REST...> (o, next, args...);
}

我认为这通过解压缩ARGS1来概括上述解决方案...这是我现在的新代码:

#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;
    long tag;
    Mediator& mediator;

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

    virtual void adjust (int, double, char, long) = 0;
    virtual void transform (char, double, int) = 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, long s) override {
        std::cout << "Type A adjusted using values " << a << ", " << b << ", " << c << ", and " << s << "." << std::endl;
    }
    virtual void transform (char a, double b, int c) override {
        std::cout << "Type A transformed using values " << a << ", " << b << ", and " << c << "." << std::endl;
    }
};

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

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

template <typename T, typename TUPLE> struct Concatenate;

template <typename FIRST, typename ...REST>
struct Concatenate<FIRST, std::tuple<REST...>> {
    using type = typename std::tuple<FIRST, REST...>;
};

template <typename HEAD, typename TUPLE>
typename Concatenate<HEAD, TUPLE>::type nextTuple (Object* o, const TUPLE& tuple) {
    if (std::is_same<HEAD, int>::value)  // Using overload from some new class can probably handle all these cases better.
        return std::tuple_cat (tuple, std::tuple<int>(o->value));
    else if (std::is_same<HEAD, double>::value)
        return std::tuple_cat (tuple, std::tuple<double>(o->rating));
    else if (std::is_same<HEAD, char>::value)
        return std::tuple_cat (tuple, std::tuple<char>(o->letter));
    else if (std::is_same<HEAD, long>::value)
        return std::tuple_cat (tuple, std::tuple<long>(o->tag));
}

template <typename HEAD, typename TUPLE, typename FIRST, typename... REST>
typename Concatenate<HEAD, TUPLE>::type nextTuple (Object* o, const TUPLE& tuple, FIRST first, REST... rest) {
    if (std::is_same<HEAD, FIRST>::value)
        return std::tuple_cat (tuple, std::tuple<FIRST>(first));
    return nextTuple<HEAD, TUPLE, REST...> (o, tuple, rest...);
}

template <typename TUPLE, typename... ARGS2>
TUPLE extractTuple (Object*, const TUPLE& tuple, ARGS2&&...) {return tuple;}

// *** The change
template <typename TUPLE, typename FIRST, typename... REST, typename... ARGS2>
auto extractTuple (Object* o, const TUPLE& current, ARGS2&&... args)
-> decltype (extractTuple<typename Concatenate<FIRST, TUPLE>::type, REST...>
(o, nextTuple<FIRST> (o, current, args...), args...)) {
    const typename Concatenate<FIRST, TUPLE>::type next = nextTuple<FIRST> (o, current, args...);
    return extractTuple<typename Concatenate<FIRST, TUPLE>::type, REST...> (o, next, args...);
}

template <typename RET, typename... ARGS1, typename... ARGS2>
RET Mediator::change (Object* o, RET (Object::*f)(ARGS1...), ARGS2&&... args) {
    const std::tuple<ARGS1...> tuple = extractTuple<std::tuple<>, ARGS1...> (o, std::tuple<>(), args...);  // The key function.
    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', 1111, mediator);
    Object *b = new B(2, 6.5, 'b', 2222, mediator);
    Object *c = new C(4, 0.8, 'c', 3333, mediator);

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

请注意,c->change (&Object::transform, 'z', 4);现在位于main()中。但我的GCC 4.8.1出现了错误,无法处理声明

template <typename TUPLE, typename FIRST, typename... REST, typename... ARGS2>
auto extractTuple (Object* o, const TUPLE& current, ARGS2&&... args)
-> decltype (extractTuple<typename Concatenate<FIRST, TUPLE>::type, REST...>
(o, nextTuple<FIRST> (o, current, args...), args...)) {

导致内部编译错误。&#34;也许拥有最近使用C ++ 14编译器的人可以检查这是否是合法声明?我没有C ++ 14。

答案 2 :(得分:0)

这个答案是基于Mooing Duck的原始答案,该答案仅处理了一个特例。我添加了大约两倍的代码量来使用元组来概括他的答案。他上面的新通用解决方案明显优越。

#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;
    long tag;
    Mediator& mediator;

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

    virtual void adjust (int, double, char, long) = 0;
    virtual void transform (char, double, int) = 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, long s) override {
        std::cout << "Type A adjusted using values " << a << ", " << b << ", " << c << ", and " << s << "." << std::endl;
    }
    virtual void transform (char a, double b, int c) override {
        std::cout << "Type A transformed using values " << a << ", " << b << ", and " << c << "." << std::endl;
    }
};

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

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

template <typename T, typename TUPLE> struct Concatenate;

template <typename FIRST, typename ...REST>
struct Concatenate<FIRST, std::tuple<REST...>> {
    using type = typename std::tuple<REST..., FIRST>;  // This time, we must concatenate at the back else 'extractTuple<std::tuple<>, ARGS1...>(t, std::tuple<>())' below will be in reverse!
};

struct NamedObjectChangeParameters {
    int value;
    double rating;
    char letter;
    long tag;

    explicit NamedObjectChangeParameters (const Object& o) : value(o.value), rating(o.rating), letter(o.letter), tag(o.tag) {}

    // change overloads for Mediator::change.
    void change (int a) {value = a;}
    void change (double b) {rating = b;}
    void change (char c) {letter = c;}
    void change (long d) {tag = d;}

    template <typename FIRST, typename... REST>
    void change (const FIRST& first, const REST&... rest) {
        change (first);
        change (rest...);
    }

    // nextTuple overloads for the important extractTuple function.
    template <typename TUPLE> typename Concatenate<int, TUPLE>::type
    nextTuple (int, const TUPLE& tuple) {return std::tuple_cat (tuple, std::tuple<int>(value));}
    template <typename TUPLE> typename Concatenate<double, TUPLE>::type
    nextTuple (double, const TUPLE& tuple) {return std::tuple_cat (tuple, std::tuple<int>(rating));}
    template <typename TUPLE> typename Concatenate<char, TUPLE>::type   
    nextTuple (char, const TUPLE& tuple) {return std::tuple_cat (tuple, std::tuple<int>(letter));}
    template <typename TUPLE> typename Concatenate<long, TUPLE>::type   
    nextTuple (long, const TUPLE& tuple) {return std::tuple_cat (tuple, std::tuple<int>(tag));}
};

template <typename TUPLE>
TUPLE extractTuple (NamedObjectChangeParameters&, const TUPLE& tuple) {return tuple;}

template <typename TUPLE, typename FIRST, typename... REST>
auto extractTuple (NamedObjectChangeParameters& t, const TUPLE& current)
-> decltype (extractTuple<typename Concatenate<FIRST, TUPLE>::type, REST...> (t, t.nextTuple<TUPLE> (FIRST(), current))) {
    const typename Concatenate<FIRST, TUPLE>::type next = t.nextTuple<TUPLE> (FIRST(), current);  // nextTuple<TUPLE> is an overloaded function of NamedObjectChangeParameters that creates the correct type based on what type FIRST is.
    return extractTuple<typename Concatenate<FIRST, TUPLE>::type, REST...> (t, next);
}

template <typename RET, typename... ARGS1, typename... ARGS2>
RET Mediator::change (Object* o, RET (Object::*f)(ARGS1...), ARGS2&&... args) {
    NamedObjectChangeParameters t(*o);
    t.change(args...);
    changeHelper (f, extractTuple<std::tuple<>, ARGS1...>(t, std::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', 1111, mediator);
    Object *b = new B(2, 6.5, 'b', 2222, mediator);
    Object *c = new C(4, 0.8, 'c', 3333, mediator);

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

输出:

Type A adjusted using values 8, 0, k, and 3333.
Type B adjusted using values 8, 0, k, and 3333.
Type C adjusted using values 8, 0, k, and 3333.
Type A transformed using values z, 0, and 4.
Type B transformed using values z, 0, and 4.
Type C transformed using values z, 0, and 4.
相关问题