[PATCH] D134675: [ADT] Make mapped_iterator copy assignable

James Player via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 26 14:21:20 PDT 2022


jplayer-nv created this revision.
jplayer-nv added a project: LLVM.
Herald added a project: All.
jplayer-nv requested review of this revision.
Herald added a subscriber: llvm-commits.

As mentioned in https://discourse.llvm.org/t/rfc-extend-ranges-infrastructure-to-better-match-c-20/65377

Lambda objects are not copy assignable, and therefore neither are iterator types which hold a lambda.  STL code require iterators be copy assignable.  Users may not use `mapped_iterator` with a `std::deque` for example: https://godbolt.org/z/4Px7odEEd

This blog post <https://www.fluentcpp.com/2019/04/16/an-alternative-design-to-iterators-and-ranges-using-stdoptional/> explains the problem and solution.  We define a wrapper class to store callable objects with two specialization.

1. Specialization for non-function types
  - Use as use `std::optional` as storage for non-function callable.
  - Define `operator=()` implementation(s) which use `std::optional::emplace()` instead of the assignment operator.
2. Specialization for function types
  - Store as a pointer (even if template argument is a function reference).
  - Default construct pointer to `nullptr`.

This Callable wrapper class is now default constructible (with invalid state) and copy/move assignable.

With these new properties available on the callable object, `mapped_iterator` can define a default constructor as well.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D134675

Files:
  llvm/include/llvm/ADT/STLExtras.h
  llvm/unittests/ADT/MappedIteratorTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134675.463015.patch
Type: text/x-patch
Size: 10660 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220926/76a3b72c/attachment.bin>


More information about the llvm-commits mailing list