summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Fiselier <eric@efcs.ca>2019-03-11 22:55:21 +0000
committerEric Fiselier <eric@efcs.ca>2019-03-11 22:55:21 +0000
commit86af6f5088b16c98e7033a6a5cf3c889c5d95e57 (patch)
tree5958b1c9af6bb3ca7b6bbe834a7a126b15ca9fda
parentf92e59cbba59793b301f0874fcd9046872c5e4c3 (diff)
downloadbcm5719-llvm-86af6f5088b16c98e7033a6a5cf3c889c5d95e57.tar.gz
bcm5719-llvm-86af6f5088b16c98e7033a6a5cf3c889c5d95e57.zip
Allow optional to tolerate being used with a nested class.
When Clang tries to complete a type containing `std::optional` it considers the `in_place_t` constructor with no arguments which checks if the value type is default constructible. If the value type is a nested class type, then this check occurs too early and poisons the is_default_constructible trait. This patch makes optional deduce `in_place_t` so we can prevent this early SFINAE evaluation. Technically this could break people doing weird things with the in_place_t tag, but that seems less important than making the nested class case work. llvm-svn: 355877
-rw-r--r--libcxx/include/optional10
-rw-r--r--libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/empty_in_place_t_does_not_clobber.pass.cpp41
2 files changed, 48 insertions, 3 deletions
diff --git a/libcxx/include/optional b/libcxx/include/optional
index be584b3cee1..7c136aec3a2 100644
--- a/libcxx/include/optional
+++ b/libcxx/include/optional
@@ -687,11 +687,15 @@ public:
_LIBCPP_INLINE_VISIBILITY constexpr optional(optional&&) = default;
_LIBCPP_INLINE_VISIBILITY constexpr optional(nullopt_t) noexcept {}
- template <class... _Args, class = enable_if_t<
- is_constructible_v<value_type, _Args...>>
+ template <class _InPlaceT, class... _Args, class = enable_if_t<
+ __lazy_and<
+ is_same<_InPlaceT, in_place_t>,
+ is_constructible<value_type, _Args...>
+ >::value
+ >
>
_LIBCPP_INLINE_VISIBILITY
- constexpr explicit optional(in_place_t, _Args&&... __args)
+ constexpr explicit optional(_InPlaceT, _Args&&... __args)
: __base(in_place, _VSTD::forward<_Args>(__args)...) {}
template <class _Up, class... _Args, class = enable_if_t<
diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/empty_in_place_t_does_not_clobber.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/empty_in_place_t_does_not_clobber.pass.cpp
new file mode 100644
index 00000000000..84db7ea024f
--- /dev/null
+++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/empty_in_place_t_does_not_clobber.pass.cpp
@@ -0,0 +1,41 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++98, c++03, c++11, c++14
+// <optional>
+
+// constexpr optional(in_place_t);
+
+// Test that the SFINAE "is_constructible<value_type>" isn't evaluated by the
+// in_place_t constructor with no arguments when the Clang is trying to check
+// copy constructor.
+
+#include <optional>
+#include <type_traits>
+#include <cassert>
+
+#include "test_macros.h"
+#include "archetypes.hpp"
+
+using std::optional;
+
+struct Wrapped {
+ struct Inner {
+ bool Dummy = true;
+ };
+ std::optional<Inner> inner;
+};
+
+int main(int, char**) {
+ static_assert(std::is_default_constructible<Wrapped::Inner>::value, "");
+ Wrapped w;
+ w.inner.emplace();
+ assert(w.inner.has_value());
+
+ return 0;
+}
OpenPOWER on IntegriCloud