Skip to content

Commit

Permalink
[pair] Mark constructors as conditionally noexcept
Browse files Browse the repository at this point in the history
Summary:
std::tuple marks its constructors as noexcept when the corresponding
memberwise constructors are noexcept too -- this commit improves std::pair
so that it behaves the same.

This is a re-application of r348824, which broke the build in C++03 mode
because a test was marked as supported in C++03 when it shouldn't be.

Note:
I did not add support in the explicit and non-explicit `pair(_Tuple&& __p)`
constructors because those are non-standard extensions, and supporting them
properly is tedious (we have to copy the rvalue-referenceness of the deduced
_Tuple&& onto the result of tuple_element).

<rdar://problem/29537079>

Reviewers: mclow.lists, EricWF

Subscribers: christof, llvm-commits

Differential Revision: https://reviews.llvm.org/D48669

llvm-svn: 348847
  • Loading branch information
ldionne committed Dec 11, 2018
1 parent bca37ac commit 9f77b1a
Show file tree
Hide file tree
Showing 10 changed files with 388 additions and 46 deletions.
22 changes: 21 additions & 1 deletion libcxx/include/utility
Expand Up @@ -409,62 +409,80 @@ struct _LIBCPP_TEMPLATE_VIS pair
_CheckArgsDep<_Dummy>::template __enable_default<_T1, _T2>()
> = false>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
pair() : first(), second() {}
pair() _NOEXCEPT_(is_nothrow_default_constructible<first_type>::value &&
is_nothrow_default_constructible<second_type>::value)
: first(), second() {}

template <bool _Dummy = true, _EnableB<
_CheckArgsDep<_Dummy>::template __enable_explicit<_T1 const&, _T2 const&>()
> = false>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
explicit pair(_T1 const& __t1, _T2 const& __t2)
_NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value &&
is_nothrow_copy_constructible<second_type>::value)
: first(__t1), second(__t2) {}

template<bool _Dummy = true, _EnableB<
_CheckArgsDep<_Dummy>::template __enable_implicit<_T1 const&, _T2 const&>()
> = false>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
pair(_T1 const& __t1, _T2 const& __t2)
_NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value &&
is_nothrow_copy_constructible<second_type>::value)
: first(__t1), second(__t2) {}

template<class _U1, class _U2, _EnableB<
_CheckArgs::template __enable_explicit<_U1, _U2>()
> = false>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
explicit pair(_U1&& __u1, _U2&& __u2)
_NOEXCEPT_((is_nothrow_constructible<first_type, _U1>::value &&
is_nothrow_constructible<second_type, _U2>::value))
: first(_VSTD::forward<_U1>(__u1)), second(_VSTD::forward<_U2>(__u2)) {}

template<class _U1, class _U2, _EnableB<
_CheckArgs::template __enable_implicit<_U1, _U2>()
> = false>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
pair(_U1&& __u1, _U2&& __u2)
_NOEXCEPT_((is_nothrow_constructible<first_type, _U1>::value &&
is_nothrow_constructible<second_type, _U2>::value))
: first(_VSTD::forward<_U1>(__u1)), second(_VSTD::forward<_U2>(__u2)) {}

template<class _U1, class _U2, _EnableB<
_CheckArgs::template __enable_explicit<_U1 const&, _U2 const&>()
> = false>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
explicit pair(pair<_U1, _U2> const& __p)
_NOEXCEPT_((is_nothrow_constructible<first_type, _U1 const&>::value &&
is_nothrow_constructible<second_type, _U2 const&>::value))
: first(__p.first), second(__p.second) {}

template<class _U1, class _U2, _EnableB<
_CheckArgs::template __enable_implicit<_U1 const&, _U2 const&>()
> = false>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
pair(pair<_U1, _U2> const& __p)
_NOEXCEPT_((is_nothrow_constructible<first_type, _U1 const&>::value &&
is_nothrow_constructible<second_type, _U2 const&>::value))
: first(__p.first), second(__p.second) {}

template<class _U1, class _U2, _EnableB<
_CheckArgs::template __enable_explicit<_U1, _U2>()
> = false>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
explicit pair(pair<_U1, _U2>&&__p)
_NOEXCEPT_((is_nothrow_constructible<first_type, _U1&&>::value &&
is_nothrow_constructible<second_type, _U2&&>::value))
: first(_VSTD::forward<_U1>(__p.first)), second(_VSTD::forward<_U2>(__p.second)) {}

template<class _U1, class _U2, _EnableB<
_CheckArgs::template __enable_implicit<_U1, _U2>()
> = false>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
pair(pair<_U1, _U2>&& __p)
_NOEXCEPT_((is_nothrow_constructible<first_type, _U1&&>::value &&
is_nothrow_constructible<second_type, _U2&&>::value))
: first(_VSTD::forward<_U1>(__p.first)), second(_VSTD::forward<_U2>(__p.second)) {}

