异常处理迭代器接口
我正在尝试为我正在设计的库制作一个无懈可击的界面。用户要输入二维数据,因此我认为类似于 std::transform 的迭代器接口将是透明的。 但是,我不确定如何异常处理迭代器的任何滥用。
我的界面是这样的(如果有更好的界面,我可以更改界面):
template<typename InputItrX, typename InputItrY>
set_data(InputItrX beginX, InputItrX endX, InputItrY beginY)
{
//What exception handling should I do here?
size_t array_size = endX-beginX; //get the size of the xarray.
my_xVector.resize(array_size); //resize my internal container
my_yVector.resize(array_size); // ..and for the ydata.
std::copy(beginX, endX, my_xVector.begin()); //copy X
std::copy(beginY, beginY+array_size, my_yVector.begin()); //copy Y
}
例如,如果用户对界面感到困惑并写入
set_data(xdata.begin(), ydata.begin(), xdata.end());
或者他们的xdata
有20, 我的程序将变得不确定元素,但它们的 ydata 没有。
是否可以在我的图书馆界面中检查此类错误?
I'm trying to make a watertight interface to a library that I'm designing. The user is to enter two dimensional data and so I thought an iterator interface similar to std::transform
would be transparant.
However, I am unsure how to exception handle any abuse of the iterators.
My interface is like so (I can change the interface if there is a better one):
template<typename InputItrX, typename InputItrY>
set_data(InputItrX beginX, InputItrX endX, InputItrY beginY)
{
//What exception handling should I do here?
size_t array_size = endX-beginX; //get the size of the xarray.
my_xVector.resize(array_size); //resize my internal container
my_yVector.resize(array_size); // ..and for the ydata.
std::copy(beginX, endX, my_xVector.begin()); //copy X
std::copy(beginY, beginY+array_size, my_yVector.begin()); //copy Y
}
For example, my program becomes undefined if the user get muddled up with the interface and writes
set_data(xdata.begin(), ydata.begin(), xdata.end());
or maybe their xdata
has 20 elements but their ydata
has none.
Is it possible to check for such mistakes in my library interface?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会向该方法添加任何检查,但会记录异常规范取决于所使用的迭代器。因此,如果用户不关心性能损失或未检查的迭代器,则可以使用检查的迭代器并获得最佳性能。我认为大多数 STL 迭代器的实现都有检查迭代器不兼容性的断言。这些错误不需要在发布模式下检查,因为它们是程序员的错误。
这使得您的方法与没有 - 运算符的迭代器不兼容!它只能与随机访问迭代器一起使用。您应该将其提取到 resize_vectors 模板,该模板可以为随机访问迭代器实现,但不会为其他迭代器进行任何大小调整。在 std::copy 中,您必须使用插入器迭代器来调整向量大小,同时在向量没有足够容量时进行插入。
I wouldn't add any checks to the method, but document that the exception specification depends on the used iterators. So the user can use checked iterators if he doesn't care about performance losses or unchecked iterators and get best performance. I think most implementations of STL iterators has asserts that check for iterator incompatibilities. Theses errors don't need check in release mode, because they are programmer mistakes.
This makes your method incompatible with iterators that don't have the -operator! It can be used only with random access iterator. You should extract this to a resize_vectors template which can be implemented for random access iterators, but doesn't make any resize for other iterators. In the
std::copy
you have to use an inserter iterator that resizes the vector, while inserting if the vectors don't have enough capacity.