Boost Multi_index容器的显式即时

Explicit instantion of boost multi_index container

本文关键字:Multi index Boost      更新时间:2023-10-16

首先,我想显示工作代码,然后说明我要如何更改事物。这是简单的boost multi_index示例:

//main.cpp    
    #include <boost/multi_index_container.hpp>
    #include <boost/multi_index/ordered_index.hpp>
    #include <boost/multi_index/identity.hpp>
    #include <boost/multi_index/member.hpp>
    #include <string>
    struct employee
    {
        int         id;
        std::string name;
        employee(int id, const std::string& name) :id(id), name(name){}
        bool operator<(const employee& e)const{ return id<e.id; }
    };
    typedef boost::multi_index::multi_index_container<
        employee,
        boost::multi_index:: indexed_by<
        // sort by employee::operator<
        boost::multi_index:: ordered_unique< boost::multi_index:: identity<employee> >,
        // sort by less<string> on name
        boost::multi_index::ordered_non_unique<boost::multi_index::member<employee, std::string, &employee::name> >
        >
    > employee_set;
    int main()
    {
        employee_set es;
        es.insert(employee(0, "Bob"));
    }

想象一下main.cpp是另一个模块,而无需增强依赖。我想如何:

包括一些标头文件,其中带有Boost MultiIndex Container类被转发为Main.CPP在其他.cpp文件中定义员工的多索引容器我尝试了大量的变体,但是如果这可以使用,则没有。可以创建这样的东西吗?

//notmain.cpp
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>
#include "notmain.h"
typedef boost::multi_index::multi_index_container<
    employee,
    boost::multi_index::indexed_by<
    // sort by employee::operator<
    boost::multi_index::ordered_unique< boost::multi_index::identity<employee> >,
    // sort by less<string> on name
    boost::multi_index::ordered_non_unique<boost::multi_index::member<employee, std::string, &employee::name> >
    >
> employee_set;

现在来了,我需要填充容器的正向声明(或明确启动)。我可能会误解这些术语,但我是C 的新手,并提升了。

//notmain.h
#include <string>
/*
    Some how here I need forward declaration or explicit initiation of boost container 
    class employee_set ???
*/
struct employee
{
    int         id;
    std::string name;
    employee(int id, const std::string& name) :id(id), name(name){}
    bool operator<(const employee& e)const{ return id<e.id; }
};

这是最终目标。我想提醒main.cpp被认为是另一个模块的.cpp,而无需提高依赖。

//main.cpp
#include "notmain.h"
int main()
{
    employee_set es;
    es.insert(employee(0, "Bob"));
}

如果类型是类'可见界面的一部分,则必须包括依赖类的任何标题,因此无能为力。如果您真的不希望它成为可见界面的一部分,请考虑使用pimpl idiom:

公共标头

#if !defined(MYCLASS_PUBLIC_H_)
#define MYCLASS_PUBLIC_H_
struct MyClassImpl;
class MyClass {
  MyClassImpl * pImpl;
public:
  void SomeOperation();
};
#endif

实施标头:

#if !defined(MYCLASS_IMPL_H_)
#define MYCLASS_IMPL_H_
#include <private_type.h>
#include "MyClass.h"
struct MyClassImpl
{
  void Operation();
private:
  SomePrivateType member;
};
#endif

实现文件:

#include "MyClassImpl.h"
void MyClass::SomeOperation()
{
  pImpl->Operation();
}
void MyClassImpl::Operation()
{
  // do something with 'member'
}

仅看到公共接口的代码:

#include "MyClass.h"
void foo()
{
  MyClass inst;
  inst.SomeOperation();
}