Информация об изменениях

Сообщение Re: *std::prev(std::end(<associative-container>)) -> UB? от 23.12.2019 13:45

Изменено 23.12.2019 13:58 rg45

Re: *std::prev(std::end(<associative-container>)) -> UB?
Здравствуйте, _niko_, Вы писали:

__>На сколько корректна следующая запись:

__>
__>std::set<int> associative_container{ 1, 2, 3, 4 };
__>int back_value{ *std::prev(std::end(associative_container)) };
__>

__>Стандарт дает какие то гарантии, что декремент end-итератора будет указывать на последний элемент ассоциативного контейнера?
__>Или это все же UB?

https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator

Это не стандарт, конечно, но доверять этому ресурсу можно, имхо:

Notes
The begin iterator is not decrementable and the behavior is undefined if --container.begin() is evaluated. A bidirectional iterator does not have to be dereferenceable to be decrementable (in particular, the end iterator is not dereferenceable but is decrementable)


Само собой, для пустого контейнера, декремент от end приведет к UB, ибо end равен begin.
Re: *std::prev(std::end(<associative-container>)) -> UB?
Здравствуйте, _niko_, Вы писали:

__>На сколько корректна следующая запись:

__>
__>std::set<int> associative_container{ 1, 2, 3, 4 };
__>int back_value{ *std::prev(std::end(associative_container)) };
__>

__>Стандарт дает какие то гарантии, что декремент end-итератора будет указывать на последний элемент ассоциативного контейнера?
__>Или это все же UB?

https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator

Это не стандарт, конечно, но доверять этому ресурсу можно, имхо:

Notes
The begin iterator is not decrementable and the behavior is undefined if --container.begin() is evaluated. A bidirectional iterator does not have to be dereferenceable to be decrementable (in particular, the end iterator is not dereferenceable but is decrementable)


Само собой, для пустого контейнера, декремент от end приведет к UB, ибо end равен begin.