【问题标题】:Composing a set of container classes and accessing them from a base组合一组容器类并从基础访问它们
【发布时间】:2011-03-17 02:27:04
【问题描述】:

我正在尝试使用boost::mpl::inherit_linearly 使用用户提供的类型组成一个容器类:

#include <typeinfo>
#include <iostream>
#include <vector>

#include <boost/mpl/inherit.hpp>
#include <boost/mpl/inherit_linearly.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/vector.hpp>

namespace mpl = ::boost::mpl;

//////////////////////////////////////////////
// Create the container by chaining vectors
//////////////////////////////////////////////

struct Base {};

// Types provided by the user
typedef mpl::vector<int, char, double>::type myTypes;

typedef mpl::inherit_linearly<
    myTypes, 
    mpl::inherit<mpl::_1, std::vector<mpl::_2> >,
    Base
    >::type InheritedContainer;


// Function for accessing containers
template <typename T>
inline std::vector<T>& get_container(Base& c) {
    return static_cast<std::vector<T>& >(c);
}


// Some functions that manipulate the containers
//     NB: These functions only know about the Base and the types
//         they want to access

void my_int_func(Base& b) {
    get_container<int>(b).push_back(42);
}

void my_char_func(Base& b) {
    get_container<char>(b).push_back('c');
}

int main() {
    InheritedContainer container;
    Base& bref = container;

    my_int_func(bref);
    std::cout << "Int: " << get_container<int>(bref).back() << std::endl;

    my_char_func(bref);
    std::cout << "Char: " << get_container<char>(bref).back() << std::endl;

    return 0;
}

我得到的编译错误是:

question.cpp: In function ‘std::vector<T, std::allocator<_CharT> >& get_container(Base&) [with T = int]’:
question.cpp:40:   instantiated from here
question.cpp:31: error: invalid static_cast from type ‘Base’ to type ‘std::vector<int, std::allocator<int> >&’
question.cpp: In function ‘std::vector<T, std::allocator<_CharT> >& get_container(Base&) [with T = char]’:
question.cpp:44:   instantiated from here
question.cpp:31: error: invalid static_cast from type ‘Base’ to type ‘std::vector<char, std::allocator<char> >&’

Base 不应该是inherit_linearly 生成的任何类型的基础吗?如果是这样,vector&lt;int&gt; 和其他向量不应该出现在类型层次结构中以供 static_cast 拉出吗?

有没有其他方法可以获得这个功能?

【问题讨论】:

    标签: c++ boost-mpl


    【解决方案1】:

    我认为BaseInheritedContainer 的基类,但不是 std::vector&lt;int&gt;。 我们知道,std::vector 没有被定义为:

    class vector : Base {...
    

    您可能期望以下继承:

    class InheritedContainer : Base, std::vector<int>, ... {...
    

    但是,在这种情况下,从 Basevector&lt;int&gt; 的演员表是交叉演员表, 所以这不能用static_cast来完成。

    如您所知,以下是允许的:

    InheritedContainer container;
    Base& bref = container;
    InheritedContainer& iref = static_cast<InheritedContainer&>(bref);
    std::vector<int>& vi = iref;
    std::vector<char>& vc = iref;
    

    如果你能准备get_containermy_int_funcmy_char_func,大概的类型 std::vector 将专门用于哪个是事先知道的。 如果是这样,我认为持有InheritedContainer&amp; 而不是Base&amp; 是相关的 一直以来。
    如果您必须将Base 转换为vector&lt;T&gt;,可能 RTTI(例如向Base添加虚拟功能)和dynamic_cast将启用 演员表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多