checked_array_iterator在<T>C++11
checked_array_iterator<T> in C++11
C++11提供了std::array<T>
来包装C数组,但仅限于在编译时知道数组大小的情况。处理只有在运行时才知道大小的数组的最佳方法是什么?
背景
我正在将一些代码从MSVC移植到GCC。MSVC提供stdext::checked_array_iterator<T>
模板来为代码行提供一些保护,例如:
std::copy(v.begin(), v.end(), stdext::checked_array_iterator<int*>(arr, numVals));
到目前为止,我可以想到两种选择:放弃安全检查或编写自己的实现。关于这一点,我将感谢对这一实施的任何建设性意见:
namespace stdext {
template<typename T>
struct checked_array_iterator
{
private:
T _val;
size_t _len;
public:
typedef typename std::remove_pointer<T>::type value_type;
checked_array_iterator(T val, size_t len) : _val(val), _len(len) {}
checked_array_iterator<T> operator++(int)
{
if(_len == 0)
throw std::range_error("Array iterator overrun");
checked_array_iterator<T> retval = *this;
_val++;
_len--;
return retval;
}
checked_array_iterator<T> & operator++()
{
if(_len == 0)
throw std::range_error("Array iterator overrun");
_val++;
_len--;
return *this;
}
value_type & operator*()
{
return *_val;
}
bool operator==(checked_array_iterator<T>& other) { return other._val == _val; }
bool operator!=(checked_array_iterator<T>& other) { return !(other == *this); }
T operator->() { return _val; }
};
}
namespace std
{
template <typename T>
struct iterator_traits<stdext::checked_array_iterator<T>>
{
typedef std::ptrdiff_t difference_type;
typedef typename std::remove_pointer<T>::type value_type;
typedef T pointer;
typedef value_type& reference;
typedef std::input_iterator_tag iterator_category;
};
}
这会这么糟糕吗?
if (v.size() > numVals)
throw std::runtime_error("oops");
std::copy(v.begin(), v.end(), arr);
它也更高效,因为它只检查一次大小是否正常,而不是每个元素检查一次。
在某些情况下,您可以使用特征映射(或编写类似的实现)。它本质上是一个向量,它不管理自己的存储,而是在构造函数中获取指针和大小。在这方面,它与stdext::checked_array_iterator<T>
提供的安全级别非常相似。另一方面,它不意味着是迭代器,它意味着是矩阵(或特殊情况下的向量)类。Ane Eigen既是自由的,也是多平台的。
相关文章:
- MSVC是否支持C++11样式的属性而不是__declspec
- 创建LinkedList退出,返回代码为-11(SIGSEGV)
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 如何将模板转换为C++11之前的模板
- EASTL矢量<向量<int>>连续的
- c++11评估顺序(未定义的行为)
- C++中的VLA,扩展名为std=C++11
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- "类模板示例<int>;"语句对 C++11 是什么意思?
- this_thread::sleep_for和计时时钟之间的关系是否由C++11标准指定
- 如何使用lock_guard在c++11中实现scoped_lock功能
- C++11 中不同类型的对象的 std::array 的替代方案
- 为什么 -mmacosx-version-min=10.10 不阻止使用标记为从 10.11 开始的函数?
- 为什么我的C++代码中出现'Segmentation Fault: 11'行?
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- 别名模板的专业化 C++11 中没有开销的最佳替代方案
- STLPort using C++11
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy