C++03标准中的几处缺陷

原文:http://blogs.msdn.com/xiangfan/archive/2008/08/30/conformance-macros-in-vc-stl.aspx

在VC的STL实现中,有两个宏"_HAS_IMMUTABLE_SETS"和"_HAS_STRICT_CONFORMANCE"(它们在yvals.h中定义)。这两个宏和C++03标准的几处缺陷有关。

1. _HAS_IMMUTABLE_SETS影响set::iterator是否是constant iterator

关联容器要求它的元素是有序的,因此通常修改这些元素的内容非常危险。但是C++03中并没有禁止程序员这么做。

在C++0x中,对关联容器的iterator加入了下述要求:

For associative containers where the value type is the same as the key type, both iterator and const_iterator are constant iterators(在标准中,是这样描述constant iterator:Constant iterators do not satisfy the requirements for output iterators, and the result of the expression *i (for constant iterator i) cannot be used in an expression where an lvalue is required)

这意味着C++0x中你再也不能通过iterator来修改关联容器中的元素的内容了。

讨论:http://www.cpptalk.net/image-vp135370.html

2. _HAS_STRICT_CONFORMANCE会对以下两处产生影响:

a. 对关联容器,它们的erase成员函数的声明有如下的改变

void erase(const_iterator position);
size_type erase(const key_type& x);
void erase(const_iterator first, const_iterator last);

->

iterator erase(const_iterator position);
size_type erase(const key_type& x);
iterator erase(const_iterator first, const_iterator last);

在C++03中,关联容器的erase没有返回值,这和序列容器是不一致的。C++0x修正了这个缺陷。
VC的扩展默认遵守C++0x的规定,即erase将返回iterator。 _HAS_STRICT_CONFORMANCE将禁用这一扩展.

讨论:http://www.tech-archive.net/Archive/VC/microsoft.public.vc.stl/2005-10/msg00030.html

进一步的讨论:http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#103

b. For codecvt::do_length(stateT& state, const externT* from, const externT* from_end, size_t max) const;

在C++03中,要求codecvt和codecvt的do_length函数返回max和(from_end-from)中较小的值.
C++0x放宽了这一要求,仅codecvt需要这样做。

BTW:VC的STL实现是由Dinkumware公司提供的。你可以在每个STL头文件的最后找到"P.J. Plauger"的字样。

你可能感兴趣的:(C++)