使用 boost::fusion::map 的 const 正确性问题
我正在编写一些使用 boost::fusion::map 的类。下面你会发现一个简化的代码:
template <typename ObjDef>
struct Object
{
typedef
typename
boost::fusion::result_of::as_map<valid_list_of_fusion_pairs>::type map_type;
map_type map;
Object()
: map()
{}
Object(Object const & other)
: map(other.map)
{}
Object & operator=(Object const & other)
{
map = other.map;
return *this;
}
// non-const version
template <typename FieldId>
typename
boost::fusion::result_of::at_key<map_type, FieldId>::type get()
{
return boost::fusion::at_key<FieldId>(map);
}
// const version
template <typename FieldId>
typename
boost::fusion::result_of::at_key<map_type const, FieldId>::type get() const
{
return boost::fusion::at_key<FieldId>(map);
}
};
和另一个类:
template <typename Obj, typename FieldId>
class Field
{
private:
Obj &obj_m;
public:
// type returned by \c operator()
typedef
typename
boost::fusion::result_of::at_key<typename Obj::map_type, FieldId>::type return_type;
// type returned by \c operator() const
typedef
typename
boost::fusion::result_of::at_key<typename Obj::map_type const, FieldId>::type return_type_const;
Field(Obj &obj)
: obj_m(obj)
{ }
virtual ~Field()
{ }
return_type operator()()
{
return obj_m.template get<FieldId>();
}
return_type_const operator()() const
{
/*
* PROBLEM!
*/
Obj const & obj_const = obj_m;
return obj_const.template get<FieldId>();
}
};
寻找“问题!”在上面代码的注释中。在该方法中,编译器忽略该方法的 const 限定符并调用 obj_m.get() 的非 const 版本,允许执行以下操作:
obj_m.template get<FieldId>() = 10;
这是不正确的,因为该方法是 const!然后,为了强制编译器调用 const 版本,声明对 obj_m 的 const 引用。现在该句子
obj_const.template get<FieldId>() = 0;
产生编译错误。到目前为止,这对于当前方法来说不是问题,但对于 const 正确性来说并不方便,并且绝对不希望
知道为什么会发生这种情况? 谢谢!
I'm writing some classes that use boost::fusion::map. Bellow you find a simplified code:
template <typename ObjDef>
struct Object
{
typedef
typename
boost::fusion::result_of::as_map<valid_list_of_fusion_pairs>::type map_type;
map_type map;
Object()
: map()
{}
Object(Object const & other)
: map(other.map)
{}
Object & operator=(Object const & other)
{
map = other.map;
return *this;
}
// non-const version
template <typename FieldId>
typename
boost::fusion::result_of::at_key<map_type, FieldId>::type get()
{
return boost::fusion::at_key<FieldId>(map);
}
// const version
template <typename FieldId>
typename
boost::fusion::result_of::at_key<map_type const, FieldId>::type get() const
{
return boost::fusion::at_key<FieldId>(map);
}
};
and another class:
template <typename Obj, typename FieldId>
class Field
{
private:
Obj &obj_m;
public:
// type returned by \c operator()
typedef
typename
boost::fusion::result_of::at_key<typename Obj::map_type, FieldId>::type return_type;
// type returned by \c operator() const
typedef
typename
boost::fusion::result_of::at_key<typename Obj::map_type const, FieldId>::type return_type_const;
Field(Obj &obj)
: obj_m(obj)
{ }
virtual ~Field()
{ }
return_type operator()()
{
return obj_m.template get<FieldId>();
}
return_type_const operator()() const
{
/*
* PROBLEM!
*/
Obj const & obj_const = obj_m;
return obj_const.template get<FieldId>();
}
};
Look for "PROBLEM!" in the comments in the above code. In that method, the compiler ignores the const qualifier of the method and calls the non-const version of obj_m.get() allowing to do something like:
obj_m.template get<FieldId>() = 10;
which is not correct since this method is const! Then, to force the compiler to call the const version, a const reference to obj_m is declared. Now the sentence
obj_const.template get<FieldId>() = 0;
produces a compilation error. So far this is not a problem for the current method,but it is not convenient for const correctness and it is definitively undesired
any idea why is this happening?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论