1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This is a "No Compile Test" suite.
6 // http://dev.chromium.org/developers/testing/no-compile-tests
10 #include "base/optional.h"
14 #if defined(NCTEST_EXPLICIT_CONVERTING_COPY_CONSTRUCTOR) // [r"fatal error: no matching function for call to object of type"]
16 // Optional<T>(const Optional<U>& arg) constructor is marked explicit if
17 // T is not convertible from "const U&".
20 // Declares as explicit so that Test is still constructible from int,
21 // but not convertible.
22 explicit Test(int a) {}
25 static_assert(!std::is_convertible<const int&, Test>::value,
26 "const int& to Test is convertible");
27 const Optional<int> arg(in_place, 1);
28 ([](Optional<Test> param) {})(arg);
31 #elif defined(NCTEST_EXPLICIT_CONVERTING_MOVE_CONSTRUCTOR) // [r"fatal error: no matching function for call to object of type"]
33 // Optional<T>(Optional<U>&& arg) constructor is marked explicit if
34 // T is not convertible from "U&&".
37 // Declares as explicit so that Test is still constructible from int,
38 // but not convertible.
39 explicit Test(int a) {}
42 static_assert(!std::is_convertible<int&&, Test>::value,
43 "int&& to Test is convertible");
44 ([](Optional<Test> param) {})(Optional<int>(in_place, 1));
47 #elif defined(NCTEST_EXPLICIT_VALUE_FORWARD_CONSTRUCTOR) // [r"fatal error: no matching function for call to object of type"]
49 // Optional<T>(U&&) constructor is marked explicit if T is not convertible
53 // Declares as explicit so that Test is still constructible from int,
54 // but not convertible.
55 explicit Test(int a) {}
58 static_assert(!std::is_convertible<int&&, Test>::value,
59 "int&& to Test is convertible");
60 ([](Optional<Test> param) {})(1);
63 #elif defined(NCTEST_ILL_FORMED_IN_PLACET_T) // [r"instantiation of base::Optional with in_place_t is ill-formed"]
65 // Optional<T> is ill-formed if T is `in_place_t`.
67 Optional<base::in_place_t> optional;
71 #elif defined(NCTEST_ILL_FORMED_CONST_IN_PLACET_T) // [r"instantiation of base::Optional with in_place_t is ill-formed"]
73 // Optional<T> is ill-formed if T is `const in_place_t`.
75 Optional<const base::in_place_t> optional;
79 #elif defined(NCTEST_ILL_FORMED_NULLOPT_T) // [r"instantiation of base::Optional with nullopt_t is ill-formed"]
81 // Optional<T> is ill-formed if T is `const nullopt_t`.
83 Optional<const base::nullopt_t> optional;
87 #elif defined(NCTEST_ILL_FORMED_CONST_NULLOPT_T) // [r"instantiation of base::Optional with nullopt_t is ill-formed"]
89 // Optional<T> is ill-formed if T is `const nullopt_t`.
91 Optional<const base::nullopt_t> optional;
95 #elif defined(NCTEST_ILL_FORMED_NON_DESTRUCTIBLE) // [r"instantiation of base::Optional with a non-destructible type is ill-formed"]
97 // Optional<T> is ill-formed if T is non-destructible.
104 static_assert(!std::is_destructible<T>::value, "T is not destructible");
106 Optional<T> optional;
107 optional.has_value();
110 // TODO(crbug.com/967722): the error message should be about the instantiation of an
111 // ill-formed base::Optional.
112 #elif defined(NCTEST_ILL_FORMED_REFERENCE) // [r"fatal error: union member 'value_' has reference type 'int &'"]
114 // Optional<T> is ill-formed if T is a reference.
118 static_assert(std::is_reference<T>::value, "T is a reference");
120 Optional<T> optional;
121 optional.has_value();
124 // TODO(crbug.com/967722): the error message should be about the instantiation of an
125 // ill-formed base::Optional.
126 #elif defined(NCTEST_ILL_FORMED_CONST_REFERENCE) // [r"fatal error: union member 'value_' has reference type 'const int &'"]
128 // Optional<T> is ill-formed if T is a const reference.
130 using T = const int&;
132 static_assert(std::is_reference<T>::value, "T is a reference");
134 Optional<T> optional;
135 optional.has_value();
138 #elif defined(NCTEST_ILL_FORMED_FIXED_LENGTH_ARRAY) // [r"instantiation of base::Optional with an array type is ill-formed"]
140 // Optional<T> is ill-formed if T is a fixed length array.
144 static_assert(std::is_array<T>::value, "T is an array");
146 Optional<T> optional;
147 optional.has_value();
150 // TODO(crbug.com/967722): the error message should be about the instantiation of an
151 // ill-formed base::Optional.
152 #elif defined(NCTEST_ILL_FORMED_UNDEFINED_LENGTH_ARRAY) // [r"fatal error: base class 'OptionalStorageBase' has a flexible array member"]
154 // Optional<T> is ill-formed if T is a undefined length array.
158 static_assert(std::is_array<T>::value, "T is an array");
160 Optional<T> optional;
161 optional.has_value();