在模板类中提升模板相关结构的多索引容器

时间:2013-01-30 22:44:49

标签: c++ templates boost boost-multi-index

我想在类中使用多索引容器,这取决于类中依赖于模板的类。 听起来很复杂,这是代码:

#include <boost/unordered_map.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>

template <typename Type>
class myDataContainer{
public:
    struct DataStruct{
        double t;
        std::vector<Type> data;
    };

    // indices structs
    struct TagTime{};
    struct TagOrdered{};

    typedef boost::multi_index::multi_index_container<
    DataStruct,
    boost::multi_index::indexed_by<
        boost::multi_index::hashed_unique<boost::multi_index::tag<TagTime>,     boost::multi_index::member<DataStruct, double, &DataStruct::t> >,
        boost::multi_index::ordered_unique<boost::multi_index::tag<TagOrdered>,     boost::multi_index::member<DataStruct, double, &DataStruct::t> > // this index represents     timestamp incremental order
        >
    > InnerDataContainer;
    typedef typename boost::multi_index::index<InnerDataContainer,TagTime>::type timestamp_view;
    typedef typename boost::multi_index::index<InnerDataContainer,TagOrdered>::type ordered_view;

    InnerDataContainer dataContainer;
    void begin(){
        ordered_view& ordView = dataContainer.get<TagOrdered>();
        ordView.begin();
    }

};

int main(int argc, char *argv[])
{
    myDataContainer<float> data;
    myDataContainer<float>::ordered_view& ordView = data.dataContainer.get<TagOrder>();
    ordView.begin();
}

如果没有myDataContainer::begin()函数,此代码会编译,但使用myDataContainer::begin()我会收到以下错误:

main.cpp: In member function 'void myDataContainer<Type>::begin()':
main.cpp:134:66: error: expected primary-expression before '>' token
main.cpp:134:68: error: expected primary-expression before ')' token

我错过了什么吗?这是一个提升中的错误还是不可能?“

提前致谢 VEIO

1 个答案:

答案 0 :(得分:5)

因为dataContainer依赖于模板参数,所以需要

ordered_view& ordView = dataContainer.template get<TagOrdered>();

main()中,您使用特定的特化,因此不再有依赖表达式。