admin管理员组文章数量:1128286
I'm not sure if it is specified in the standard that advance/dereference of standard container iterator are noexcept. GCC and MSVC consistently make them noexcept for all standard containers. But clang make them noexcept for std::vector/std::basic_string and noexcept(false) for std::list/std::set/std::map/std::unordered_set/std::unordered_map.
Example code could be found below or on godbolt.
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <list>
#include <string>
#include <ranges>
template<typename Iterator>
concept nothrow_advancable = noexcept(++std::declval<Iterator&>());
template<typename Iterator>
concept nothrow_dereferencable = noexcept(*std::declval<Iterator&>());
static_assert(nothrow_advancable<std::ranges::iterator_t<std::vector<int>&>>);
static_assert(nothrow_advancable<std::ranges::iterator_t<std::basic_string<char>&>>);
static_assert(nothrow_advancable<std::ranges::iterator_t<std::list<int>&>>); // not noexcept with clang
static_assert(nothrow_advancable<std::ranges::iterator_t<std::set<int>&>>); // not noexcept with clang
static_assert(nothrow_advancable<std::ranges::iterator_t<std::map<int, int>&>>); // not noexcept with clang
static_assert(nothrow_advancable<std::ranges::iterator_t<std::unordered_set<int>&>>); // not noexcept with clang
static_assert(nothrow_advancable<std::ranges::iterator_t<std::unordered_map<int, int>&>>); // not noexcept with clang
static_assert(nothrow_dereferencable<std::ranges::iterator_t<std::vector<int>&>>);
static_assert(nothrow_dereferencable<std::ranges::iterator_t<std::basic_string<char>&>>);
static_assert(nothrow_dereferencable<std::ranges::iterator_t<std::list<int>&>>); // not noexcept with clang
static_assert(nothrow_dereferencable<std::ranges::iterator_t<std::set<int>&>>); // not noexcept with clang
static_assert(nothrow_dereferencable<std::ranges::iterator_t<std::map<int, int>&>>); // not noexcept with clang
static_assert(nothrow_dereferencable<std::ranges::iterator_t<std::unordered_set<int>&>>); // not noexcept with clang
static_assert(nothrow_dereferencable<std::ranges::iterator_t<std::unordered_map<int, int>&>>); // not noexcept with clang
本文标签: cWhy is clang not consistent on noexcept(it) and noexcept(*it) for containersStack Overflow
版权声明:本文标题:c++ - Why is clang not consistent on noexcept(++it) and noexcept(*it) for containers? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736714529a1949126.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论