template<class _Tuple, _EnableB<
Expand All @@ -487,6 +505,8 @@ struct _LIBCPP_TEMPLATE_VIS pair
_LIBCPP_INLINE_VISIBILITY
pair(piecewise_construct_t __pc,
tuple<_Args1...> __first_args, tuple<_Args2...> __second_args)
_NOEXCEPT_((is_nothrow_constructible<first_type, _Args1...>::value &&
is_nothrow_constructible<second_type, _Args2...>::value))
: pair(__pc, __first_args, __second_args,
typename __make_tuple_indices<sizeof...(_Args1)>::type(),
typename __make_tuple_indices<sizeof...(_Args2) >::type()) {}
Expand Down
54 changes: 54 additions & 0 deletions libcxx/test/libcxx/utilities/utility/pairs/pairs.pair/U_V.pass.cpp
@@ -0,0 +1,54 @@
//===----------------------------------------------------------------------===//
//
// 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.
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++98, c++03

// <utility>

// template <class T1, class T2> struct pair

// template<class U, class V> pair(U&& x, V&& y);

#include <utility>


struct ExplicitT {
constexpr explicit ExplicitT(int x) : value(x) {}
int value;
};

struct ImplicitT {
constexpr ImplicitT(int x) : value(x) {}
int value;
};

struct ExplicitNothrowT {
explicit ExplicitNothrowT(int x) noexcept : value(x) {}
int value;
};

struct ImplicitNothrowT {
ImplicitNothrowT(int x) noexcept : value(x) {}
int value;
};

int main() {
{ // explicit noexcept test
static_assert(!std::is_nothrow_constructible<std::pair<ExplicitT, ExplicitT>, int, int>::value, "");
static_assert(!std::is_nothrow_constructible<std::pair<ExplicitNothrowT, ExplicitT>, int, int>::value, "");
static_assert(!std::is_nothrow_constructible<std::pair<ExplicitT, ExplicitNothrowT>, int, int>::value, "");
static_assert( std::is_nothrow_constructible<std::pair<ExplicitNothrowT, ExplicitNothrowT>, int, int>::value, "");
}
{ // implicit noexcept test
static_assert(!std::is_nothrow_constructible<std::pair<ImplicitT, ImplicitT>, int, int>::value, "");
static_assert(!std::is_nothrow_constructible<std::pair<ImplicitNothrowT, ImplicitT>, int, int>::value, "");
static_assert(!std::is_nothrow_constructible<std::pair<ImplicitT, ImplicitNothrowT>, int, int>::value, "");
static_assert( std::is_nothrow_constructible<std::pair<ImplicitNothrowT, ImplicitNothrowT>, int, int>::value, "");
}
}
@@ -0,0 +1,62 @@
//===----------------------------------------------------------------------===//
//
// 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.
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++98, c++03

// <utility>

// template <class T1, class T2> struct pair

// pair(const T1& x, const T2& y);

#include <utility>


struct ExplicitT {
constexpr explicit ExplicitT(int x) : value(x) {}
constexpr explicit ExplicitT(ExplicitT const& o) : value(o.value) {}
int value;
};

struct ImplicitT {
constexpr ImplicitT(int x) : value(x) {}
constexpr ImplicitT(ImplicitT const& o) : value(o.value) {}
int value;
};

struct ExplicitNothrowT {
explicit ExplicitNothrowT(ExplicitNothrowT const&) noexcept {}
};

struct ImplicitNothrowT {
ImplicitNothrowT(ImplicitNothrowT const&) noexcept {}
};

int main() {
{ // explicit noexcept test
static_assert(!std::is_nothrow_constructible<std::pair<ExplicitT, ExplicitT>,
ExplicitT const&, ExplicitT const&>::value, "");
static_assert(!std::is_nothrow_constructible<std::pair<ExplicitNothrowT, ExplicitT>,
ExplicitNothrowT const&, ExplicitT const&>::value, "");
static_assert(!std::is_nothrow_constructible<std::pair<ExplicitT, ExplicitNothrowT>,
ExplicitT const&, ExplicitNothrowT const&>::value, "");
static_assert( std::is_nothrow_constructible<std::pair<ExplicitNothrowT, ExplicitNothrowT>,
ExplicitNothrowT const&, ExplicitNothrowT const&>::value, "");
}
{ // implicit noexcept test
static_assert(!std::is_nothrow_constructible<std::pair<ImplicitT, ImplicitT>,
ImplicitT const&, ImplicitT const&>::value, "");
static_assert(!std::is_nothrow_constructible<std::pair<ImplicitNothrowT, ImplicitT>,
ImplicitNothrowT const&, ImplicitT const&>::value, "");
static_assert(!std::is_nothrow_constructible<std::pair<ImplicitT, ImplicitNothrowT>,
ImplicitT const&, ImplicitNothrowT const&>::value, "");
static_assert( std::is_nothrow_constructible<std::pair<ImplicitNothrowT, ImplicitNothrowT>,
ImplicitNothrowT const&, ImplicitNothrowT const&>::value, "");
}
}
@@ -0,0 +1,64 @@
//===----------------------------------------------------------------------===//
//
// 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.
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++98, c++03

