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

boost multi index container of template-dependent struct in template-class

本文关键字:索引 结构      更新时间:2023-10-16

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

#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

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

提前致谢维奥

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

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

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