From c3fa9655a4cc1ddaf521e9ca51d2b8c855f649e5 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Mon, 30 Oct 2017 15:50:00 +0000 Subject: [PATCH] Fix PR#35119 : set_union misbehaves with move_iterators. Thanks to Denis Yaroshevskiy for both the bug report and the fix. git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@316914 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/algorithm | 2 +- .../set.union/set_union_move.pass.cpp | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 test/std/algorithms/alg.sorting/alg.set.operations/set.union/set_union_move.pass.cpp diff --git a/include/algorithm b/include/algorithm index 282083a89..f5776b404 100644 --- a/include/algorithm +++ b/include/algorithm @@ -5547,9 +5547,9 @@ __set_union(_InputIterator1 __first1, _InputIterator1 __last1, } else { - *__result = *__first1; if (!__comp(*__first1, *__first2)) ++__first2; + *__result = *__first1; ++__first1; } } diff --git a/test/std/algorithms/alg.sorting/alg.set.operations/set.union/set_union_move.pass.cpp b/test/std/algorithms/alg.sorting/alg.set.operations/set.union/set_union_move.pass.cpp new file mode 100644 index 000000000..aa5b87009 --- /dev/null +++ b/test/std/algorithms/alg.sorting/alg.set.operations/set.union/set_union_move.pass.cpp @@ -0,0 +1,44 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// requires OutputIterator +// && OutputIterator +// && Predicate +// && Predicate +// OutIter +// set_union(InIter1 first1, InIter1 last1, InIter2 first2, InIter2 last2, +// OutIter result, Compare comp); + +#include +#include +#include +#include + +#include "MoveOnly.h" + + +int main() +{ + std::vector lhs, rhs; + lhs.push_back(MoveOnly(2)); + rhs.push_back(MoveOnly(2)); + + std::vector res; + std::set_union(std::make_move_iterator(lhs.begin()), + std::make_move_iterator(lhs.end()), + std::make_move_iterator(rhs.begin()), + std::make_move_iterator(rhs.end()), std::back_inserter(res)); + + assert(res.size() == 1); + assert(res[0].get() == 2); +}