[PATCH] D112957: [llvm][adt] make_first_range returning reference to temporary
Jeff Niu via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 1 13:38:36 PDT 2021
Mogball created this revision.
Herald added a subscriber: dexonsmith.
Mogball requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D112957
Files:
llvm/include/llvm/ADT/STLExtras.h
Index: llvm/include/llvm/ADT/STLExtras.h
===================================================================
--- llvm/include/llvm/ADT/STLExtras.h
+++ llvm/include/llvm/ADT/STLExtras.h
@@ -1251,20 +1251,35 @@
}
};
+namespace detail {
+/// Return a reference to the first or second member of a reference. Otherwise,
+/// return a copy of the member of a temporary.
+template <typename EltTy, typename FirstTy> class first_or_second_type {
+public:
+ using type =
+ typename std::conditional_t<std::is_reference<EltTy>::value, FirstTy,
+ std::remove_reference_t<FirstTy>>;
+};
+} // end namespace detail
+
/// Given a container of pairs, return a range over the first elements.
template <typename ContainerTy> auto make_first_range(ContainerTy &&c) {
- return llvm::map_range(
- std::forward<ContainerTy>(c),
- [](decltype((*std::begin(c))) elt) -> decltype((elt.first)) {
- return elt.first;
- });
+ using EltTy = decltype((*std::begin(c)));
+ return llvm::map_range(std::forward<ContainerTy>(c),
+ [](EltTy elt) -> typename detail::first_or_second_type<
+ EltTy, decltype((elt.first))>::type {
+ return elt.first;
+ });
}
/// Given a container of pairs, return a range over the second elements.
template <typename ContainerTy> auto make_second_range(ContainerTy &&c) {
+ using EltTy = decltype((*std::begin(c)));
return llvm::map_range(
std::forward<ContainerTy>(c),
- [](decltype((*std::begin(c))) elt) -> decltype((elt.second)) {
+ [](EltTy elt) ->
+ typename detail::first_or_second_type<EltTy,
+ decltype((elt.second))>::type {
return elt.second;
});
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112957.383874.patch
Type: text/x-patch
Size: 1836 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211101/53639994/attachment.bin>
More information about the llvm-commits
mailing list