brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.5 KiB · c276451 Raw
57 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++1410 11// <optional>12//13// template <class T>14//   constexpr optional<decay_t<T>> make_optional(T&& v);15 16#include <cassert>17#include <memory>18#include <optional>19#include <string>20 21#include "test_macros.h"22 23int main(int, char**)24{25    {26        int arr[10];27        auto opt = std::make_optional(arr);28        ASSERT_SAME_TYPE(decltype(opt), std::optional<int*>);29        assert(*opt == arr);30    }31    {32        constexpr auto opt = std::make_optional(2);33        ASSERT_SAME_TYPE(decltype(opt), const std::optional<int>);34        static_assert(opt.value() == 2);35    }36    {37        auto opt = std::make_optional(2);38        ASSERT_SAME_TYPE(decltype(opt), std::optional<int>);39        assert(*opt == 2);40    }41    {42        const std::string s = "123";43        auto opt = std::make_optional(s);44        ASSERT_SAME_TYPE(decltype(opt), std::optional<std::string>);45        assert(*opt == "123");46    }47    {48        std::unique_ptr<int> s = std::make_unique<int>(3);49        auto opt = std::make_optional(std::move(s));50        ASSERT_SAME_TYPE(decltype(opt), std::optional<std::unique_ptr<int>>);51        assert(**opt == 3);52        assert(s == nullptr);53    }54 55  return 0;56}57