2010-05-11 19:42:16 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 10:56:40 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-05-11 19:42:16 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// <tuple>
|
|
|
|
|
|
|
|
// template <class... Types> class tuple;
|
|
|
|
|
|
|
|
// tuple& operator=(tuple&& u);
|
|
|
|
|
2020-06-01 10:38:23 -04:00
|
|
|
// UNSUPPORTED: c++03
|
2015-02-19 02:10:42 +00:00
|
|
|
|
2018-12-18 23:19:00 +00:00
|
|
|
#include <memory>
|
2010-05-11 19:42:16 +00:00
|
|
|
#include <tuple>
|
2015-01-09 20:25:52 +00:00
|
|
|
#include <utility>
|
2010-05-11 19:42:16 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
2019-05-31 18:35:30 +00:00
|
|
|
#include "test_macros.h"
|
2015-01-28 21:22:53 +00:00
|
|
|
#include "MoveOnly.h"
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2016-07-25 02:36:42 +00:00
|
|
|
struct NonAssignable {
|
|
|
|
NonAssignable& operator=(NonAssignable const&) = delete;
|
|
|
|
NonAssignable& operator=(NonAssignable&&) = delete;
|
|
|
|
};
|
|
|
|
struct CopyAssignable {
|
|
|
|
CopyAssignable& operator=(CopyAssignable const&) = default;
|
|
|
|
CopyAssignable& operator=(CopyAssignable&&) = delete;
|
|
|
|
};
|
|
|
|
static_assert(std::is_copy_assignable<CopyAssignable>::value, "");
|
|
|
|
struct MoveAssignable {
|
|
|
|
MoveAssignable& operator=(MoveAssignable const&) = delete;
|
|
|
|
MoveAssignable& operator=(MoveAssignable&&) = default;
|
|
|
|
};
|
2018-07-31 11:56:20 -04:00
|
|
|
struct NothrowMoveAssignable {
|
|
|
|
NothrowMoveAssignable& operator=(NothrowMoveAssignable&&) noexcept { return *this; }
|
|
|
|
};
|
|
|
|
struct PotentiallyThrowingMoveAssignable {
|
|
|
|
PotentiallyThrowingMoveAssignable& operator=(PotentiallyThrowingMoveAssignable&&) { return *this; }
|
|
|
|
};
|
2016-07-25 02:36:42 +00:00
|
|
|
|
|
|
|
struct CountAssign {
|
|
|
|
static int copied;
|
|
|
|
static int moved;
|
|
|
|
static void reset() { copied = moved = 0; }
|
|
|
|
CountAssign() = default;
|
|
|
|
CountAssign& operator=(CountAssign const&) { ++copied; return *this; }
|
|
|
|
CountAssign& operator=(CountAssign&&) { ++moved; return *this; }
|
|
|
|
};
|
|
|
|
int CountAssign::copied = 0;
|
|
|
|
int CountAssign::moved = 0;
|
|
|
|
|
2021-02-09 19:12:16 -05:00
|
|
|
TEST_CONSTEXPR_CXX20
|
|
|
|
bool test()
|
2010-05-11 19:42:16 +00:00
|
|
|
{
|
|
|
|
{
|
|
|
|
typedef std::tuple<> T;
|
|
|
|
T t0;
|
|
|
|
T t;
|
|
|
|
t = std::move(t0);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
typedef std::tuple<MoveOnly> T;
|
|
|
|
T t0(MoveOnly(0));
|
|
|
|
T t;
|
|
|
|
t = std::move(t0);
|
|
|
|
assert(std::get<0>(t) == 0);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
typedef std::tuple<MoveOnly, MoveOnly> T;
|
|
|
|
T t0(MoveOnly(0), MoveOnly(1));
|
|
|
|
T t;
|
|
|
|
t = std::move(t0);
|
|
|
|
assert(std::get<0>(t) == 0);
|
|
|
|
assert(std::get<1>(t) == 1);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
typedef std::tuple<MoveOnly, MoveOnly, MoveOnly> T;
|
|
|
|
T t0(MoveOnly(0), MoveOnly(1), MoveOnly(2));
|
|
|
|
T t;
|
|
|
|
t = std::move(t0);
|
|
|
|
assert(std::get<0>(t) == 0);
|
|
|
|
assert(std::get<1>(t) == 1);
|
|
|
|
assert(std::get<2>(t) == 2);
|
|
|
|
}
|
2016-07-25 02:36:42 +00:00
|
|
|
{
|
|
|
|
// test reference assignment.
|
|
|
|
using T = std::tuple<int&, int&&>;
|
|
|
|
int x = 42;
|
|
|
|
int y = 100;
|
|
|
|
int x2 = -1;
|
|
|
|
int y2 = 500;
|
|
|
|
T t(x, std::move(y));
|
|
|
|
T t2(x2, std::move(y2));
|
|
|
|
t = std::move(t2);
|
|
|
|
assert(std::get<0>(t) == x2);
|
|
|
|
assert(&std::get<0>(t) == &x);
|
|
|
|
assert(std::get<1>(t) == y2);
|
|
|
|
assert(&std::get<1>(t) == &y);
|
|
|
|
}
|
2021-02-09 19:12:16 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int, char**)
|
|
|
|
{
|
|
|
|
test();
|
|
|
|
#if TEST_STD_VER >= 20
|
|
|
|
static_assert(test());
|
|
|
|
#endif
|
|
|
|
|
2016-07-25 02:36:42 +00:00
|
|
|
{
|
|
|
|
// test that the implicitly generated move assignment operator
|
|
|
|
// is properly deleted
|
|
|
|
using T = std::tuple<std::unique_ptr<int>>;
|
|
|
|
static_assert(std::is_move_assignable<T>::value, "");
|
|
|
|
static_assert(!std::is_copy_assignable<T>::value, "");
|
|
|
|
}
|
|
|
|
{
|
2021-02-09 19:12:16 -05:00
|
|
|
using T = std::tuple<int, NonAssignable>;
|
|
|
|
static_assert(!std::is_move_assignable<T>::value, "");
|
2016-07-25 02:36:42 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
using T = std::tuple<int, MoveAssignable>;
|
|
|
|
static_assert(std::is_move_assignable<T>::value, "");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// The move should decay to a copy.
|
|
|
|
CountAssign::reset();
|
|
|
|
using T = std::tuple<CountAssign, CopyAssignable>;
|
|
|
|
static_assert(std::is_move_assignable<T>::value, "");
|
|
|
|
T t1;
|
|
|
|
T t2;
|
|
|
|
t1 = std::move(t2);
|
|
|
|
assert(CountAssign::copied == 1);
|
|
|
|
assert(CountAssign::moved == 0);
|
|
|
|
}
|
2018-07-31 11:56:20 -04:00
|
|
|
{
|
|
|
|
using T = std::tuple<int, NonAssignable>;
|
|
|
|
static_assert(!std::is_move_assignable<T>::value, "");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
using T = std::tuple<int, MoveAssignable>;
|
|
|
|
static_assert(std::is_move_assignable<T>::value, "");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
using T = std::tuple<NothrowMoveAssignable, int>;
|
|
|
|
static_assert(std::is_nothrow_move_assignable<T>::value, "");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
using T = std::tuple<PotentiallyThrowingMoveAssignable, int>;
|
|
|
|
static_assert(!std::is_nothrow_move_assignable<T>::value, "");
|
|
|
|
}
|
2021-02-24 14:53:21 -05:00
|
|
|
{
|
|
|
|
// We assign through the reference and don't move out of the incoming ref,
|
|
|
|
// so this doesn't work (but would if the type were CopyAssignable).
|
|
|
|
using T1 = std::tuple<MoveAssignable&, int>;
|
|
|
|
static_assert(!std::is_move_assignable<T1>::value, "");
|
|
|
|
|
|
|
|
// ... works if it's CopyAssignable
|
|
|
|
using T2 = std::tuple<CopyAssignable&, int>;
|
|
|
|
static_assert(std::is_move_assignable<T2>::value, "");
|
|
|
|
|
|
|
|
// For rvalue-references, we can move-assign if the type is MoveAssignable
|
|
|
|
// or CopyAssignable (since in the worst case the move will decay into a copy).
|
|
|
|
using T3 = std::tuple<MoveAssignable&&, int>;
|
|
|
|
using T4 = std::tuple<CopyAssignable&&, int>;
|
|
|
|
static_assert(std::is_move_assignable<T3>::value, "");
|
|
|
|
static_assert(std::is_move_assignable<T4>::value, "");
|
|
|
|
|
|
|
|
// In all cases, we can't move-assign if the types are not assignable,
|
|
|
|
// since we assign through the reference.
|
|
|
|
using T5 = std::tuple<NonAssignable&, int>;
|
|
|
|
using T6 = std::tuple<NonAssignable&&, int>;
|
|
|
|
static_assert(!std::is_move_assignable<T5>::value, "");
|
|
|
|
static_assert(!std::is_move_assignable<T6>::value, "");
|
|
|
|
}
|
Support tests in freestanding
Summary:
Freestanding is *weird*. The standard allows it to differ in a bunch of odd
manners from regular C++, and the committee would like to improve that
situation. I'd like to make libc++ behave better with what freestanding should
be, so that it can be a tool we use in improving the standard. To do that we
need to try stuff out, both with "freestanding the language mode" and
"freestanding the library subset".
Let's start with the super basic: run the libc++ tests in freestanding, using
clang as the compiler, and see what works. The easiest hack to do this:
In utils/libcxx/test/config.py add:
self.cxx.compile_flags += ['-ffreestanding']
Run the tests and they all fail.
Why? Because in freestanding `main` isn't special. This "not special" property
has two effects: main doesn't get mangled, and main isn't allowed to omit its
`return` statement. The first means main gets mangled and the linker can't
create a valid executable for us to test. The second means we spew out warnings
(ew) and the compiler doesn't insert the `return` we omitted, and main just
falls of the end and does whatever undefined behavior (if you're luck, ud2
leading to non-zero return code).
Let's start my work with the basics. This patch changes all libc++ tests to
declare `main` as `int main(int, char**` so it mangles consistently (enabling us
to declare another `extern "C"` main for freestanding which calls the mangled
one), and adds `return 0;` to all places where it was missing. This touches 6124
files, and I apologize.
The former was done with The Magic Of Sed.
The later was done with a (not quite correct but decent) clang tool:
https://gist.github.com/jfbastien/793819ff360baa845483dde81170feed
This works for most tests, though I did have to adjust a few places when e.g.
the test runs with `-x c`, macros are used for main (such as for the filesystem
tests), etc.
Once this is in we can create a freestanding bot which will prevent further
regressions. After that, we can start the real work of supporting C++
freestanding fairly well in libc++.
<rdar://problem/47754795>
Reviewers: ldionne, mclow.lists, EricWF
Subscribers: christof, jkorous, dexonsmith, arphaman, miyuki, libcxx-commits
Differential Revision: https://reviews.llvm.org/D57624
llvm-svn: 353086
2019-02-04 20:31:13 +00:00
|
|
|
|
2018-07-31 11:56:20 -04:00
|
|
|
return 0;
|
2010-05-11 19:42:16 +00:00
|
|
|
}
|