C++:在Boost MultiIndex的使用中确定指针类型的类
C++: Determine the class of a pointer type in the use of Boost MultiIndex
这个问题的答案有通用性,但我将用下面的例子来激励它:
我有以下模板类:
template <typename V>
class Collection
{
public:
struct id{};
struct name{};
// A collection parameterized by type V that is indexed by a string 'name'
// and a integer 'id'
// The type V must implement 'get_id()' and a 'get_name()' functions
typedef multi_index_container <
V,
indexed_by<
ordered_unique<
tag<id>, const_mem_fun<V, unsigned int, &V::get_id> >,
ordered_unique<
tag<name>, const_mem_fun<V, std::string, &V::get_name> >
>
> ni_collection;
>
我想修改这个模板,这样我就可以用对象、它们的指针或引用创建一个集合:Collection<Obj>
、Collection<std::unique_ptr<Obj>>
或Collection<Obj *>
。
我将如何修改我的模板来实现这一点?
---更新---我在这里发布了一个相关问题:函数指针类型的计算
综合这两个地方的优秀答案,我终于达到了最初的目标。以下是我当前实现的详细信息:
template <typename V>
class Collection
{
private:
// A type-level function that returns the undecorated type of the object
// So unrwap_object_type<Widget *> = Widget
// unwrap_object_type<std::unique_ptr<Widget>> = Widget
// unwrap_object_type<Widget> = Widget
template<typename T, typename = void>
struct unwrap_object_type { typedef T type; };
template <typename T>
struct unwrap_object_type<T *, void> { typedef T type; };
template<typename T>
struct unwrap_object_type<T,
typename std::conditional<false,
typename T::element_type, void>::type>
{
typedef typename T::element_type type;
};
////
// So that QHNETO_COLLECTION<Widget>, QHNETO_COLLECTION<Widet *>,
// and QHNETO_COLLECTION<std::unique_ptr<Widget>> are valid
typedef typename unwrap_object_type<V>::type W;
// Tags for the two indices (id and name) of the collection
struct id;
struct name;
// A collection parameterized by type V that is indexed by a string 'name'
// and a integer 'id'
// The type V must implement 'get_id()' and a 'get_name()' functions
typedef multi_index_container <
V,
indexed_by<
ordered_unique<
tag<id>,
const_mem_fun<W, unsigned int, &W::get_id> >,
ordered_unique<
tag<name>,
const_mem_fun<W, std::string, &W::get_name> >
>
> ni_collection;
ni_collection m_collection;
};
详述@sehe的答案:Boost.MultiIndex预定义的密钥提取器自动处理取消引用(例如,const_mem_fun<foo,bar,&foo::bar>
可以与foo*
s的multi_index_container
一起使用)。您可以利用此功能编写以下内容(无需任何用户提供的密钥提取器):
在Coliru上直播
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <memory>
namespace bmi = boost::multi_index;
template<typename T>
struct remove_pointer{using type=T;};
template<typename T>
struct remove_pointer<T*>{using type=T;};
template<typename T>
struct remove_pointer<std::shared_ptr<T>>{using type=T;};
template <typename V> class Collection {
public:
struct id;
struct name;
using W=typename remove_pointer<V>::type;
typedef boost::multi_index_container<
V,
bmi::indexed_by<
bmi::ordered_unique<
bmi::tag<id>,
bmi::const_mem_fun<W, unsigned int, &W::get_id>
>,
bmi::ordered_unique<
bmi::tag<name>,
bmi::const_mem_fun<W,const std::string&, &W::get_name>
>
>
> ni_collection;
};
struct Demo {
unsigned _id;
std::string _name;
Demo(unsigned _id,const std::string& _name):_id(_id),_name(_name){}
unsigned get_id() const { return _id; }
std::string const& get_name() const { return _name; }
};
int main() {
Collection<Demo>::ni_collection works{ { 42, "LTUAE" }, { 4, "PI" } };
Collection<Demo *>::ni_collection also_works{ new Demo{ 42, "LTUAE" }, new Demo{ 4, "PI" } };
Collection<std::shared_ptr<Demo>>::ni_collection this_too{ std::make_shared<Demo>( 42, "LTUAE" ), std::make_shared<Demo>( 4, "PI" ) };
}
唯一棘手的部分是const_mem_fun
使用W
=std::remove_pointer<V>::type
(即,如果V
是普通类型,则使用V
;如果是指针,则使用它所指向的类型)。
编辑:更新后的代码使用手工制作的remove_pointer
模板类,而不是std::remove_pointer<V>
,该模板类部分专门用于理解T*
和std::shared_ptr<T>
;例如,您可以将其扩展到std::unique_ptr<T>
或任何其他需要满足的智能指针类。
您可以使用自定义密钥提取器。例如:Boost.MultiIndex密钥提取器的高级功能
对于参考元素,请考虑
boost::reference_wrapper
或std::reference_wrapper
在Coliru上直播
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/ordered_index.hpp>
namespace bmi = boost::multi_index;
template <typename V> class Collection {
struct id_extractor {
typedef unsigned result_type;
template <typename U> result_type operator()(U const&e) const { return e.get_id(); }
template <typename U> result_type operator()(U*e) const { return e->get_id(); }
};
struct name_extractor {
typedef std::string result_type;
template <typename U> result_type const& operator()(U const&e) const { return e.get_name(); }
template <typename U> result_type const& operator()(U*e) const { return e->get_name(); }
};
public:
struct id;
struct name;
// A collection parameterized by type V that is indexed by a string 'name'
// and a integer 'id'
// The type V must implement 'get_id()' and a 'get_name()' functions
typedef boost::multi_index_container<
V, bmi::indexed_by<bmi::ordered_unique<bmi::tag<id>, name_extractor >,
bmi::ordered_unique<bmi::tag<name>, id_extractor > > >
ni_collection;
};
struct Demo {
unsigned _id;
std::string _name;
unsigned get_id() const { return _id; }
std::string const& get_name() const { return _name; }
};
int main() {
Collection<Demo>::ni_collection works{ { 42, "LTUAE" }, { 4, "PI" } };
Collection<Demo *>::ni_collection also_works{ new Demo{ 42, "LTUAE" }, new Demo{ 4, "PI" } };
}
- 将常量指针引用绑定到非常量指针
- 运行时错误:引用绑定到类型为"int"的空指针
- 创建一个函数的 Python 绑定,返回指向带有 boost 的向量的指针
- 与简单地创建派生类指针相比,将基类绑定到派生类有什么优点?
- 获取具有静态局部变量的绑定/推断捕获 lambda 的函数指针
- 努力将指向成员函数的指针绑定到类模板的T成员,然后在槽中调用
- 将引用绑定到指针的语法是什么?(各种)
- 为什么 VS 无法将右值引用绑定到指针?
- 使用 python 绑定来检查 C++ 类型是否是规范方式的指针、引用等?
- 函数参数绑定通过参考与传递指针传递数组的规则
- 排序时引用绑定到 'value_type' 类型的 null 指针
- PyBind11:绑定使用双指针的函数
- C++绑定函数到指针
- 如何破坏包含自指指针的对象
- const自动参考绑定到(NULL)指针 - 实际类型是什么
- 如何将函数指针传递给绑定
- 指向成员的指针如何知道它绑定了哪个成员
- RValue 引用在将其绑定(非指针)对象分配给另一个指针后是否会删除该对象?
- 取消绑定此指针有什么问题
- 为什么 GCC 5.3.0 在将引用绑定到指针时发出警告"this"