[英]Why can't I insert a const pointer to a multiset?
According to the documentation for multiset for instance see http://www.cplusplus.com/reference/set/multiset/insert/ . 例如,根据multiset的文档,请参阅http://www.cplusplus.com/reference/set/multiset/insert/ 。 It should be possible to insert a const value.
应该可以插入一个const值。 In my example the multiset is a collection of pointers, but when I try to insert a const pointer I get an error.
在我的例子中,multiset是一个指针集合,但是当我尝试插入一个const指针时,我得到一个错误。
template<typename Key>
struct node_key: public node_base {
node_key(Key k): _key(k) {}
virtual ~node_key() {}
const Key& key() const { return _key;}
protected:
Key _key;
void _copy_key(node_key<Key> *n) const { n->_key=_key;}
};
template <typename Compare>
struct ptr_less_key {
ptr_less_key() : _comp() {}
virtual ~ptr_less_key() {}
template <typename Pointer>
bool operator()(const Pointer& a, const Pointer& b) const { return _comp(a->key(), b->key()); }
Compare _comp;
};
int main() {
typedef node_key<int>* keyp;
std::multiset<keyp,ptr_less_key<std::less<int>>> x;
node_key<int> k(5);
const node_key<int> *p=&k;
x.insert(p); //this fails
return 0;
}
What you are currently doing: You are not trying to insert a const pointer, as you think you do, but a non-const pointer to a const element . 你目前在做什么:你不是试图插入一个const指针,就像你想的那样,而是一个指向const元素的非const指针。
Change this 改变这个
const node_key<int> *p=&k;
to this 对此
node_key<int> *const p=&k;
to make the const keyword apply on the pointer rather than on what it points to. 使const关键字适用于指针而不是它指向的内容。
Given 特定
struct node {
void member();
void const_member() const;
};
consider the four declarations 考虑四个声明
node* pointer_to_node;
const node* pointer_to_const_node;
node* const const_pointer_to_node;
const node* const const_pointer_to_const_node;
There are two different aspects of const
ness: that of the object node
and that of the pointer. const
有两个不同的方面:对象node
和指针node
。 The first two declare mutable pointers to either node
or const node
. 前两个声明指向
node
或const node
可变指针。 A conversion from node*
to const node*
is allowed (and implicit), but not the other way around, as this would allow to modify a const node
. 允许(和隐式)从
node*
到const node*
转换,但不是相反,因为这将允许修改const node
。
The second two declarations declare the respective pointers to be constant, ie these pointers cannot be modified (though the node
pointed to by const_pointer_to_node
can. 后两个声明声明各个指针是常量,即这些指针不能被修改(尽管
const_pointer_to_node
指向的node
可以。
pointer_to_node->member(); // okay
pointer_to_node->const_member(); // okay
pointer_to_node = new node; // okay
pointer_to_node = const_pointer_to_node; // okay
pointer_to_node = pointer_to_const_node; // ERROR
pointer_to_const_node->member(); // ERROR
pointer_to_const_node->const_member(); // okay
pointer_to_const_node = new node; // okay
pointer_to_const_node = pointer_to_node; // okay
pointer_to_const_node = const_pointer_to_node; // okay
pointer_to_const_node = const_pointer_to_const_node; // okay
const_pointer_to_node->member(); // okay
const_pointer_to_node->const_member(); // okay
const_pointer_to_node = new node; // ERROR
const_pointer_to_node = const_pointer_to_node; // ERROR
const_pointer_to_node = pointer_to_const_node; // ERROR
const_pointer_to_const_node->member(); // ERROR
const_pointer_to_const_node->const_member(); // okay
const_pointer_to_const_node = new node; // ERROR
const_pointer_to_const_node = const_pointer_to_node; // ERROR
const_pointer_to_const_node = pointer_to_const_node; // ERROR
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.