简体   繁体   English

显式提升boost multi_index容器

[英]Explicit instantion of boost multi_index container

First I would like to show the working code and then explain, how i want to change things. 首先,我想展示工作代码,然后解释,我想如何改变一切。 This is simple boost multi_index example: 这是简单的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"));
    }

Imagine if main.cpp is another module, without boost dependency. 想象一下,如果main.cpp是另一个模块,没有boost依赖。 I want to udnerstand how to: 我想知道如何:

include some header file with boost multiindex container class being forward declared into main.cpp define multiindex container of employees in additional .cpp file I have tried tons of variants, but none if this works. 包含一些头文件,其中boost multiindex容器类被转发声明为main.cpp定义员工的多指数容器,在另外的.cpp文件中我尝试过很多变种,但是如果这样可行则没有。 Is it possible to create something like this? 有可能创造这样的东西吗?

//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;

Now comes h.file I need to fill with forward declaration (or explicit initiation) of container. 现在来了h.file我需要填充容器的前向声明(或显式启动)。 I may be misunderstanding these terms, but I am new to c++ and boost. 我可能误解了这些术语,但我不熟悉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; }
};

This is final goal. 这是最终目标。 I want to remind that main.cpp is imagined to be .cpp of another module, without boost dependency. 我想提醒一下,main.cpp被认为是另一个模块的.cpp,没有boost依赖。

//main.cpp
#include "notmain.h"

int main()
{
    employee_set es;
    es.insert(employee(0, "Bob"));
}

If the type is part of a class' visible interface then any headers that class is dependent on have to be included, no way around that. 如果类型是类的可见接口的一部分,那么必须包含类所依赖的任何头,而不能解决这个问题。 If you really don't want it to be part of the visible interface consider using the pImpl idiom: 如果您真的不希望它成为可见界面的一部分,请考虑使用pImpl习语:

Public header 公共标题

#if !defined(MYCLASS_PUBLIC_H_)
#define MYCLASS_PUBLIC_H_

struct MyClassImpl;
class MyClass {
  MyClassImpl * pImpl;
public:
  void SomeOperation();
};
#endif

Implementation header: 实施标题:

#if !defined(MYCLASS_IMPL_H_)
#define MYCLASS_IMPL_H_
#include <private_type.h>
#include "MyClass.h"
struct MyClassImpl
{
  void Operation();

private:
  SomePrivateType member;
};
#endif

Implementation file: 实施文件:

#include "MyClassImpl.h"
void MyClass::SomeOperation()
{
  pImpl->Operation();
}

void MyClassImpl::Operation()
{
  // do something with 'member'
}

Code that only sees the public interface: 只看到公共接口的代码:

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

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

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