从任意Map获取指向mapped_type的指针或引用
Getting Pointer or Reference to mapped_type from Arbitrary Map
编辑:我已经找到并写了一个解决我的问题的方法,但我没有回答这个问题,因为我的解决方案可能仍然不理想。
我正在编写一个小库,旨在执行地图的地图例程,但我在设计一组类模板时遇到了麻烦,这些模板将让我获得一个指针或引用(取决于地图的value_type的second_type)到地图的mapped_type,而不管地图的类型(例如std::map, boost::ptr_map)。
为了进一步说明,我列出了一些输入类型和期望的输出类型。
Case Input Type Output Type
A std::map<int, std::map<int, int> > std::map<int, int>&
B std::map<int, boost::ptr_map<int, int> > boost::ptr_map<int, int>&
C boost::ptr_map<int, std::map<int, int> > std::map<int, int>* const
D std::map<int, std::map<int, int> >* std::map<int, int>&
E std::map<int, boost::ptr_map<int, int> >* boost::ptr_map<int, int>&
F boost::ptr_map<int, std::map<int, int> >* std::map<int, int>* const
我的代码通过了情况A、B、D和E,但在情况C和f上失败了。
template <class Map>
struct map_utils
{
template <class K>
static typename
boost::remove_pointer<
typename Map::value_type
>::type::second_type&
get(Map& m, const K k)
{
return m[k];
}
template <class K>
static typename
boost::remove_pointer<
typename Map::value_type
>::type::second_type&
get(const Map& m, const K k)
{
return const_cast<Map&>(m)[k];
}
};
template <class Map>
struct map_utils<Map*>
{
template <class T>
static typename
boost::remove_pointer<
typename Map::value_type
>::type::second_type&
get(Map* m, const T t)
{
return (*m)[t];
}
template <class T>
static typename
boost::remove_pointer<
typename Map::value_type
>::type::second_type&
get(const Map* m, const T t)
{
return const_cast<Map*>(m)->operator[](t);
}
};
我试图使用boost::mpl来做到这一点,这就是我到目前为止所做的,但是我使用两个版本的代码得到相同的错误。
错误。
error: invalid initialization of reference of type ‘std::map<int, double>* const&’ from expression of type ‘boost::ptr_container_detail::reversible_ptr_container<boost::ptr_container_detail::map_config<std::map<int, double>, std::map<int, void*, std::less<int>, std::allocator<std::pair<const int, void*> > >, true>, boost::heap_clone_allocator>::Ty_’
结构体的修改专门化,以处理非映射指针的l值。
template <class K>
static typename
boost::mpl::if_<
boost::is_pointer<
typename boost::remove_pointer<
typename Map::value_type
>::type::second_type
>,
typename boost::remove_pointer<
typename boost::remove_const<
typename Map::value_type
>::type
>::type::second_type,
typename boost::remove_pointer<
typename Map::value_type
>::type::second_type&
>::type
get(Map& m, const K k)
{
return m[k];
}
C和F似乎是错误的,映射类型不是boost::ptr_map。否则,听起来您可以使用完整的模板专门化来确定它是std::map还是boost::ptr_map。像这样:
template <class Map>
class Whatever;
template <class K, class V>
class Whatever<std::map<K, V> >
{
public:
typedef V& Type;
};
template <class K, class V>
class Whatever<std::map<K, V>* >
{
public:
typedef V& Type;
};
template <class K, class V>
class Whatever<boost::ptr_map<K, V> >
{
public:
typedef V* const Type;
};
template <class K, class V>
class Whatever<boost::ptr_map<K, V>* >
{
public:
typedef V* const Type;
};
我已经得到了代码编译成功,并大大简化了它在这个过程中,通过使用mapped_type类型定义,像这样:
template <class K>
static typename boost::remove_pointer<
typename Map::mapped_type
>::type&
get(Map& m, const K k)
{
return m[k];
}
在上面问题中定义的结构体的专用版本中,只有方法体需要更改,以便也适应指向映射的指针。如果您想要完整的代码清单,请告诉我,我会将整个代码转储到这里。
相关文章:
- 为什么指针没有隐式转换为 <type> const*const
- 在接口文件中使用模板时出现"not a type"错误的函数指针
- 不同指针类型之间的无效比较:'type*' 和 'type&(*)()'
- 'cv::Mat::type':非标准语法;使用 '&' 创建指向成员的指针
- 将shared_ptr投射<void>到shared_ptr<type>时智能指针的行为
- "base operand of ‘->’ has non-pointer type" 但它是指针
- 尝试使用指向函数的指针时出错"does not name a type"
- 'Incomplete type' 为标准::函数声明指向成员的指针函数模板参数时出错
- C++ 错误 C2662 无法将"this"指针从"const Type"转换为"Type &"
- 使用公共类函数初始化节点类型<Type>指针C++
- 指向 c++ 中具有自动返回'type'的模板成员的指针?
- 编译时指针"does not have a type"?
- "Does not name a type"错误,但类指针已经有前向声明?
- 对<type>链表使用列表和指针(经典 C)之间的差异
- 常量函数指针类型作为模板参数的"invalid use of incomplete type"
- 引用(类型 &)"non-const lvalue reference to type cannot bind"错误,但指针(类型 *)不错误
- 模板函数指针:"overloaded function with no contextual type information"
- "error: request for member ‘size’ in ‘a’, which is of pointer type"但我不认为这是一个指针
- 为什么"TYPE* const"指针的行为不同?
- new(size, value) Type[0]返回的指针是否合法,是否可以用来构建数组?