将 STL 容器<T *> 转换为容器<T 常量 *>
Converting STL container<T *> to container<T const *>
我正在寻找一种方法来制定一个类具有:
- 一个使用指针的STL容器的接口,具有最大的'constness'
- 但是它在内部改变了指向对象
- 与非const模拟 相比,没有额外的运行时开销
理想情况下,与非const版本相比,该解决方案不会编译出额外的代码,因为const/非const-ness在这里只是对程序员的一种帮助。
以下是我到目前为止所做的尝试:
#include <list>
#include <algorithm>
using namespace std;
typedef int T;
class C
{
public:
// Elements pointed to are mutable, list is not, 'this' is not - compiles OK
list<T *> const & get_t_list() const { return t_list_; }
// Neither elements nor list nor' this' are mutable - doesn't compile
list<T const *> const & get_t_list2() const { return t_list_; }
// Sanity check: T const * is the problem - doesn't compile
list<T const *> & get_t_list3() { return t_list_; }
// Elements pointed to are immutable, 'this' and this->t_list_ are
// also immutable - Compiles OK, but actually burns some CPU cycles
list<T const *> get_t_list4() const {
return list<T const *>( t_list_.begin() , t_list_.end() );
}
private:
list<T *> t_list_;
};
如果没有类型转换的解决方案,我想要关于如何制定具有所描述属性的类的其他建议。
暂时假设您可以将list<T*>&
转换为list<T const *>&
。现在考虑下面的代码:
list<char*> a;
list<char const*>& b = a;
b.push_back("foo");
a.front()[0] = 'x'; // oops mutating const data
将T**
转化为T const**
也是同样的概念问题。
如果你想提供对底层数据的只读访问,你需要提供它的一些自定义视图,可能使用自定义迭代器。
类似如下:
template <typename It>
class const_const_iterator {
private:
using underlying_value_type = typename std::iterator_traits<It>::value_type;
static_assert(std::is_pointer<underlying_value_type>(),
"must be an iterator to a pointer");
using pointerless_value_type = typename std::remove_pointer<underlying_value_type>::type;
public:
const_const_iterator(It it) : it(it) {}
using value_type = pointerless_value_type const*;
value_type operator*() const {
return *it; // *it is a T*, but we return a T const*,
// converted implicitly
// also note that it is not assignable
}
// rest of iterator implementation here
// boost::iterator_facade may be of help
private:
It it;
};
template <typename Container>
class const_const_view {
private:
using container_iterator = typename Container::iterator;
public:
using const_iterator = const_const_iterator<container_iterator>;
using iterator = const_iterator;
const_const_view(Container const& container) : container(&container) {}
const_iterator begin() const { return iterator(container->begin()); }
const_iterator end() const { return iterator(container->end()); }
private:
Container const* container;
}
不要返回容器。返回迭代器。
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中