Boost Multi_index容器的显式即时
Explicit instantion of boost multi_index container
首先,我想显示工作代码,然后说明我要如何更改事物。这是简单的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();
}
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- DrawIndexedInstanced 具有不同的 Index Count per Instance (Directx
- 有没有办法使用递归函数找到数组中最小值的 INDEX?C++
- 可视化C++:发布模式的运行时库作为'Multi-threaded Debug DLL'
- OpenGL VBO Indexing ( How to compute Index Array)
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 如何根据排序索引的向量对 std::index 集进行排序?
- 在一行代码中理解 index++
- 为什么打印 c 样式字符串的'address of index n'会导致子字符串的输出
- boost beast Websocket Multi Request Server/Client 并不是真正的 mul
- 使用 std::get 访问 std::variant<index>
- 代码中的"vector<<int>vector>&index"是什么意思?
- c++如何使用Curl Multi-Interface连续循环
- CStringArray::GetAt(int index) 返回一个 const. 为什么?
- 如何配置 Doxygen 以在"Class Index"中包含类的类型定义?
- 在 R 中调用"multi file C++ code with external libraries referenced"
- 如何使用 std::get 作为 boost-multi-index 容器键的global_fun
- Boost Multi Index标识索引
- Boost Multi Index:基于列表内容的索引
- Boost Multi-Index中的多个索引查询