// <utility>

// template <class T1, class T2> struct pair

// template <class U, class V> EXPLICIT constexpr pair(const pair<U, V>& p);

#include <utility>


struct ExplicitT {
constexpr explicit ExplicitT(int x) : value(x) {}
constexpr explicit ExplicitT(ExplicitT const& o) : value(o.value) {}
int value;
};

struct ImplicitT {
constexpr ImplicitT(int x) : value(x) {}
constexpr ImplicitT(ImplicitT const& o) : value(o.value) {}
int value;
};

struct ExplicitNothrowT {
explicit ExplicitNothrowT(int x) noexcept : value(x) {}
int value;
};

struct ImplicitNothrowT {
ImplicitNothrowT(int x) noexcept : value(x) {}
int value;
};

int main() {
{ // explicit noexcept test
static_assert(!std::is_nothrow_constructible<std::pair<ExplicitT, ExplicitT>,
std::pair<int, int> const&>::value, "");
static_assert(!std::is_nothrow_constructible<std::pair<ExplicitNothrowT, ExplicitT>,
std::pair<int, int> const&>::value, "");
static_assert(!std::is_nothrow_constructible<std::pair<ExplicitT, ExplicitNothrowT>,
std::pair<int, int> const&>::value, "");
static_assert( std::is_nothrow_constructible<std::pair<ExplicitNothrowT, ExplicitNothrowT>,
std::pair<int, int> const&>::value, "");
}
{ // implicit noexcept test
static_assert(!std::is_nothrow_constructible<std::pair<ImplicitT, ImplicitT>,
std::pair<int, int> const&>::value, "");
static_assert(!std::is_nothrow_constructible<std::pair<ImplicitNothrowT, ImplicitT>,
std::pair<int, int> const&>::value, "");
static_assert(!std::is_nothrow_constructible<std::pair<ImplicitT, ImplicitNothrowT>,
std::pair<int, int> const&>::value, "");
static_assert( std::is_nothrow_constructible<std::pair<ImplicitNothrowT, ImplicitNothrowT>,
std::pair<int, int> const&>::value, "");
}
}
@@ -0,0 +1,31 @@
//===----------------------------------------------------------------------===//
//
// 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.
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++98, c++03

// <utility>

// template <class T1, class T2> struct pair

// constexpr pair();

#include <utility>
#include <type_traits>

#include "archetypes.hpp"


int main() {
using NonThrowingDefault = NonThrowingTypes::DefaultOnly;
using ThrowingDefault = NonTrivialTypes::DefaultOnly;
static_assert(!std::is_nothrow_default_constructible<std::pair<ThrowingDefault, ThrowingDefault>>::value, "");
static_assert(!std::is_nothrow_default_constructible<std::pair<NonThrowingDefault, ThrowingDefault>>::value, "");
static_assert(!std::is_nothrow_default_constructible<std::pair<ThrowingDefault, NonThrowingDefault>>::value, "");
static_assert( std::is_nothrow_default_constructible<std::pair<NonThrowingDefault, NonThrowingDefault>>::value, "");
}
@@ -0,0 +1,38 @@
//===----------------------------------------------------------------------===//
//
// 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.
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++98, c++03

// <utility>

// template <class T1, class T2> struct pair

// template <class... Args1, class... Args2>
// pair(piecewise_construct_t, tuple<Args1...> first_args,
// tuple<Args2...> second_args);

#include <tuple>
#include <type_traits>
#include <utility>

#include "archetypes.hpp"


int main() {
using NonThrowingConvert = NonThrowingTypes::ConvertingType;
using ThrowingConvert = NonTrivialTypes::ConvertingType;
static_assert(!std::is_nothrow_constructible<std::pair<ThrowingConvert, ThrowingConvert>,
std::piecewise_construct_t, std::tuple<int, int>, std::tuple<long, long>>::value, "");
static_assert(!std::is_nothrow_constructible<std::pair<NonThrowingConvert, ThrowingConvert>,
std::piecewise_construct_t, std::tuple<int, int>, std::tuple<long, long>>::value, "");
static_assert(!std::is_nothrow_constructible<std::pair<ThrowingConvert, NonThrowingConvert>,
std::piecewise_construct_t, std::tuple<int, int>, std::tuple<long, long>>::value, "");
static_assert( std::is_nothrow_constructible<std::pair<NonThrowingConvert, NonThrowingConvert>,
std::piecewise_construct_t, std::tuple<int, int>, std::tuple<long, long>>::value, "");
}

0 comments on commit 9f77b1a

Please sign in to comment.