template <class _Up>
auto operator()(_Tp, _Up&& __t) const
+#ifndef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
-> decltype(__test({ _VSTD::forward<_Up>(__t) }));
+#else
+ -> __identity<_Tp>;
+#endif
};
template <class _Base, class _Tp>
}
{
using V = std::variant<std::string, float>;
- static_assert(!std::is_assignable<V, int>::value, "no matching operator=");
+ static_assert(std::is_assignable<V, int>::value == VariantAllowsNarrowingConversions,
+ "no matching operator=");
}
{
using V = std::variant<std::unique_ptr<int>, bool>;
assert(v.index() == 1);
assert(std::get<1>(v) == 43);
}
+#ifndef TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
{
std::variant<unsigned, long> v;
v = 42;
assert(v.index() == 0);
assert(std::get<0>(v) == 43);
}
+#endif
{
std::variant<std::string, bool> v = true;
v = "bar";
+++ /dev/null
-// -*- C++ -*-
-//===----------------------------------------------------------------------===//
-//
-// 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
-
-// <variant>
-
-// template <class ...Types> class variant;
-
-// template <class T>
-// variant& operator=(T&&) noexcept(see below);
-
-#include <variant>
-#include <string>
-#include <memory>
-
-int main(int, char**)
-{
- std::variant<int, int> v1;
- std::variant<long, long long> v2;
- std::variant<char> v3;
- v1 = 1; // expected-error {{no viable overloaded '='}}
- v2 = 1; // expected-error {{no viable overloaded '='}}
- v3 = 1; // expected-error {{no viable overloaded '='}}
-
- std::variant<std::string, float> v4;
- std::variant<std::string, double> v5;
- std::variant<std::string, bool> v6;
- v4 = 1; // expected-error {{no viable overloaded '='}}
- v5 = 1; // expected-error {{no viable overloaded '='}}
- v6 = 1; // expected-error {{no viable overloaded '='}}
-
- std::variant<int, bool> v7;
- std::variant<int, bool const> v8;
- std::variant<int, bool volatile> v9;
- v7 = "meow"; // expected-error {{no viable overloaded '='}}
- v8 = "meow"; // expected-error {{no viable overloaded '='}}
- v9 = "meow"; // expected-error {{no viable overloaded '='}}
-
- std::variant<bool> v10;
- std::variant<bool> v11;
- std::variant<bool> v12;
- v10 = std::true_type(); // expected-error {{no viable overloaded '='}}
- v11 = std::unique_ptr<char>(); // expected-error {{no viable overloaded '='}}
- v12 = nullptr; // expected-error {{no viable overloaded '='}}
-}
--- /dev/null
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// <variant>
+
+// template <class ...Types> class variant;
+
+// template <class T>
+// variant& operator=(T&&) noexcept(see below);
+
+#include <variant>
+#include <string>
+#include <memory>
+
+#include "variant_test_helpers.hpp"
+
+int main(int, char**)
+{
+ static_assert(!std::is_assignable<std::variant<int, int>, int>::value, "");
+ static_assert(!std::is_assignable<std::variant<long, long long>, int>::value, "");
+ static_assert(std::is_assignable<std::variant<char>, int>::value == VariantAllowsNarrowingConversions, "");
+
+ static_assert(std::is_assignable<std::variant<std::string, float>, int>::value == VariantAllowsNarrowingConversions, "");
+ static_assert(std::is_assignable<std::variant<std::string, double>, int>::value == VariantAllowsNarrowingConversions, "");
+ static_assert(!std::is_assignable<std::variant<std::string, bool>, int>::value, "");
+
+ static_assert(!std::is_assignable<std::variant<int, bool>, decltype("meow")>::value, "");
+ static_assert(!std::is_assignable<std::variant<int, const bool>, decltype("meow")>::value, "");
+ static_assert(!std::is_assignable<std::variant<int, const volatile bool>, decltype("meow")>::value, "");
+
+ static_assert(!std::is_assignable<std::variant<bool>, std::true_type>::value, "");
+ static_assert(!std::is_assignable<std::variant<bool>, std::unique_ptr<char> >::value, "");
+ static_assert(!std::is_assignable<std::variant<bool>, decltype(nullptr)>::value, "");
+
+}
}
{
using V = std::variant<std::string, float>;
- static_assert(!std::is_constructible<V, int>::value,
+ static_assert(std::is_constructible<V, int>::value == VariantAllowsNarrowingConversions,
"no matching constructor");
}
{
static_assert(v.index() == 1, "");
static_assert(std::get<1>(v) == 42, "");
}
+#ifndef TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
{
constexpr std::variant<unsigned, long> v(42);
static_assert(v.index() == 1, "");
static_assert(std::get<1>(v) == 42, "");
}
+#endif
{
std::variant<std::string, bool const> v = "foo";
assert(v.index() == 0);
+++ /dev/null
-// -*- C++ -*-
-//===----------------------------------------------------------------------===//
-//
-// 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
-
-// <variant>
-
-// template <class ...Types> class variant;
-
-// template <class T> constexpr variant(T&&) noexcept(see below);
-
-#include <variant>
-#include <string>
-#include <memory>
-
-int main(int, char**)
-{
- std::variant<int, int> v1 = 1; // expected-error {{no viable conversion}}
- std::variant<long, long long> v2 = 1; // expected-error {{no viable conversion}}
- std::variant<char> v3 = 1; // expected-error {{no viable conversion}}
-
- std::variant<std::string, float> v4 = 1; // expected-error {{no viable conversion}}
- std::variant<std::string, double> v5 = 1; // expected-error {{no viable conversion}}
- std::variant<std::string, bool> v6 = 1; // expected-error {{no viable conversion}}
-
- std::variant<int, bool> v7 = "meow"; // expected-error {{no viable conversion}}
- std::variant<int, bool const> v8 = "meow"; // expected-error {{no viable conversion}}
- std::variant<int, bool volatile> v9 = "meow"; // expected-error {{no viable conversion}}
-
- std::variant<bool> v10 = std::true_type(); // expected-error {{no viable conversion}}
- std::variant<bool> v11 = std::unique_ptr<char>(); // expected-error {{no viable conversion}}
- std::variant<bool> v12 = nullptr; // expected-error {{no viable conversion}}
-}
--- /dev/null
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// <variant>
+
+// template <class ...Types> class variant;
+
+// template <class T> constexpr variant(T&&) noexcept(see below);
+
+#include <variant>
+#include <string>
+#include <memory>
+
+#include "variant_test_helpers.hpp"
+
+int main(int, char**)
+{
+ static_assert(!std::is_constructible<std::variant<int, int>, int>::value, "");
+ static_assert(!std::is_constructible<std::variant<long, long long>, int>::value, "");
+ static_assert(std::is_constructible<std::variant<char>, int>::value == VariantAllowsNarrowingConversions, "");
+
+ static_assert(std::is_constructible<std::variant<std::string, float>, int>::value == VariantAllowsNarrowingConversions, "");
+ static_assert(std::is_constructible<std::variant<std::string, double>, int>::value == VariantAllowsNarrowingConversions, "");
+ static_assert(!std::is_constructible<std::variant<std::string, bool>, int>::value, "");
+
+ static_assert(!std::is_constructible<std::variant<int, bool>, decltype("meow")>::value, "");
+ static_assert(!std::is_constructible<std::variant<int, const bool>, decltype("meow")>::value, "");
+ static_assert(!std::is_constructible<std::variant<int, const volatile bool>, decltype("meow")>::value, "");
+
+ static_assert(!std::is_constructible<std::variant<bool>, std::true_type>::value, "");
+ static_assert(!std::is_constructible<std::variant<bool>, std::unique_ptr<char> >::value, "");
+ static_assert(!std::is_constructible<std::variant<bool>, decltype(nullptr)>::value, "");
+
+}
// FIXME: Currently the variant<T&> tests are disabled using this macro.
#define TEST_VARIANT_HAS_NO_REFERENCES
+#ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
+# define TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
+#endif
+
+#ifdef TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
+constexpr bool VariantAllowsNarrowingConversions = true;
+#else
+constexpr bool VariantAllowsNarrowingConversions = false;
+#endif
#ifndef TEST_HAS_NO_EXCEPTIONS
struct CopyThrows {