xref: /aosp_15_r20/external/libchrome/base/optional_unittest.nc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker// Copyright 2018 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker// Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker// found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker
5*635a8641SAndroid Build Coastguard Worker// This is a "No Compile Test" suite.
6*635a8641SAndroid Build Coastguard Worker// http://dev.chromium.org/developers/testing/no-compile-tests
7*635a8641SAndroid Build Coastguard Worker
8*635a8641SAndroid Build Coastguard Worker#include <type_traits>
9*635a8641SAndroid Build Coastguard Worker
10*635a8641SAndroid Build Coastguard Worker#include "base/optional.h"
11*635a8641SAndroid Build Coastguard Worker
12*635a8641SAndroid Build Coastguard Workernamespace base {
13*635a8641SAndroid Build Coastguard Worker
14*635a8641SAndroid Build Coastguard Worker#if defined(NCTEST_EXPLICIT_CONVERTING_COPY_CONSTRUCTOR)  // [r"fatal error: no matching function for call to object of type"]
15*635a8641SAndroid Build Coastguard Worker
16*635a8641SAndroid Build Coastguard Worker// Optional<T>(const Optional<U>& arg) constructor is marked explicit if
17*635a8641SAndroid Build Coastguard Worker// T is not convertible from "const U&".
18*635a8641SAndroid Build Coastguard Workervoid WontCompile() {
19*635a8641SAndroid Build Coastguard Worker  struct Test {
20*635a8641SAndroid Build Coastguard Worker    // Declares as explicit so that Test is still constructible from int,
21*635a8641SAndroid Build Coastguard Worker    // but not convertible.
22*635a8641SAndroid Build Coastguard Worker    explicit Test(int a) {}
23*635a8641SAndroid Build Coastguard Worker  };
24*635a8641SAndroid Build Coastguard Worker
25*635a8641SAndroid Build Coastguard Worker  static_assert(!std::is_convertible<const int&, Test>::value,
26*635a8641SAndroid Build Coastguard Worker                "const int& to Test is convertible");
27*635a8641SAndroid Build Coastguard Worker  const Optional<int> arg(in_place, 1);
28*635a8641SAndroid Build Coastguard Worker  ([](Optional<Test> param) {})(arg);
29*635a8641SAndroid Build Coastguard Worker}
30*635a8641SAndroid Build Coastguard Worker
31*635a8641SAndroid Build Coastguard Worker#elif defined(NCTEST_EXPLICIT_CONVERTING_MOVE_CONSTRUCTOR)  // [r"fatal error: no matching function for call to object of type"]
32*635a8641SAndroid Build Coastguard Worker
33*635a8641SAndroid Build Coastguard Worker// Optional<T>(Optional<U>&& arg) constructor is marked explicit if
34*635a8641SAndroid Build Coastguard Worker// T is not convertible from "U&&".
35*635a8641SAndroid Build Coastguard Workervoid WontCompile() {
36*635a8641SAndroid Build Coastguard Worker  struct Test {
37*635a8641SAndroid Build Coastguard Worker    // Declares as explicit so that Test is still constructible from int,
38*635a8641SAndroid Build Coastguard Worker    // but not convertible.
39*635a8641SAndroid Build Coastguard Worker    explicit Test(int a) {}
40*635a8641SAndroid Build Coastguard Worker  };
41*635a8641SAndroid Build Coastguard Worker
42*635a8641SAndroid Build Coastguard Worker  static_assert(!std::is_convertible<int&&, Test>::value,
43*635a8641SAndroid Build Coastguard Worker                "int&& to Test is convertible");
44*635a8641SAndroid Build Coastguard Worker  ([](Optional<Test> param) {})(Optional<int>(in_place, 1));
45*635a8641SAndroid Build Coastguard Worker}
46*635a8641SAndroid Build Coastguard Worker
47*635a8641SAndroid Build Coastguard Worker#elif defined(NCTEST_EXPLICIT_VALUE_FORWARD_CONSTRUCTOR)  // [r"fatal error: no matching function for call to object of type"]
48*635a8641SAndroid Build Coastguard Worker
49*635a8641SAndroid Build Coastguard Worker// Optional<T>(U&&) constructor is marked explicit if T is not convertible
50*635a8641SAndroid Build Coastguard Worker// from U&&.
51*635a8641SAndroid Build Coastguard Workervoid WontCompile() {
52*635a8641SAndroid Build Coastguard Worker  struct Test {
53*635a8641SAndroid Build Coastguard Worker    // Declares as explicit so that Test is still constructible from int,
54*635a8641SAndroid Build Coastguard Worker    // but not convertible.
55*635a8641SAndroid Build Coastguard Worker    explicit Test(int a) {}
56*635a8641SAndroid Build Coastguard Worker  };
57*635a8641SAndroid Build Coastguard Worker
58*635a8641SAndroid Build Coastguard Worker  static_assert(!std::is_convertible<int&&, Test>::value,
59*635a8641SAndroid Build Coastguard Worker                "int&& to Test is convertible");
60*635a8641SAndroid Build Coastguard Worker  ([](Optional<Test> param) {})(1);
61*635a8641SAndroid Build Coastguard Worker}
62*635a8641SAndroid Build Coastguard Worker
63*635a8641SAndroid Build Coastguard Worker#endif
64*635a8641SAndroid Build Coastguard Worker
65*635a8641SAndroid Build Coastguard Worker}  // namespace base
66