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