繁体   English   中英

没有虚拟或继承的多态向量

[英]polymorphic vector without virtual or inheritance

我正在尝试实现一个向量,该向量可以采用多种类型的元素,并且可以对所有元素应用函数。 使用基类,虚函数和继承很容易做到这一点,但是我明确地不想使用它。 这是我到目前为止的距离:

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

// this will be my new polymorphic vector;
template<typename... Ts>
class myvector {
    std::tuple<std::vector<Ts>...> vectors;

    template <template<typename> class funtype>
    void for_each() {
    }

    template <template<typename> class funtype, typename X, typename... Xs>
    void for_each() {
        std::vector<X>& vector = std::get<std::vector<X>>(vectors);
        for ( X& x : vector ) {
            funtype<X> fun;
            fun(x);
        }
        for_each<funtype, Xs...>();
    }

public:
    template <typename T>
    void push_back(const T& t) {
        std::vector<T>& vector = std::get<std::vector<T>>(vectors);
        vector.push_back(t);
    }

    template <typename T>
    void pop_back() {
        std::vector<T>& vector = std::get<std::vector<T>>(vectors);
        vector.pop_back();
    }

    /* here I would like to pass a function, or function object that 
     * can be expanded to all underlying types. I would prefer to just 
     * give a function name, that has an implementation to all types in Ts
     */
    template <template<typename> class funtype>
    void ForEach() {
        for_each<funtype,Ts...>();
    }
};


struct foo {
};

struct bar {
};

template <typename T>
void method(T& t);

template<>
void method(foo& b) {
    std::cout << "foo" << std::endl;
}

template<>
void method(bar& b) {
    std::cout << "bar" << std::endl;
}

int main()
{
    myvector<foo,bar> mv;
    mv.push_back( foo{} );
    mv.push_back( bar{} );
    mv.ForEach<method>();
}

目前,我感到有些困惑,希望您能给我一些进一步建议。

常见的解决方案是将函数对象与一组operator()

struct my_fun_type
{
    void operator()(foo&) const
    { std::cout << "foo\n"; }

    void operator()(bar&) const
    { std::cout << "bar\n"; }
};

这允许将“重载函数”的“集合”传递给算法,状态,并且使用起来相当方便:

my_algorithm(my_fun_type{});

如果要添加对此类功能对象的支持,可以按以下方式定义ForEach

template <typename Elem, typename Fun>
void for_each(Fun&& fun) {
    std::vector<Elem>& vector = std::get<std::vector<Elem>>(vectors);
    for ( Elem& e : vector ) {
        fun(x);
    }
}

template <typename Fun>
void ForEach(Fun&& fun) {
    int dummy[] = { 0, (for_each<Ts>(fun), 0)... };
    (void)dummy;
}

这个dummy是为Ts所有类型调用for_each的技巧。 (void)dummy用于禁止编译器警告(永远不会读取dummy )。

您可以在其他问答中进一步了解此技术,例如问答。

Fun&&不是右值引用,而是通用引用


请注意,上面的示例与许多标准库算法不同,后者按值获取函数对象:

template <typename Elem, typename Fun>
void for_each(Fun fun) {
    std::vector<Elem>& vector = std::get<std::vector<Elem>>(vectors);
    std::for_each(vector.begin(), vector.end(), std::move(fun));
}

template <typename Fun>
void ForEach(Fun fun) {
    int dummy[] = { 0, (for_each<Ts>(fun), 0)... };
    (void)dummy;
}

要传递一组重载的自由函数,我们可以将它们包装在一个函数对象中(感谢@Yakk的建议):

struct method_t
{
    template<class... Ts>
    void operator()(Ts&&... ts) const
    { method( std::forward<Ts>(ts)... ); }
};

在C ++ 1y中,使用多态lambda可以用更少的样板创建这样的函数对象类型:

[](auto&&... pp)
{ method( std::forward<decltype(pp)>(pp)... ); }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM