gpt4 book ai didi

c++ - boost multi_index 容器的显式实例化

转载 作者:行者123 更新时间:2023-11-30 02:21:47 27 4
gpt4 key购买 nike

首先我想展示工作代码,然后解释我想如何改变。这是简单的 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 依赖。我想了解如何:

包括一些头文件和 boost multiindex 容器类被转发声明到 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;

现在是 h.file 我需要填写容器的前向声明(或显式启动)。我可能误解了这些术语,但我是 c++ 和 boost 的新手。

//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,没有 boost 依赖。

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

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

最佳答案

如果该类型是类的可见接口(interface)的一部分,则必须包含该类所依赖的任何 header ,这是没有办法的。如果您真的不希望它成为可见界面的一部分,请考虑使用 pImpl 习惯用法:

公共(public) header

#if !defined(MYCLASS_PUBLIC_H_)
#define MYCLASS_PUBLIC_H_

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

实现 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

执行文件:

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

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

只看到公共(public)接口(interface)的代码:

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

关于c++ - boost multi_index 容器的显式实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47997242/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com