pax_global_header00006660000000000000000000000064144043167030014514gustar00rootroot0000000000000052 comment=292eff8bd8ee230a7df1d6a1c00c4ea0eb2f0362 expected-1.1.0/000077500000000000000000000000001440431670300133145ustar00rootroot00000000000000expected-1.1.0/.appveyor.yml000066400000000000000000000003001440431670300157530ustar00rootroot00000000000000os: - Visual Studio 2015 - Visual Studio 2017 - Visual Studio 2019 - Visual Studio 2022 build_script: - cmake -Bbuild -S. - cmake --build build - cmake --build build --target RUN_TESTS expected-1.1.0/.clang-format000066400000000000000000000000231440431670300156620ustar00rootroot00000000000000BasedOnStyle: LLVM expected-1.1.0/.github/000077500000000000000000000000001440431670300146545ustar00rootroot00000000000000expected-1.1.0/.github/workflows/000077500000000000000000000000001440431670300167115ustar00rootroot00000000000000expected-1.1.0/.github/workflows/cmake.yml000066400000000000000000000077361440431670300205310ustar00rootroot00000000000000name: CMake on: push: branches: [ "master" ] pull_request: branches: [ "master" ] jobs: build: runs-on: ubuntu-20.04 strategy: matrix: std: [11, 14] cxx: [g++-4.8, g++-4.9, g++-5, g++-6, g++-7, g++-8, g++-9, g++-10, clang++-3.5, clang++-3.6, clang++-3.7, clang++-3.8, clang++-3.9, clang++-4.0, clang++-5.0, clang++-6.0, clang++-7, clang++-8, clang++-9, clang++-10, clang++-11] exclude: - cxx: g++-4.8 std: 14 - cxx: g++4.9 std: 14 include: - cxx: g++-4.8 install: sudo apt install g++-4.8 - cxx: g++-4.9 install: sudo apt install g++-4.9 - cxx: g++-5 install: sudo apt install g++-5 - cxx: g++-6 install: sudo apt install g++-6 - cxx: g++-7 install: sudo apt install g++-7 - cxx: g++-8 std: 11 install: sudo apt install g++-8 - cxx: g++-8 std: 14 install: sudo apt install g++-8 - cxx: g++-8 std: 17 install: sudo apt install g++-8 - cxx: g++-9 std: 14 - cxx: g++-9 std: 17 - cxx: g++-10 std: 14 - cxx: g++-10 std: 17 - cxx: g++-11 std: 14 install: sudo apt install g++-11 - cxx: g++-11 std: 17 install: sudo apt install g++-11 - cxx: g++-11 std: 20 install: sudo apt install g++-11 - cxx: clang++-3.5 install: sudo apt install clang-3.5 - cxx: clang++-3.6 install: sudo apt install clang-3.6 - cxx: clang++-3.7 install: sudo apt install clang-3.7 - cxx: clang++-3.8 install: sudo apt install clang-3.8 - cxx: clang++-3.9 install: sudo apt install clang-3.9 - cxx: clang++-4.0 install: sudo apt install clang-4.0 - cxx: clang++-5.0 install: sudo apt install clang-5.0 - cxx: clang++-6.0 install: sudo apt install clang-6.0 - cxx: clang++-7 install: sudo apt install clang-7 - cxx: clang++-8 install: sudo apt install clang-8 - cxx: clang++-9 install: sudo apt install clang-9 - cxx: clang++-10 install: sudo apt install clang-10 - cxx: clang++-11 install: sudo apt install clang-11 - cxx: clang++-6.0 std: 17 install: sudo apt install clang-6.0 - cxx: clang++-7 std: 17 install: sudo apt install clang-7 - cxx: clang++-8 std: 17 install: sudo apt install clang-8 - cxx: clang++-9 std: 17 install: sudo apt install clang-9 - cxx: clang++-10 std: 17 install: sudo apt install clang-10 - cxx: clang++-11 std: 17 install: sudo apt install clang-11 steps: - uses: actions/checkout@v3 - name: Setup Toolchain run: | sudo apt-add-repository 'deb http://azure.archive.ubuntu.com/ubuntu/ xenial main' sudo apt-add-repository 'deb http://azure.archive.ubuntu.com/ubuntu/ xenial universe' sudo apt-add-repository 'deb http://azure.archive.ubuntu.com/ubuntu/ bionic main' sudo apt-add-repository 'deb http://azure.archive.ubuntu.com/ubuntu/ bionic universe' ${{matrix.install}} - name: Configure CMake env: CXX: ${{matrix.cxx}} run: cmake -B ${{github.workspace}}/build -DCMAKE_CXX_STANDARD=${{matrix.std}} - name: Build run: cmake --build ${{github.workspace}}/build - name: Test working-directory: ${{github.workspace}}/build run: cmake --build ${{github.workspace}}/build --target test expected-1.1.0/.gitignore000066400000000000000000000000211440431670300152750ustar00rootroot00000000000000\#* .\#* /build/ expected-1.1.0/.travis.yml000066400000000000000000000161241440431670300154310ustar00rootroot00000000000000language: cpp dist: xenial matrix: include: - compiler: gcc addons: apt: sources: - ubuntu-toolchain-r-test packages: - g++-5 env: COMPILER=g++-5 CXXSTD=11 - compiler: gcc addons: apt: sources: - ubuntu-toolchain-r-test packages: - g++-6 env: COMPILER=g++-6 CXXSTD=11 - compiler: gcc addons: apt: sources: - ubuntu-toolchain-r-test packages: - g++-7 env: COMPILER=g++-7 CXXSTD=11 - compiler: gcc addons: apt: sources: - ubuntu-toolchain-r-test packages: - g++-8 env: COMPILER=g++-8 CXXSTD=11 - compiler: gcc addons: apt: sources: - ubuntu-toolchain-r-test packages: - g++-4.9 env: COMPILER=g++-4.9 CXXSTD=11 - compiler: gcc addons: apt: sources: - ubuntu-toolchain-r-test packages: - g++-4.8 env: COMPILER=g++-4.8 CXXSTD=11 - compiler: clang addons: apt: sources: - llvm-toolchain-precise-3.5 - ubuntu-toolchain-r-test packages: - clang++-3.5 - libc++-dev env: COMPILER=clang++-3.5 CXXSTD=11 - compiler: clang addons: apt: sources: - llvm-toolchain-precise-3.6 - ubuntu-toolchain-r-test packages: - clang++-3.6 - libc++-dev env: COMPILER=clang++-3.6 CXXSTD=11 - compiler: clang addons: apt: sources: - llvm-toolchain-precise-3.7 - ubuntu-toolchain-r-test packages: - clang++-3.7 - libc++-dev env: COMPILER=clang++-3.7 CXXSTD=11 - compiler: clang addons: apt: sources: - sourceline: "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-3.8 main" key_url: "http://apt.llvm.org/llvm-snapshot.gpg.key" - ubuntu-toolchain-r-test packages: - clang++-3.8 - libc++-dev env: COMPILER=clang++-3.8 CXXSTD=11 - compiler: clang addons: apt: sources: - sourceline: "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-3.9 main" key_url: "http://apt.llvm.org/llvm-snapshot.gpg.key" - ubuntu-toolchain-r-test packages: - clang++-3.9 - libc++-dev env: COMPILER=clang++-3.9 CXXSTD=11 - compiler: clang addons: apt: sources: - llvm-toolchain-xenial-4.0 - ubuntu-toolchain-r-test packages: - clang++-4.0 - libc++-dev env: COMPILER=clang++-4.0 CXXSTD=11 - compiler: clang addons: apt: sources: - llvm-toolchain-xenial-5.0 - ubuntu-toolchain-r-test packages: - clang++-5.0 - libc++-dev env: COMPILER=clang++-5.0 CXXSTD=11 - compiler: clang addons: apt: sources: - llvm-toolchain-xenial-6.0 - ubuntu-toolchain-r-test packages: - clang++-6.0 - libc++-dev env: COMPILER=clang++-6.0 CXXSTD=11 - compiler: gcc addons: apt: sources: - ubuntu-toolchain-r-test packages: - g++-5 env: COMPILER=g++-5 CXXSTD=14 - compiler: gcc addons: apt: sources: - ubuntu-toolchain-r-test packages: - g++-6 env: COMPILER=g++-6 CXXSTD=14 - compiler: gcc addons: apt: sources: - ubuntu-toolchain-r-test packages: - g++-7 env: COMPILER=g++-7 CXXSTD=14 - compiler: gcc addons: apt: sources: - ubuntu-toolchain-r-test packages: - g++-8 env: COMPILER=g++-8 CXXSTD=14 - compiler: clang addons: apt: sources: - llvm-toolchain-precise-3.5 - ubuntu-toolchain-r-test packages: - clang++-3.5 - libc++-dev env: COMPILER=clang++-3.5 CXXSTD=14 - compiler: clang addons: apt: sources: - llvm-toolchain-precise-3.6 - ubuntu-toolchain-r-test packages: - clang++-3.6 - libc++-dev env: COMPILER=clang++-3.6 CXXSTD=14 - compiler: clang addons: apt: sources: - llvm-toolchain-precise-3.7 - ubuntu-toolchain-r-test packages: - clang++-3.7 - libc++-dev env: COMPILER=clang++-3.7 CXXSTD=14 - compiler: clang addons: apt: sources: - sourceline: "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-3.8 main" key_url: "http://apt.llvm.org/llvm-snapshot.gpg.key" - ubuntu-toolchain-r-test packages: - clang++-3.8 - libc++-dev env: COMPILER=clang++-3.8 CXXSTD=14 - compiler: clang addons: apt: sources: - sourceline: "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-3.9 main" key_url: "http://apt.llvm.org/llvm-snapshot.gpg.key" - ubuntu-toolchain-r-test packages: - clang++-3.9 - libc++-dev env: COMPILER=clang++-3.9 CXXSTD=14 - compiler: clang addons: apt: sources: - llvm-toolchain-xenial-4.0 - ubuntu-toolchain-r-test packages: - clang++-4.0 - libc++-dev env: COMPILER=clang++-4.0 CXXSTD=14 - compiler: clang addons: apt: sources: - llvm-toolchain-xenial-5.0 - ubuntu-toolchain-r-test packages: - clang++-5.0 - libc++-dev env: COMPILER=clang++-5.0 CXXSTD=14 - compiler: clang addons: apt: sources: - llvm-toolchain-xenial-6.0 - ubuntu-toolchain-r-test packages: - clang++-6.0 - libc++-dev env: COMPILER=clang++-6.0 CXXSTD=14 before_install: - sudo apt update - sudo apt install -y apt-transport-https ca-certificates gnupg software-properties-common - curl -L https://apt.kitware.com/keys/kitware-archive-latest.asc | sudo apt-key add - - sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ xenial main' - sudo apt update install: - if [ "$CXX" = "clang++" ]; then export CXX="$COMPILER -stdlib=libc++"; fi - if [ "$CXX" = "g++" ]; then export CXX="$COMPILER"; fi - sudo apt install -y cmake script: - /usr/bin/cmake -B build -S . "-DCMAKE_CXX_STANDARD=$CXXSTD" - /usr/bin/cmake --build build - /usr/bin/cmake --build build --target test expected-1.1.0/CMakeLists.txt000066400000000000000000000063111440431670300160550ustar00rootroot00000000000000cmake_minimum_required(VERSION 3.14) project(tl-expected HOMEPAGE_URL https://tl.tartanllama.xyz DESCRIPTION "C++11/14/17 std::expected with functional-style extensions" VERSION 1.0.0 LANGUAGES CXX) include(CMakePackageConfigHelpers) include(CMakeDependentOption) include(GNUInstallDirs) include(FetchContent) include(CTest) if (NOT DEFINED CMAKE_CXX_STANDARD) set(CMAKE_CXX_STANDARD 14) endif() option(EXPECTED_BUILD_PACKAGE "Build package files as well" ON) cmake_dependent_option(EXPECTED_BUILD_TESTS "Enable tl::expected tests" ON "BUILD_TESTING" OFF) cmake_dependent_option(EXPECTED_BUILD_PACKAGE_DEB "Create a DEB" ON "EXPECTED_BUILD_PACKAGE" OFF) add_library(expected INTERFACE) target_include_directories(expected INTERFACE $ $) if (NOT CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) add_library(tl::expected ALIAS expected) endif() # Installation help configure_package_config_file( "${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}-config.cmake.in" "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake" INSTALL_DESTINATION "share/cmake/${PROJECT_NAME}") write_basic_package_version_file( "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake" COMPATIBILITY SameMajorVersion ARCH_INDEPENDENT) install(TARGETS expected EXPORT ${PROJECT_NAME}-targets INCLUDES DESTINATION "${CMAKE_INSTALL_DATADIR}") install(EXPORT ${PROJECT_NAME}-targets DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}" NAMESPACE tl:: FILE "${PROJECT_NAME}-targets.cmake") install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake" "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake" DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}") install(DIRECTORY "include/" TYPE INCLUDE) if(EXPECTED_BUILD_TESTS) set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) set(CATCH_INSTALL_HELPERS OFF) set(CATCH_BUILD_TESTING OFF) set(CATCH_INSTALL_DOCS OFF) FetchContent_Declare(Catch2 URL https://github.com/catchorg/Catch2/archive/v2.13.10.zip) FetchContent_MakeAvailable(Catch2) file(GLOB test-sources CONFIGURE_DEPENDS tests/*.cpp) list(FILTER test-sources EXCLUDE REGEX "tests/test.cpp") add_executable(${PROJECT_NAME}-tests "${test-sources}") target_compile_options(${PROJECT_NAME}-tests PRIVATE $<$>:-Wall -Wextra>) target_link_libraries(${PROJECT_NAME}-tests PRIVATE Catch2::Catch2 expected) add_test(NAME tl::expected::tests COMMAND ${PROJECT_NAME}-tests) endif() if (NOT EXPECTED_BUILD_PACKAGE) return() endif() list(APPEND source-generators TBZ2 TGZ TXZ ZIP) if (CMAKE_HOST_WIN32) list(APPEND binary-generators "WIX") endif() if (EXPECTED_BUILD_PACKAGE_DEB) list(APPEND binary-generators "DEB") endif() if (EXPECTED_BUILD_RPM) list(APPEND binary-generators "RPM") endif() set(CPACK_SOURCE_GENERATOR ${source-generators}) set(CPACK_GENERATOR ${binary-generators}) set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${PROJECT_VERSION}") set(CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}") set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Sy Brand") list(APPEND CPACK_SOURCE_IGNORE_FILES /.git/ /build/ .gitignore .DS_Store) include(CPack) expected-1.1.0/COPYING000066400000000000000000000156101440431670300143520ustar00rootroot00000000000000Creative Commons Legal Code CC0 1.0 Universal CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER. Statement of Purpose The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent owner(s) (each and all, an "owner") of an original work of authorship and/or a database (each, a "Work"). Certain owners wish to permanently relinquish those rights to a Work for the purpose of contributing to a commons of creative, cultural and scientific works ("Commons") that the public can reliably and without fear of later claims of infringement build upon, modify, incorporate in other works, reuse and redistribute as freely as possible in any form whatsoever and for any purposes, including without limitation commercial purposes. These owners may contribute to the Commons to promote the ideal of a free culture and the further production of creative, cultural and scientific works, or to gain reputation or greater distribution for their Work in part through the use and efforts of others. For these and/or other purposes and motivations, and without any expectation of additional consideration or compensation, the person associating CC0 with a Work (the "Affirmer"), to the extent that he or she is an owner of Copyright and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and publicly distribute the Work under its terms, with knowledge of his or her Copyright and Related Rights in the Work and the meaning and intended legal effect of CC0 on those rights. 1. Copyright and Related Rights. A Work made available under CC0 may be protected by copyright and related or neighboring rights ("Copyright and Related Rights"). Copyright and Related Rights include, but are not limited to, the following: i. the right to reproduce, adapt, distribute, perform, display, communicate, and translate a Work; ii. moral rights retained by the original author(s) and/or performer(s); iii. publicity and privacy rights pertaining to a person's image or likeness depicted in a Work; iv. rights protecting against unfair competition in regards to a Work, subject to the limitations in paragraph 4(a), below; v. rights protecting the extraction, dissemination, use and reuse of data in a Work; vi. database rights (such as those arising under Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, and under any national implementation thereof, including any amended or successor version of such directive); and vii. other similar, equivalent or corresponding rights throughout the world based on applicable law or treaty, and any national implementations thereof. 2. Waiver. To the greatest extent permitted by, but not in contravention of, applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and unconditionally waives, abandons, and surrenders all of Affirmer's Copyright and Related Rights and associated claims and causes of action, whether now known or unknown (including existing as well as future claims and causes of action), in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each member of the public at large and to the detriment of Affirmer's heirs and successors, fully intending that such Waiver shall not be subject to revocation, rescission, cancellation, termination, or any other legal or equitable action to disrupt the quiet enjoyment of the Work by the public as contemplated by Affirmer's express Statement of Purpose. 3. Public License Fallback. Should any part of the Waiver for any reason be judged legally invalid or ineffective under applicable law, then the Waiver shall be preserved to the maximum extent permitted taking into account Affirmer's express Statement of Purpose. In addition, to the extent the Waiver is so judged Affirmer hereby grants to each affected person a royalty-free, non transferable, non sublicensable, non exclusive, irrevocable and unconditional license to exercise Affirmer's Copyright and Related Rights in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "License"). The License shall be deemed effective as of the date CC0 was applied by Affirmer to the Work. Should any part of the License for any reason be judged legally invalid or ineffective under applicable law, such partial invalidity or ineffectiveness shall not invalidate the remainder of the License, and in such case Affirmer hereby affirms that he or she will not (i) exercise any of his or her remaining Copyright and Related Rights in the Work or (ii) assert any associated claims and causes of action with respect to the Work, in either case contrary to Affirmer's express Statement of Purpose. 4. Limitations and Disclaimers. a. No trademark or patent rights held by Affirmer are waived, abandoned, surrendered, licensed or otherwise affected by this document. b. Affirmer offers the Work as-is and makes no representations or warranties of any kind concerning the Work, express, implied, statutory or otherwise, including without limitation warranties of title, merchantability, fitness for a particular purpose, non infringement, or the absence of latent or other defects, accuracy, or the present or absence of errors, whether or not discoverable, all to the greatest extent permissible under applicable law. c. Affirmer disclaims responsibility for clearing rights of other persons that may apply to the Work or any use thereof, including without limitation any person's Copyright and Related Rights in the Work. Further, Affirmer disclaims responsibility for obtaining any necessary consents, permissions or other rights required for any use of the Work. d. Affirmer understands and acknowledges that Creative Commons is not a party to this document and has no duty or obligation with respect to this CC0 or use of the Work. expected-1.1.0/README.md000066400000000000000000000075101440431670300145760ustar00rootroot00000000000000# expected Single header implementation of `std::expected` with functional-style extensions. [![Documentation Status](https://readthedocs.org/projects/tl-docs/badge/?version=latest)](https://tl.tartanllama.xyz/en/latest/?badge=latest) Clang + GCC: [![Linux Build Status](https://github.com/TartanLlama/expected/actions/workflows/cmake.yml/badge.svg)](https://github.com/TartanLlama/expected/actions/workflows/cmake.yml) MSVC: [![Windows Build Status](https://ci.appveyor.com/api/projects/status/k5x00xa11y3s5wsg?svg=true)](https://ci.appveyor.com/project/TartanLlama/expected) Available on [Vcpkg](https://github.com/microsoft/vcpkg/tree/master/ports/tl-expected) and [Conan](https://github.com/yipdw/conan-tl-expected). [`std::expected`](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0323r3.pdf) is proposed as the preferred way to represent object which will either have an expected value, or an unexpected value giving information about why something failed. Unfortunately, chaining together many computations which may fail can be verbose, as error-checking code will be mixed in with the actual programming logic. This implementation provides a number of utilities to make coding with `expected` cleaner. For example, instead of writing this code: ```cpp std::expected get_cute_cat (const image& img) { auto cropped = crop_to_cat(img); if (!cropped) { return cropped; } auto with_tie = add_bow_tie(*cropped); if (!with_tie) { return with_tie; } auto with_sparkles = make_eyes_sparkle(*with_tie); if (!with_sparkles) { return with_sparkles; } return add_rainbow(make_smaller(*with_sparkles)); } ``` You can do this: ```cpp tl::expected get_cute_cat (const image& img) { return crop_to_cat(img) .and_then(add_bow_tie) .and_then(make_eyes_sparkle) .map(make_smaller) .map(add_rainbow); } ``` The interface is the same as `std::expected` as proposed in [p0323r3](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0323r3.pdf), but the following member functions are also defined. Explicit types are for clarity. - `map`: carries out some operation on the stored object if there is one. * `tl::expected s = exp_string.map(&std::string::size);` - `map_error`: carries out some operation on the unexpected object if there is one. * `my_error_code translate_error (std::error_code);` * `tl::expected s = exp_int.map_error(translate_error);` - `and_then`: like `map`, but for operations which return a `tl::expected`. * `tl::expected parse (const std::string& s);` * `tl::expected exp_ast = exp_string.and_then(parse);` - `or_else`: calls some function if there is no value stored. * `exp.or_else([] { throw std::runtime_error{"oh no"}; });` p0323r3 specifies calling `.error()` on an expected value, or using the `*` or `->` operators on an unexpected value, to be undefined behaviour. In this implementation it causes an assertion failure. The implementation of assertions can be overridden by defining the macro `TL_ASSERT(boolean_condition)` before #including ; by default, `assert(boolean_condition)` from the `` header is used. Note that correct code would not rely on these assertions. ### Compiler support Tested on: - Linux * clang++ 3.5, 3.6, 3.7, 3.8, 3.9, 4, 5, 6, 7, 8, 9, 10, 11 * g++ 4.8, 4.9, 5.5, 6.4, 7.5, 8, 9, 10 - Windows * MSVC 2015, 2017, 2019, 2022 ---------- [![CC0](http://i.creativecommons.org/p/zero/1.0/88x31.png)]("http://creativecommons.org/publicdomain/zero/1.0/") To the extent possible under law, [Sy Brand](https://twitter.com/TartanLlama) has waived all copyright and related or neighboring rights to the `expected` library. This work is published from: United Kingdom. expected-1.1.0/cmake/000077500000000000000000000000001440431670300143745ustar00rootroot00000000000000expected-1.1.0/cmake/tl-expected-config.cmake.in000066400000000000000000000001201440431670300214550ustar00rootroot00000000000000@PACKAGE_INIT@ include("${CMAKE_CURRENT_LIST_DIR}/tl-expected-targets.cmake")expected-1.1.0/include/000077500000000000000000000000001440431670300147375ustar00rootroot00000000000000expected-1.1.0/include/tl/000077500000000000000000000000001440431670300153565ustar00rootroot00000000000000expected-1.1.0/include/tl/expected.hpp000066400000000000000000002651521440431670300177030ustar00rootroot00000000000000/// // expected - An implementation of std::expected with extensions // Written in 2017 by Sy Brand (tartanllama@gmail.com, @TartanLlama) // // Documentation available at http://tl.tartanllama.xyz/ // // To the extent possible under law, the author(s) have dedicated all // copyright and related and neighboring rights to this software to the // public domain worldwide. This software is distributed without any warranty. // // You should have received a copy of the CC0 Public Domain Dedication // along with this software. If not, see // . /// #ifndef TL_EXPECTED_HPP #define TL_EXPECTED_HPP #define TL_EXPECTED_VERSION_MAJOR 1 #define TL_EXPECTED_VERSION_MINOR 1 #define TL_EXPECTED_VERSION_PATCH 0 #include #include #include #include #if defined(__EXCEPTIONS) || defined(_CPPUNWIND) #define TL_EXPECTED_EXCEPTIONS_ENABLED #endif #if (defined(_MSC_VER) && _MSC_VER == 1900) #define TL_EXPECTED_MSVC2015 #define TL_EXPECTED_MSVC2015_CONSTEXPR #else #define TL_EXPECTED_MSVC2015_CONSTEXPR constexpr #endif #if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && \ !defined(__clang__)) #define TL_EXPECTED_GCC49 #endif #if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 4 && \ !defined(__clang__)) #define TL_EXPECTED_GCC54 #endif #if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 5 && \ !defined(__clang__)) #define TL_EXPECTED_GCC55 #endif #if !defined(TL_ASSERT) //can't have assert in constexpr in C++11 and GCC 4.9 has a compiler bug #if (__cplusplus > 201103L) && !defined(TL_EXPECTED_GCC49) #include #define TL_ASSERT(x) assert(x) #else #define TL_ASSERT(x) #endif #endif #if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && \ !defined(__clang__)) // GCC < 5 doesn't support overloading on const&& for member functions #define TL_EXPECTED_NO_CONSTRR // GCC < 5 doesn't support some standard C++11 type traits #define TL_EXPECTED_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \ std::has_trivial_copy_constructor #define TL_EXPECTED_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \ std::has_trivial_copy_assign // This one will be different for GCC 5.7 if it's ever supported #define TL_EXPECTED_IS_TRIVIALLY_DESTRUCTIBLE(T) \ std::is_trivially_destructible // GCC 5 < v < 8 has a bug in is_trivially_copy_constructible which breaks // std::vector for non-copyable types #elif (defined(__GNUC__) && __GNUC__ < 8 && !defined(__clang__)) #ifndef TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX #define TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX namespace tl { namespace detail { template struct is_trivially_copy_constructible : std::is_trivially_copy_constructible {}; #ifdef _GLIBCXX_VECTOR template struct is_trivially_copy_constructible> : std::false_type {}; #endif } // namespace detail } // namespace tl #endif #define TL_EXPECTED_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \ tl::detail::is_trivially_copy_constructible #define TL_EXPECTED_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \ std::is_trivially_copy_assignable #define TL_EXPECTED_IS_TRIVIALLY_DESTRUCTIBLE(T) \ std::is_trivially_destructible #else #define TL_EXPECTED_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \ std::is_trivially_copy_constructible #define TL_EXPECTED_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \ std::is_trivially_copy_assignable #define TL_EXPECTED_IS_TRIVIALLY_DESTRUCTIBLE(T) \ std::is_trivially_destructible #endif #if __cplusplus > 201103L #define TL_EXPECTED_CXX14 #endif #ifdef TL_EXPECTED_GCC49 #define TL_EXPECTED_GCC49_CONSTEXPR #else #define TL_EXPECTED_GCC49_CONSTEXPR constexpr #endif #if (__cplusplus == 201103L || defined(TL_EXPECTED_MSVC2015) || \ defined(TL_EXPECTED_GCC49)) #define TL_EXPECTED_11_CONSTEXPR #else #define TL_EXPECTED_11_CONSTEXPR constexpr #endif namespace tl { template class expected; #ifndef TL_MONOSTATE_INPLACE_MUTEX #define TL_MONOSTATE_INPLACE_MUTEX class monostate {}; struct in_place_t { explicit in_place_t() = default; }; static constexpr in_place_t in_place{}; #endif template class unexpected { public: static_assert(!std::is_same::value, "E must not be void"); unexpected() = delete; constexpr explicit unexpected(const E &e) : m_val(e) {} constexpr explicit unexpected(E &&e) : m_val(std::move(e)) {} template ::value>::type * = nullptr> constexpr explicit unexpected(Args &&...args) : m_val(std::forward(args)...) {} template < class U, class... Args, typename std::enable_if &, Args &&...>::value>::type * = nullptr> constexpr explicit unexpected(std::initializer_list l, Args &&...args) : m_val(l, std::forward(args)...) {} constexpr const E &value() const & { return m_val; } TL_EXPECTED_11_CONSTEXPR E &value() & { return m_val; } TL_EXPECTED_11_CONSTEXPR E &&value() && { return std::move(m_val); } constexpr const E &&value() const && { return std::move(m_val); } private: E m_val; }; #ifdef __cpp_deduction_guides template unexpected(E) -> unexpected; #endif template constexpr bool operator==(const unexpected &lhs, const unexpected &rhs) { return lhs.value() == rhs.value(); } template constexpr bool operator!=(const unexpected &lhs, const unexpected &rhs) { return lhs.value() != rhs.value(); } template constexpr bool operator<(const unexpected &lhs, const unexpected &rhs) { return lhs.value() < rhs.value(); } template constexpr bool operator<=(const unexpected &lhs, const unexpected &rhs) { return lhs.value() <= rhs.value(); } template constexpr bool operator>(const unexpected &lhs, const unexpected &rhs) { return lhs.value() > rhs.value(); } template constexpr bool operator>=(const unexpected &lhs, const unexpected &rhs) { return lhs.value() >= rhs.value(); } template unexpected::type> make_unexpected(E &&e) { return unexpected::type>(std::forward(e)); } struct unexpect_t { unexpect_t() = default; }; static constexpr unexpect_t unexpect{}; namespace detail { template [[noreturn]] TL_EXPECTED_11_CONSTEXPR void throw_exception(E &&e) { #ifdef TL_EXPECTED_EXCEPTIONS_ENABLED throw std::forward(e); #else (void)e; #ifdef _MSC_VER __assume(0); #else __builtin_unreachable(); #endif #endif } #ifndef TL_TRAITS_MUTEX #define TL_TRAITS_MUTEX // C++14-style aliases for brevity template using remove_const_t = typename std::remove_const::type; template using remove_reference_t = typename std::remove_reference::type; template using decay_t = typename std::decay::type; template using enable_if_t = typename std::enable_if::type; template using conditional_t = typename std::conditional::type; // std::conjunction from C++17 template struct conjunction : std::true_type {}; template struct conjunction : B {}; template struct conjunction : std::conditional, B>::type {}; #if defined(_LIBCPP_VERSION) && __cplusplus == 201103L #define TL_TRAITS_LIBCXX_MEM_FN_WORKAROUND #endif // In C++11 mode, there's an issue in libc++'s std::mem_fn // which results in a hard-error when using it in a noexcept expression // in some cases. This is a check to workaround the common failing case. #ifdef TL_TRAITS_LIBCXX_MEM_FN_WORKAROUND template struct is_pointer_to_non_const_member_func : std::false_type {}; template struct is_pointer_to_non_const_member_func : std::true_type {}; template struct is_pointer_to_non_const_member_func : std::true_type {}; template struct is_pointer_to_non_const_member_func : std::true_type {}; template struct is_pointer_to_non_const_member_func : std::true_type {}; template struct is_pointer_to_non_const_member_func : std::true_type {}; template struct is_pointer_to_non_const_member_func : std::true_type {}; template struct is_const_or_const_ref : std::false_type {}; template struct is_const_or_const_ref : std::true_type {}; template struct is_const_or_const_ref : std::true_type {}; #endif // std::invoke from C++17 // https://stackoverflow.com/questions/38288042/c11-14-invoke-workaround template < typename Fn, typename... Args, #ifdef TL_TRAITS_LIBCXX_MEM_FN_WORKAROUND typename = enable_if_t::value && is_const_or_const_ref::value)>, #endif typename = enable_if_t>::value>, int = 0> constexpr auto invoke(Fn &&f, Args &&...args) noexcept( noexcept(std::mem_fn(f)(std::forward(args)...))) -> decltype(std::mem_fn(f)(std::forward(args)...)) { return std::mem_fn(f)(std::forward(args)...); } template >::value>> constexpr auto invoke(Fn &&f, Args &&...args) noexcept( noexcept(std::forward(f)(std::forward(args)...))) -> decltype(std::forward(f)(std::forward(args)...)) { return std::forward(f)(std::forward(args)...); } // std::invoke_result from C++17 template struct invoke_result_impl; template struct invoke_result_impl< F, decltype(detail::invoke(std::declval(), std::declval()...), void()), Us...> { using type = decltype(detail::invoke(std::declval(), std::declval()...)); }; template using invoke_result = invoke_result_impl; template using invoke_result_t = typename invoke_result::type; #if defined(_MSC_VER) && _MSC_VER <= 1900 // TODO make a version which works with MSVC 2015 template struct is_swappable : std::true_type {}; template struct is_nothrow_swappable : std::true_type {}; #else // https://stackoverflow.com/questions/26744589/what-is-a-proper-way-to-implement-is-swappable-to-test-for-the-swappable-concept namespace swap_adl_tests { // if swap ADL finds this then it would call std::swap otherwise (same // signature) struct tag {}; template tag swap(T &, T &); template tag swap(T (&a)[N], T (&b)[N]); // helper functions to test if an unqualified swap is possible, and if it // becomes std::swap template std::false_type can_swap(...) noexcept(false); template (), std::declval()))> std::true_type can_swap(int) noexcept(noexcept(swap(std::declval(), std::declval()))); template std::false_type uses_std(...); template std::is_same(), std::declval())), tag> uses_std(int); template struct is_std_swap_noexcept : std::integral_constant::value && std::is_nothrow_move_assignable::value> {}; template struct is_std_swap_noexcept : is_std_swap_noexcept {}; template struct is_adl_swap_noexcept : std::integral_constant(0))> {}; } // namespace swap_adl_tests template struct is_swappable : std::integral_constant< bool, decltype(detail::swap_adl_tests::can_swap(0))::value && (!decltype(detail::swap_adl_tests::uses_std(0))::value || (std::is_move_assignable::value && std::is_move_constructible::value))> {}; template struct is_swappable : std::integral_constant< bool, decltype(detail::swap_adl_tests::can_swap(0))::value && (!decltype(detail::swap_adl_tests::uses_std( 0))::value || is_swappable::value)> {}; template struct is_nothrow_swappable : std::integral_constant< bool, is_swappable::value && ((decltype(detail::swap_adl_tests::uses_std(0))::value && detail::swap_adl_tests::is_std_swap_noexcept::value) || (!decltype(detail::swap_adl_tests::uses_std(0))::value && detail::swap_adl_tests::is_adl_swap_noexcept::value))> {}; #endif #endif // Trait for checking if a type is a tl::expected template struct is_expected_impl : std::false_type {}; template struct is_expected_impl> : std::true_type {}; template using is_expected = is_expected_impl>; template using expected_enable_forward_value = detail::enable_if_t< std::is_constructible::value && !std::is_same, in_place_t>::value && !std::is_same, detail::decay_t>::value && !std::is_same, detail::decay_t>::value>; template using expected_enable_from_other = detail::enable_if_t< std::is_constructible::value && std::is_constructible::value && !std::is_constructible &>::value && !std::is_constructible &&>::value && !std::is_constructible &>::value && !std::is_constructible &&>::value && !std::is_convertible &, T>::value && !std::is_convertible &&, T>::value && !std::is_convertible &, T>::value && !std::is_convertible &&, T>::value>; template using is_void_or = conditional_t::value, std::true_type, U>; template using is_copy_constructible_or_void = is_void_or>; template using is_move_constructible_or_void = is_void_or>; template using is_copy_assignable_or_void = is_void_or>; template using is_move_assignable_or_void = is_void_or>; } // namespace detail namespace detail { struct no_init_t {}; static constexpr no_init_t no_init{}; // Implements the storage of the values, and ensures that the destructor is // trivial if it can be. // // This specialization is for where neither `T` or `E` is trivially // destructible, so the destructors must be called on destruction of the // `expected` template ::value, bool = std::is_trivially_destructible::value> struct expected_storage_base { constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {} constexpr expected_storage_base(no_init_t) : m_no_init(), m_has_val(false) {} template ::value> * = nullptr> constexpr expected_storage_base(in_place_t, Args &&...args) : m_val(std::forward(args)...), m_has_val(true) {} template &, Args &&...>::value> * = nullptr> constexpr expected_storage_base(in_place_t, std::initializer_list il, Args &&...args) : m_val(il, std::forward(args)...), m_has_val(true) {} template ::value> * = nullptr> constexpr explicit expected_storage_base(unexpect_t, Args &&...args) : m_unexpect(std::forward(args)...), m_has_val(false) {} template &, Args &&...>::value> * = nullptr> constexpr explicit expected_storage_base(unexpect_t, std::initializer_list il, Args &&...args) : m_unexpect(il, std::forward(args)...), m_has_val(false) {} ~expected_storage_base() { if (m_has_val) { m_val.~T(); } else { m_unexpect.~unexpected(); } } union { T m_val; unexpected m_unexpect; char m_no_init; }; bool m_has_val; }; // This specialization is for when both `T` and `E` are trivially-destructible, // so the destructor of the `expected` can be trivial. template struct expected_storage_base { constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {} constexpr expected_storage_base(no_init_t) : m_no_init(), m_has_val(false) {} template ::value> * = nullptr> constexpr expected_storage_base(in_place_t, Args &&...args) : m_val(std::forward(args)...), m_has_val(true) {} template &, Args &&...>::value> * = nullptr> constexpr expected_storage_base(in_place_t, std::initializer_list il, Args &&...args) : m_val(il, std::forward(args)...), m_has_val(true) {} template ::value> * = nullptr> constexpr explicit expected_storage_base(unexpect_t, Args &&...args) : m_unexpect(std::forward(args)...), m_has_val(false) {} template &, Args &&...>::value> * = nullptr> constexpr explicit expected_storage_base(unexpect_t, std::initializer_list il, Args &&...args) : m_unexpect(il, std::forward(args)...), m_has_val(false) {} ~expected_storage_base() = default; union { T m_val; unexpected m_unexpect; char m_no_init; }; bool m_has_val; }; // T is trivial, E is not. template struct expected_storage_base { constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {} TL_EXPECTED_MSVC2015_CONSTEXPR expected_storage_base(no_init_t) : m_no_init(), m_has_val(false) {} template ::value> * = nullptr> constexpr expected_storage_base(in_place_t, Args &&...args) : m_val(std::forward(args)...), m_has_val(true) {} template &, Args &&...>::value> * = nullptr> constexpr expected_storage_base(in_place_t, std::initializer_list il, Args &&...args) : m_val(il, std::forward(args)...), m_has_val(true) {} template ::value> * = nullptr> constexpr explicit expected_storage_base(unexpect_t, Args &&...args) : m_unexpect(std::forward(args)...), m_has_val(false) {} template &, Args &&...>::value> * = nullptr> constexpr explicit expected_storage_base(unexpect_t, std::initializer_list il, Args &&...args) : m_unexpect(il, std::forward(args)...), m_has_val(false) {} ~expected_storage_base() { if (!m_has_val) { m_unexpect.~unexpected(); } } union { T m_val; unexpected m_unexpect; char m_no_init; }; bool m_has_val; }; // E is trivial, T is not. template struct expected_storage_base { constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {} constexpr expected_storage_base(no_init_t) : m_no_init(), m_has_val(false) {} template ::value> * = nullptr> constexpr expected_storage_base(in_place_t, Args &&...args) : m_val(std::forward(args)...), m_has_val(true) {} template &, Args &&...>::value> * = nullptr> constexpr expected_storage_base(in_place_t, std::initializer_list il, Args &&...args) : m_val(il, std::forward(args)...), m_has_val(true) {} template ::value> * = nullptr> constexpr explicit expected_storage_base(unexpect_t, Args &&...args) : m_unexpect(std::forward(args)...), m_has_val(false) {} template &, Args &&...>::value> * = nullptr> constexpr explicit expected_storage_base(unexpect_t, std::initializer_list il, Args &&...args) : m_unexpect(il, std::forward(args)...), m_has_val(false) {} ~expected_storage_base() { if (m_has_val) { m_val.~T(); } } union { T m_val; unexpected m_unexpect; char m_no_init; }; bool m_has_val; }; // `T` is `void`, `E` is trivially-destructible template struct expected_storage_base { #if __GNUC__ <= 5 //no constexpr for GCC 4/5 bug #else TL_EXPECTED_MSVC2015_CONSTEXPR #endif expected_storage_base() : m_has_val(true) {} constexpr expected_storage_base(no_init_t) : m_val(), m_has_val(false) {} constexpr expected_storage_base(in_place_t) : m_has_val(true) {} template ::value> * = nullptr> constexpr explicit expected_storage_base(unexpect_t, Args &&...args) : m_unexpect(std::forward(args)...), m_has_val(false) {} template &, Args &&...>::value> * = nullptr> constexpr explicit expected_storage_base(unexpect_t, std::initializer_list il, Args &&...args) : m_unexpect(il, std::forward(args)...), m_has_val(false) {} ~expected_storage_base() = default; struct dummy {}; union { unexpected m_unexpect; dummy m_val; }; bool m_has_val; }; // `T` is `void`, `E` is not trivially-destructible template struct expected_storage_base { constexpr expected_storage_base() : m_dummy(), m_has_val(true) {} constexpr expected_storage_base(no_init_t) : m_dummy(), m_has_val(false) {} constexpr expected_storage_base(in_place_t) : m_dummy(), m_has_val(true) {} template ::value> * = nullptr> constexpr explicit expected_storage_base(unexpect_t, Args &&...args) : m_unexpect(std::forward(args)...), m_has_val(false) {} template &, Args &&...>::value> * = nullptr> constexpr explicit expected_storage_base(unexpect_t, std::initializer_list il, Args &&...args) : m_unexpect(il, std::forward(args)...), m_has_val(false) {} ~expected_storage_base() { if (!m_has_val) { m_unexpect.~unexpected(); } } union { unexpected m_unexpect; char m_dummy; }; bool m_has_val; }; // This base class provides some handy member functions which can be used in // further derived classes template struct expected_operations_base : expected_storage_base { using expected_storage_base::expected_storage_base; template void construct(Args &&...args) noexcept { new (std::addressof(this->m_val)) T(std::forward(args)...); this->m_has_val = true; } template void construct_with(Rhs &&rhs) noexcept { new (std::addressof(this->m_val)) T(std::forward(rhs).get()); this->m_has_val = true; } template void construct_error(Args &&...args) noexcept { new (std::addressof(this->m_unexpect)) unexpected(std::forward(args)...); this->m_has_val = false; } #ifdef TL_EXPECTED_EXCEPTIONS_ENABLED // These assign overloads ensure that the most efficient assignment // implementation is used while maintaining the strong exception guarantee. // The problematic case is where rhs has a value, but *this does not. // // This overload handles the case where we can just copy-construct `T` // directly into place without throwing. template ::value> * = nullptr> void assign(const expected_operations_base &rhs) noexcept { if (!this->m_has_val && rhs.m_has_val) { geterr().~unexpected(); construct(rhs.get()); } else { assign_common(rhs); } } // This overload handles the case where we can attempt to create a copy of // `T`, then no-throw move it into place if the copy was successful. template ::value && std::is_nothrow_move_constructible::value> * = nullptr> void assign(const expected_operations_base &rhs) noexcept { if (!this->m_has_val && rhs.m_has_val) { T tmp = rhs.get(); geterr().~unexpected(); construct(std::move(tmp)); } else { assign_common(rhs); } } // This overload is the worst-case, where we have to move-construct the // unexpected value into temporary storage, then try to copy the T into place. // If the construction succeeds, then everything is fine, but if it throws, // then we move the old unexpected value back into place before rethrowing the // exception. template ::value && !std::is_nothrow_move_constructible::value> * = nullptr> void assign(const expected_operations_base &rhs) { if (!this->m_has_val && rhs.m_has_val) { auto tmp = std::move(geterr()); geterr().~unexpected(); #ifdef TL_EXPECTED_EXCEPTIONS_ENABLED try { construct(rhs.get()); } catch (...) { geterr() = std::move(tmp); throw; } #else construct(rhs.get()); #endif } else { assign_common(rhs); } } // These overloads do the same as above, but for rvalues template ::value> * = nullptr> void assign(expected_operations_base &&rhs) noexcept { if (!this->m_has_val && rhs.m_has_val) { geterr().~unexpected(); construct(std::move(rhs).get()); } else { assign_common(std::move(rhs)); } } template ::value> * = nullptr> void assign(expected_operations_base &&rhs) { if (!this->m_has_val && rhs.m_has_val) { auto tmp = std::move(geterr()); geterr().~unexpected(); #ifdef TL_EXPECTED_EXCEPTIONS_ENABLED try { construct(std::move(rhs).get()); } catch (...) { geterr() = std::move(tmp); throw; } #else construct(std::move(rhs).get()); #endif } else { assign_common(std::move(rhs)); } } #else // If exceptions are disabled then we can just copy-construct void assign(const expected_operations_base &rhs) noexcept { if (!this->m_has_val && rhs.m_has_val) { geterr().~unexpected(); construct(rhs.get()); } else { assign_common(rhs); } } void assign(expected_operations_base &&rhs) noexcept { if (!this->m_has_val && rhs.m_has_val) { geterr().~unexpected(); construct(std::move(rhs).get()); } else { assign_common(std::move(rhs)); } } #endif // The common part of move/copy assigning template void assign_common(Rhs &&rhs) { if (this->m_has_val) { if (rhs.m_has_val) { get() = std::forward(rhs).get(); } else { destroy_val(); construct_error(std::forward(rhs).geterr()); } } else { if (!rhs.m_has_val) { geterr() = std::forward(rhs).geterr(); } } } bool has_value() const { return this->m_has_val; } TL_EXPECTED_11_CONSTEXPR T &get() & { return this->m_val; } constexpr const T &get() const & { return this->m_val; } TL_EXPECTED_11_CONSTEXPR T &&get() && { return std::move(this->m_val); } #ifndef TL_EXPECTED_NO_CONSTRR constexpr const T &&get() const && { return std::move(this->m_val); } #endif TL_EXPECTED_11_CONSTEXPR unexpected &geterr() & { return this->m_unexpect; } constexpr const unexpected &geterr() const & { return this->m_unexpect; } TL_EXPECTED_11_CONSTEXPR unexpected &&geterr() && { return std::move(this->m_unexpect); } #ifndef TL_EXPECTED_NO_CONSTRR constexpr const unexpected &&geterr() const && { return std::move(this->m_unexpect); } #endif TL_EXPECTED_11_CONSTEXPR void destroy_val() { get().~T(); } }; // This base class provides some handy member functions which can be used in // further derived classes template struct expected_operations_base : expected_storage_base { using expected_storage_base::expected_storage_base; template void construct() noexcept { this->m_has_val = true; } // This function doesn't use its argument, but needs it so that code in // levels above this can work independently of whether T is void template void construct_with(Rhs &&) noexcept { this->m_has_val = true; } template void construct_error(Args &&...args) noexcept { new (std::addressof(this->m_unexpect)) unexpected(std::forward(args)...); this->m_has_val = false; } template void assign(Rhs &&rhs) noexcept { if (!this->m_has_val) { if (rhs.m_has_val) { geterr().~unexpected(); construct(); } else { geterr() = std::forward(rhs).geterr(); } } else { if (!rhs.m_has_val) { construct_error(std::forward(rhs).geterr()); } } } bool has_value() const { return this->m_has_val; } TL_EXPECTED_11_CONSTEXPR unexpected &geterr() & { return this->m_unexpect; } constexpr const unexpected &geterr() const & { return this->m_unexpect; } TL_EXPECTED_11_CONSTEXPR unexpected &&geterr() && { return std::move(this->m_unexpect); } #ifndef TL_EXPECTED_NO_CONSTRR constexpr const unexpected &&geterr() const && { return std::move(this->m_unexpect); } #endif TL_EXPECTED_11_CONSTEXPR void destroy_val() { // no-op } }; // This class manages conditionally having a trivial copy constructor // This specialization is for when T and E are trivially copy constructible template :: value &&TL_EXPECTED_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(E)::value> struct expected_copy_base : expected_operations_base { using expected_operations_base::expected_operations_base; }; // This specialization is for when T or E are not trivially copy constructible template struct expected_copy_base : expected_operations_base { using expected_operations_base::expected_operations_base; expected_copy_base() = default; expected_copy_base(const expected_copy_base &rhs) : expected_operations_base(no_init) { if (rhs.has_value()) { this->construct_with(rhs); } else { this->construct_error(rhs.geterr()); } } expected_copy_base(expected_copy_base &&rhs) = default; expected_copy_base &operator=(const expected_copy_base &rhs) = default; expected_copy_base &operator=(expected_copy_base &&rhs) = default; }; // This class manages conditionally having a trivial move constructor // Unfortunately there's no way to achieve this in GCC < 5 AFAIK, since it // doesn't implement an analogue to std::is_trivially_move_constructible. We // have to make do with a non-trivial move constructor even if T is trivially // move constructible #ifndef TL_EXPECTED_GCC49 template >::value &&std::is_trivially_move_constructible::value> struct expected_move_base : expected_copy_base { using expected_copy_base::expected_copy_base; }; #else template struct expected_move_base; #endif template struct expected_move_base : expected_copy_base { using expected_copy_base::expected_copy_base; expected_move_base() = default; expected_move_base(const expected_move_base &rhs) = default; expected_move_base(expected_move_base &&rhs) noexcept( std::is_nothrow_move_constructible::value) : expected_copy_base(no_init) { if (rhs.has_value()) { this->construct_with(std::move(rhs)); } else { this->construct_error(std::move(rhs.geterr())); } } expected_move_base &operator=(const expected_move_base &rhs) = default; expected_move_base &operator=(expected_move_base &&rhs) = default; }; // This class manages conditionally having a trivial copy assignment operator template >::value &&TL_EXPECTED_IS_TRIVIALLY_COPY_ASSIGNABLE(E)::value &&TL_EXPECTED_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(E)::value &&TL_EXPECTED_IS_TRIVIALLY_DESTRUCTIBLE(E)::value> struct expected_copy_assign_base : expected_move_base { using expected_move_base::expected_move_base; }; template struct expected_copy_assign_base : expected_move_base { using expected_move_base::expected_move_base; expected_copy_assign_base() = default; expected_copy_assign_base(const expected_copy_assign_base &rhs) = default; expected_copy_assign_base(expected_copy_assign_base &&rhs) = default; expected_copy_assign_base &operator=(const expected_copy_assign_base &rhs) { this->assign(rhs); return *this; } expected_copy_assign_base & operator=(expected_copy_assign_base &&rhs) = default; }; // This class manages conditionally having a trivial move assignment operator // Unfortunately there's no way to achieve this in GCC < 5 AFAIK, since it // doesn't implement an analogue to std::is_trivially_move_assignable. We have // to make do with a non-trivial move assignment operator even if T is trivially // move assignable #ifndef TL_EXPECTED_GCC49 template , std::is_trivially_move_constructible, std::is_trivially_move_assignable>>:: value &&std::is_trivially_destructible::value &&std::is_trivially_move_constructible::value &&std::is_trivially_move_assignable::value> struct expected_move_assign_base : expected_copy_assign_base { using expected_copy_assign_base::expected_copy_assign_base; }; #else template struct expected_move_assign_base; #endif template struct expected_move_assign_base : expected_copy_assign_base { using expected_copy_assign_base::expected_copy_assign_base; expected_move_assign_base() = default; expected_move_assign_base(const expected_move_assign_base &rhs) = default; expected_move_assign_base(expected_move_assign_base &&rhs) = default; expected_move_assign_base & operator=(const expected_move_assign_base &rhs) = default; expected_move_assign_base & operator=(expected_move_assign_base &&rhs) noexcept( std::is_nothrow_move_constructible::value &&std::is_nothrow_move_assignable::value) { this->assign(std::move(rhs)); return *this; } }; // expected_delete_ctor_base will conditionally delete copy and move // constructors depending on whether T is copy/move constructible template ::value && std::is_copy_constructible::value), bool EnableMove = (is_move_constructible_or_void::value && std::is_move_constructible::value)> struct expected_delete_ctor_base { expected_delete_ctor_base() = default; expected_delete_ctor_base(const expected_delete_ctor_base &) = default; expected_delete_ctor_base(expected_delete_ctor_base &&) noexcept = default; expected_delete_ctor_base & operator=(const expected_delete_ctor_base &) = default; expected_delete_ctor_base & operator=(expected_delete_ctor_base &&) noexcept = default; }; template struct expected_delete_ctor_base { expected_delete_ctor_base() = default; expected_delete_ctor_base(const expected_delete_ctor_base &) = default; expected_delete_ctor_base(expected_delete_ctor_base &&) noexcept = delete; expected_delete_ctor_base & operator=(const expected_delete_ctor_base &) = default; expected_delete_ctor_base & operator=(expected_delete_ctor_base &&) noexcept = default; }; template struct expected_delete_ctor_base { expected_delete_ctor_base() = default; expected_delete_ctor_base(const expected_delete_ctor_base &) = delete; expected_delete_ctor_base(expected_delete_ctor_base &&) noexcept = default; expected_delete_ctor_base & operator=(const expected_delete_ctor_base &) = default; expected_delete_ctor_base & operator=(expected_delete_ctor_base &&) noexcept = default; }; template struct expected_delete_ctor_base { expected_delete_ctor_base() = default; expected_delete_ctor_base(const expected_delete_ctor_base &) = delete; expected_delete_ctor_base(expected_delete_ctor_base &&) noexcept = delete; expected_delete_ctor_base & operator=(const expected_delete_ctor_base &) = default; expected_delete_ctor_base & operator=(expected_delete_ctor_base &&) noexcept = default; }; // expected_delete_assign_base will conditionally delete copy and move // constructors depending on whether T and E are copy/move constructible + // assignable template ::value && std::is_copy_constructible::value && is_copy_assignable_or_void::value && std::is_copy_assignable::value), bool EnableMove = (is_move_constructible_or_void::value && std::is_move_constructible::value && is_move_assignable_or_void::value && std::is_move_assignable::value)> struct expected_delete_assign_base { expected_delete_assign_base() = default; expected_delete_assign_base(const expected_delete_assign_base &) = default; expected_delete_assign_base(expected_delete_assign_base &&) noexcept = default; expected_delete_assign_base & operator=(const expected_delete_assign_base &) = default; expected_delete_assign_base & operator=(expected_delete_assign_base &&) noexcept = default; }; template struct expected_delete_assign_base { expected_delete_assign_base() = default; expected_delete_assign_base(const expected_delete_assign_base &) = default; expected_delete_assign_base(expected_delete_assign_base &&) noexcept = default; expected_delete_assign_base & operator=(const expected_delete_assign_base &) = default; expected_delete_assign_base & operator=(expected_delete_assign_base &&) noexcept = delete; }; template struct expected_delete_assign_base { expected_delete_assign_base() = default; expected_delete_assign_base(const expected_delete_assign_base &) = default; expected_delete_assign_base(expected_delete_assign_base &&) noexcept = default; expected_delete_assign_base & operator=(const expected_delete_assign_base &) = delete; expected_delete_assign_base & operator=(expected_delete_assign_base &&) noexcept = default; }; template struct expected_delete_assign_base { expected_delete_assign_base() = default; expected_delete_assign_base(const expected_delete_assign_base &) = default; expected_delete_assign_base(expected_delete_assign_base &&) noexcept = default; expected_delete_assign_base & operator=(const expected_delete_assign_base &) = delete; expected_delete_assign_base & operator=(expected_delete_assign_base &&) noexcept = delete; }; // This is needed to be able to construct the expected_default_ctor_base which // follows, while still conditionally deleting the default constructor. struct default_constructor_tag { explicit constexpr default_constructor_tag() = default; }; // expected_default_ctor_base will ensure that expected has a deleted default // consturctor if T is not default constructible. // This specialization is for when T is default constructible template ::value || std::is_void::value> struct expected_default_ctor_base { constexpr expected_default_ctor_base() noexcept = default; constexpr expected_default_ctor_base( expected_default_ctor_base const &) noexcept = default; constexpr expected_default_ctor_base(expected_default_ctor_base &&) noexcept = default; expected_default_ctor_base & operator=(expected_default_ctor_base const &) noexcept = default; expected_default_ctor_base & operator=(expected_default_ctor_base &&) noexcept = default; constexpr explicit expected_default_ctor_base(default_constructor_tag) {} }; // This specialization is for when T is not default constructible template struct expected_default_ctor_base { constexpr expected_default_ctor_base() noexcept = delete; constexpr expected_default_ctor_base( expected_default_ctor_base const &) noexcept = default; constexpr expected_default_ctor_base(expected_default_ctor_base &&) noexcept = default; expected_default_ctor_base & operator=(expected_default_ctor_base const &) noexcept = default; expected_default_ctor_base & operator=(expected_default_ctor_base &&) noexcept = default; constexpr explicit expected_default_ctor_base(default_constructor_tag) {} }; } // namespace detail template class bad_expected_access : public std::exception { public: explicit bad_expected_access(E e) : m_val(std::move(e)) {} virtual const char *what() const noexcept override { return "Bad expected access"; } const E &error() const & { return m_val; } E &error() & { return m_val; } const E &&error() const && { return std::move(m_val); } E &&error() && { return std::move(m_val); } private: E m_val; }; /// An `expected` object is an object that contains the storage for /// another object and manages the lifetime of this contained object `T`. /// Alternatively it could contain the storage for another unexpected object /// `E`. The contained object may not be initialized after the expected object /// has been initialized, and may not be destroyed before the expected object /// has been destroyed. The initialization state of the contained object is /// tracked by the expected object. template class expected : private detail::expected_move_assign_base, private detail::expected_delete_ctor_base, private detail::expected_delete_assign_base, private detail::expected_default_ctor_base { static_assert(!std::is_reference::value, "T must not be a reference"); static_assert(!std::is_same::type>::value, "T must not be in_place_t"); static_assert(!std::is_same::type>::value, "T must not be unexpect_t"); static_assert( !std::is_same>::type>::value, "T must not be unexpected"); static_assert(!std::is_reference::value, "E must not be a reference"); T *valptr() { return std::addressof(this->m_val); } const T *valptr() const { return std::addressof(this->m_val); } unexpected *errptr() { return std::addressof(this->m_unexpect); } const unexpected *errptr() const { return std::addressof(this->m_unexpect); } template ::value> * = nullptr> TL_EXPECTED_11_CONSTEXPR U &val() { return this->m_val; } TL_EXPECTED_11_CONSTEXPR unexpected &err() { return this->m_unexpect; } template ::value> * = nullptr> constexpr const U &val() const { return this->m_val; } constexpr const unexpected &err() const { return this->m_unexpect; } using impl_base = detail::expected_move_assign_base; using ctor_base = detail::expected_default_ctor_base; public: typedef T value_type; typedef E error_type; typedef unexpected unexpected_type; #if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \ !defined(TL_EXPECTED_GCC54) && !defined(TL_EXPECTED_GCC55) template TL_EXPECTED_11_CONSTEXPR auto and_then(F &&f) & { return and_then_impl(*this, std::forward(f)); } template TL_EXPECTED_11_CONSTEXPR auto and_then(F &&f) && { return and_then_impl(std::move(*this), std::forward(f)); } template constexpr auto and_then(F &&f) const & { return and_then_impl(*this, std::forward(f)); } #ifndef TL_EXPECTED_NO_CONSTRR template constexpr auto and_then(F &&f) const && { return and_then_impl(std::move(*this), std::forward(f)); } #endif #else template TL_EXPECTED_11_CONSTEXPR auto and_then(F &&f) & -> decltype(and_then_impl(std::declval(), std::forward(f))) { return and_then_impl(*this, std::forward(f)); } template TL_EXPECTED_11_CONSTEXPR auto and_then(F &&f) && -> decltype(and_then_impl(std::declval(), std::forward(f))) { return and_then_impl(std::move(*this), std::forward(f)); } template constexpr auto and_then(F &&f) const & -> decltype(and_then_impl( std::declval(), std::forward(f))) { return and_then_impl(*this, std::forward(f)); } #ifndef TL_EXPECTED_NO_CONSTRR template constexpr auto and_then(F &&f) const && -> decltype(and_then_impl( std::declval(), std::forward(f))) { return and_then_impl(std::move(*this), std::forward(f)); } #endif #endif #if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \ !defined(TL_EXPECTED_GCC54) && !defined(TL_EXPECTED_GCC55) template TL_EXPECTED_11_CONSTEXPR auto map(F &&f) & { return expected_map_impl(*this, std::forward(f)); } template TL_EXPECTED_11_CONSTEXPR auto map(F &&f) && { return expected_map_impl(std::move(*this), std::forward(f)); } template constexpr auto map(F &&f) const & { return expected_map_impl(*this, std::forward(f)); } template constexpr auto map(F &&f) const && { return expected_map_impl(std::move(*this), std::forward(f)); } #else template TL_EXPECTED_11_CONSTEXPR decltype(expected_map_impl( std::declval(), std::declval())) map(F &&f) & { return expected_map_impl(*this, std::forward(f)); } template TL_EXPECTED_11_CONSTEXPR decltype(expected_map_impl(std::declval(), std::declval())) map(F &&f) && { return expected_map_impl(std::move(*this), std::forward(f)); } template constexpr decltype(expected_map_impl(std::declval(), std::declval())) map(F &&f) const & { return expected_map_impl(*this, std::forward(f)); } #ifndef TL_EXPECTED_NO_CONSTRR template constexpr decltype(expected_map_impl(std::declval(), std::declval())) map(F &&f) const && { return expected_map_impl(std::move(*this), std::forward(f)); } #endif #endif #if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \ !defined(TL_EXPECTED_GCC54) && !defined(TL_EXPECTED_GCC55) template TL_EXPECTED_11_CONSTEXPR auto transform(F &&f) & { return expected_map_impl(*this, std::forward(f)); } template TL_EXPECTED_11_CONSTEXPR auto transform(F &&f) && { return expected_map_impl(std::move(*this), std::forward(f)); } template constexpr auto transform(F &&f) const & { return expected_map_impl(*this, std::forward(f)); } template constexpr auto transform(F &&f) const && { return expected_map_impl(std::move(*this), std::forward(f)); } #else template TL_EXPECTED_11_CONSTEXPR decltype(expected_map_impl( std::declval(), std::declval())) transform(F &&f) & { return expected_map_impl(*this, std::forward(f)); } template TL_EXPECTED_11_CONSTEXPR decltype(expected_map_impl(std::declval(), std::declval())) transform(F &&f) && { return expected_map_impl(std::move(*this), std::forward(f)); } template constexpr decltype(expected_map_impl(std::declval(), std::declval())) transform(F &&f) const & { return expected_map_impl(*this, std::forward(f)); } #ifndef TL_EXPECTED_NO_CONSTRR template constexpr decltype(expected_map_impl(std::declval(), std::declval())) transform(F &&f) const && { return expected_map_impl(std::move(*this), std::forward(f)); } #endif #endif #if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \ !defined(TL_EXPECTED_GCC54) && !defined(TL_EXPECTED_GCC55) template TL_EXPECTED_11_CONSTEXPR auto map_error(F &&f) & { return map_error_impl(*this, std::forward(f)); } template TL_EXPECTED_11_CONSTEXPR auto map_error(F &&f) && { return map_error_impl(std::move(*this), std::forward(f)); } template constexpr auto map_error(F &&f) const & { return map_error_impl(*this, std::forward(f)); } template constexpr auto map_error(F &&f) const && { return map_error_impl(std::move(*this), std::forward(f)); } #else template TL_EXPECTED_11_CONSTEXPR decltype(map_error_impl(std::declval(), std::declval())) map_error(F &&f) & { return map_error_impl(*this, std::forward(f)); } template TL_EXPECTED_11_CONSTEXPR decltype(map_error_impl(std::declval(), std::declval())) map_error(F &&f) && { return map_error_impl(std::move(*this), std::forward(f)); } template constexpr decltype(map_error_impl(std::declval(), std::declval())) map_error(F &&f) const & { return map_error_impl(*this, std::forward(f)); } #ifndef TL_EXPECTED_NO_CONSTRR template constexpr decltype(map_error_impl(std::declval(), std::declval())) map_error(F &&f) const && { return map_error_impl(std::move(*this), std::forward(f)); } #endif #endif #if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \ !defined(TL_EXPECTED_GCC54) && !defined(TL_EXPECTED_GCC55) template TL_EXPECTED_11_CONSTEXPR auto transform_error(F &&f) & { return map_error_impl(*this, std::forward(f)); } template TL_EXPECTED_11_CONSTEXPR auto transform_error(F &&f) && { return map_error_impl(std::move(*this), std::forward(f)); } template constexpr auto transform_error(F &&f) const & { return map_error_impl(*this, std::forward(f)); } template constexpr auto transform_error(F &&f) const && { return map_error_impl(std::move(*this), std::forward(f)); } #else template TL_EXPECTED_11_CONSTEXPR decltype(map_error_impl(std::declval(), std::declval())) transform_error(F &&f) & { return map_error_impl(*this, std::forward(f)); } template TL_EXPECTED_11_CONSTEXPR decltype(map_error_impl(std::declval(), std::declval())) transform_error(F &&f) && { return map_error_impl(std::move(*this), std::forward(f)); } template constexpr decltype(map_error_impl(std::declval(), std::declval())) transform_error(F &&f) const & { return map_error_impl(*this, std::forward(f)); } #ifndef TL_EXPECTED_NO_CONSTRR template constexpr decltype(map_error_impl(std::declval(), std::declval())) transform_error(F &&f) const && { return map_error_impl(std::move(*this), std::forward(f)); } #endif #endif template expected TL_EXPECTED_11_CONSTEXPR or_else(F &&f) & { return or_else_impl(*this, std::forward(f)); } template expected TL_EXPECTED_11_CONSTEXPR or_else(F &&f) && { return or_else_impl(std::move(*this), std::forward(f)); } template expected constexpr or_else(F &&f) const & { return or_else_impl(*this, std::forward(f)); } #ifndef TL_EXPECTED_NO_CONSTRR template expected constexpr or_else(F &&f) const && { return or_else_impl(std::move(*this), std::forward(f)); } #endif constexpr expected() = default; constexpr expected(const expected &rhs) = default; constexpr expected(expected &&rhs) = default; expected &operator=(const expected &rhs) = default; expected &operator=(expected &&rhs) = default; template ::value> * = nullptr> constexpr expected(in_place_t, Args &&...args) : impl_base(in_place, std::forward(args)...), ctor_base(detail::default_constructor_tag{}) {} template &, Args &&...>::value> * = nullptr> constexpr expected(in_place_t, std::initializer_list il, Args &&...args) : impl_base(in_place, il, std::forward(args)...), ctor_base(detail::default_constructor_tag{}) {} template ::value> * = nullptr, detail::enable_if_t::value> * = nullptr> explicit constexpr expected(const unexpected &e) : impl_base(unexpect, e.value()), ctor_base(detail::default_constructor_tag{}) {} template < class G = E, detail::enable_if_t::value> * = nullptr, detail::enable_if_t::value> * = nullptr> constexpr expected(unexpected const &e) : impl_base(unexpect, e.value()), ctor_base(detail::default_constructor_tag{}) {} template < class G = E, detail::enable_if_t::value> * = nullptr, detail::enable_if_t::value> * = nullptr> explicit constexpr expected(unexpected &&e) noexcept( std::is_nothrow_constructible::value) : impl_base(unexpect, std::move(e.value())), ctor_base(detail::default_constructor_tag{}) {} template < class G = E, detail::enable_if_t::value> * = nullptr, detail::enable_if_t::value> * = nullptr> constexpr expected(unexpected &&e) noexcept( std::is_nothrow_constructible::value) : impl_base(unexpect, std::move(e.value())), ctor_base(detail::default_constructor_tag{}) {} template ::value> * = nullptr> constexpr explicit expected(unexpect_t, Args &&...args) : impl_base(unexpect, std::forward(args)...), ctor_base(detail::default_constructor_tag{}) {} template &, Args &&...>::value> * = nullptr> constexpr explicit expected(unexpect_t, std::initializer_list il, Args &&...args) : impl_base(unexpect, il, std::forward(args)...), ctor_base(detail::default_constructor_tag{}) {} template ::value && std::is_convertible::value)> * = nullptr, detail::expected_enable_from_other * = nullptr> explicit TL_EXPECTED_11_CONSTEXPR expected(const expected &rhs) : ctor_base(detail::default_constructor_tag{}) { if (rhs.has_value()) { this->construct(*rhs); } else { this->construct_error(rhs.error()); } } template ::value && std::is_convertible::value)> * = nullptr, detail::expected_enable_from_other * = nullptr> TL_EXPECTED_11_CONSTEXPR expected(const expected &rhs) : ctor_base(detail::default_constructor_tag{}) { if (rhs.has_value()) { this->construct(*rhs); } else { this->construct_error(rhs.error()); } } template < class U, class G, detail::enable_if_t::value && std::is_convertible::value)> * = nullptr, detail::expected_enable_from_other * = nullptr> explicit TL_EXPECTED_11_CONSTEXPR expected(expected &&rhs) : ctor_base(detail::default_constructor_tag{}) { if (rhs.has_value()) { this->construct(std::move(*rhs)); } else { this->construct_error(std::move(rhs.error())); } } template < class U, class G, detail::enable_if_t<(std::is_convertible::value && std::is_convertible::value)> * = nullptr, detail::expected_enable_from_other * = nullptr> TL_EXPECTED_11_CONSTEXPR expected(expected &&rhs) : ctor_base(detail::default_constructor_tag{}) { if (rhs.has_value()) { this->construct(std::move(*rhs)); } else { this->construct_error(std::move(rhs.error())); } } template < class U = T, detail::enable_if_t::value> * = nullptr, detail::expected_enable_forward_value * = nullptr> explicit TL_EXPECTED_MSVC2015_CONSTEXPR expected(U &&v) : expected(in_place, std::forward(v)) {} template < class U = T, detail::enable_if_t::value> * = nullptr, detail::expected_enable_forward_value * = nullptr> TL_EXPECTED_MSVC2015_CONSTEXPR expected(U &&v) : expected(in_place, std::forward(v)) {} template < class U = T, class G = T, detail::enable_if_t::value> * = nullptr, detail::enable_if_t::value> * = nullptr, detail::enable_if_t< (!std::is_same, detail::decay_t>::value && !detail::conjunction, std::is_same>>::value && std::is_constructible::value && std::is_assignable::value && std::is_nothrow_move_constructible::value)> * = nullptr> expected &operator=(U &&v) { if (has_value()) { val() = std::forward(v); } else { err().~unexpected(); ::new (valptr()) T(std::forward(v)); this->m_has_val = true; } return *this; } template < class U = T, class G = T, detail::enable_if_t::value> * = nullptr, detail::enable_if_t::value> * = nullptr, detail::enable_if_t< (!std::is_same, detail::decay_t>::value && !detail::conjunction, std::is_same>>::value && std::is_constructible::value && std::is_assignable::value && std::is_nothrow_move_constructible::value)> * = nullptr> expected &operator=(U &&v) { if (has_value()) { val() = std::forward(v); } else { auto tmp = std::move(err()); err().~unexpected(); #ifdef TL_EXPECTED_EXCEPTIONS_ENABLED try { ::new (valptr()) T(std::forward(v)); this->m_has_val = true; } catch (...) { err() = std::move(tmp); throw; } #else ::new (valptr()) T(std::forward(v)); this->m_has_val = true; #endif } return *this; } template ::value && std::is_assignable::value> * = nullptr> expected &operator=(const unexpected &rhs) { if (!has_value()) { err() = rhs; } else { this->destroy_val(); ::new (errptr()) unexpected(rhs); this->m_has_val = false; } return *this; } template ::value && std::is_move_assignable::value> * = nullptr> expected &operator=(unexpected &&rhs) noexcept { if (!has_value()) { err() = std::move(rhs); } else { this->destroy_val(); ::new (errptr()) unexpected(std::move(rhs)); this->m_has_val = false; } return *this; } template ::value> * = nullptr> void emplace(Args &&...args) { if (has_value()) { val().~T(); } else { err().~unexpected(); this->m_has_val = true; } ::new (valptr()) T(std::forward(args)...); } template ::value> * = nullptr> void emplace(Args &&...args) { if (has_value()) { val().~T(); ::new (valptr()) T(std::forward(args)...); } else { auto tmp = std::move(err()); err().~unexpected(); #ifdef TL_EXPECTED_EXCEPTIONS_ENABLED try { ::new (valptr()) T(std::forward(args)...); this->m_has_val = true; } catch (...) { err() = std::move(tmp); throw; } #else ::new (valptr()) T(std::forward(args)...); this->m_has_val = true; #endif } } template &, Args &&...>::value> * = nullptr> void emplace(std::initializer_list il, Args &&...args) { if (has_value()) { T t(il, std::forward(args)...); val() = std::move(t); } else { err().~unexpected(); ::new (valptr()) T(il, std::forward(args)...); this->m_has_val = true; } } template &, Args &&...>::value> * = nullptr> void emplace(std::initializer_list il, Args &&...args) { if (has_value()) { T t(il, std::forward(args)...); val() = std::move(t); } else { auto tmp = std::move(err()); err().~unexpected(); #ifdef TL_EXPECTED_EXCEPTIONS_ENABLED try { ::new (valptr()) T(il, std::forward(args)...); this->m_has_val = true; } catch (...) { err() = std::move(tmp); throw; } #else ::new (valptr()) T(il, std::forward(args)...); this->m_has_val = true; #endif } } private: using t_is_void = std::true_type; using t_is_not_void = std::false_type; using t_is_nothrow_move_constructible = std::true_type; using move_constructing_t_can_throw = std::false_type; using e_is_nothrow_move_constructible = std::true_type; using move_constructing_e_can_throw = std::false_type; void swap_where_both_have_value(expected & /*rhs*/, t_is_void) noexcept { // swapping void is a no-op } void swap_where_both_have_value(expected &rhs, t_is_not_void) { using std::swap; swap(val(), rhs.val()); } void swap_where_only_one_has_value(expected &rhs, t_is_void) noexcept( std::is_nothrow_move_constructible::value) { ::new (errptr()) unexpected_type(std::move(rhs.err())); rhs.err().~unexpected_type(); std::swap(this->m_has_val, rhs.m_has_val); } void swap_where_only_one_has_value(expected &rhs, t_is_not_void) { swap_where_only_one_has_value_and_t_is_not_void( rhs, typename std::is_nothrow_move_constructible::type{}, typename std::is_nothrow_move_constructible::type{}); } void swap_where_only_one_has_value_and_t_is_not_void( expected &rhs, t_is_nothrow_move_constructible, e_is_nothrow_move_constructible) noexcept { auto temp = std::move(val()); val().~T(); ::new (errptr()) unexpected_type(std::move(rhs.err())); rhs.err().~unexpected_type(); ::new (rhs.valptr()) T(std::move(temp)); std::swap(this->m_has_val, rhs.m_has_val); } void swap_where_only_one_has_value_and_t_is_not_void( expected &rhs, t_is_nothrow_move_constructible, move_constructing_e_can_throw) { auto temp = std::move(val()); val().~T(); #ifdef TL_EXPECTED_EXCEPTIONS_ENABLED try { ::new (errptr()) unexpected_type(std::move(rhs.err())); rhs.err().~unexpected_type(); ::new (rhs.valptr()) T(std::move(temp)); std::swap(this->m_has_val, rhs.m_has_val); } catch (...) { val() = std::move(temp); throw; } #else ::new (errptr()) unexpected_type(std::move(rhs.err())); rhs.err().~unexpected_type(); ::new (rhs.valptr()) T(std::move(temp)); std::swap(this->m_has_val, rhs.m_has_val); #endif } void swap_where_only_one_has_value_and_t_is_not_void( expected &rhs, move_constructing_t_can_throw, e_is_nothrow_move_constructible) { auto temp = std::move(rhs.err()); rhs.err().~unexpected_type(); #ifdef TL_EXPECTED_EXCEPTIONS_ENABLED try { ::new (rhs.valptr()) T(std::move(val())); val().~T(); ::new (errptr()) unexpected_type(std::move(temp)); std::swap(this->m_has_val, rhs.m_has_val); } catch (...) { rhs.err() = std::move(temp); throw; } #else ::new (rhs.valptr()) T(std::move(val())); val().~T(); ::new (errptr()) unexpected_type(std::move(temp)); std::swap(this->m_has_val, rhs.m_has_val); #endif } public: template detail::enable_if_t::value && detail::is_swappable::value && (std::is_nothrow_move_constructible::value || std::is_nothrow_move_constructible::value)> swap(expected &rhs) noexcept( std::is_nothrow_move_constructible::value &&detail::is_nothrow_swappable::value &&std::is_nothrow_move_constructible::value &&detail::is_nothrow_swappable::value) { if (has_value() && rhs.has_value()) { swap_where_both_have_value(rhs, typename std::is_void::type{}); } else if (!has_value() && rhs.has_value()) { rhs.swap(*this); } else if (has_value()) { swap_where_only_one_has_value(rhs, typename std::is_void::type{}); } else { using std::swap; swap(err(), rhs.err()); } } constexpr const T *operator->() const { TL_ASSERT(has_value()); return valptr(); } TL_EXPECTED_11_CONSTEXPR T *operator->() { TL_ASSERT(has_value()); return valptr(); } template ::value> * = nullptr> constexpr const U &operator*() const & { TL_ASSERT(has_value()); return val(); } template ::value> * = nullptr> TL_EXPECTED_11_CONSTEXPR U &operator*() & { TL_ASSERT(has_value()); return val(); } template ::value> * = nullptr> constexpr const U &&operator*() const && { TL_ASSERT(has_value()); return std::move(val()); } template ::value> * = nullptr> TL_EXPECTED_11_CONSTEXPR U &&operator*() && { TL_ASSERT(has_value()); return std::move(val()); } constexpr bool has_value() const noexcept { return this->m_has_val; } constexpr explicit operator bool() const noexcept { return this->m_has_val; } template ::value> * = nullptr> TL_EXPECTED_11_CONSTEXPR const U &value() const & { if (!has_value()) detail::throw_exception(bad_expected_access(err().value())); return val(); } template ::value> * = nullptr> TL_EXPECTED_11_CONSTEXPR U &value() & { if (!has_value()) detail::throw_exception(bad_expected_access(err().value())); return val(); } template ::value> * = nullptr> TL_EXPECTED_11_CONSTEXPR const U &&value() const && { if (!has_value()) detail::throw_exception(bad_expected_access(std::move(err()).value())); return std::move(val()); } template ::value> * = nullptr> TL_EXPECTED_11_CONSTEXPR U &&value() && { if (!has_value()) detail::throw_exception(bad_expected_access(std::move(err()).value())); return std::move(val()); } constexpr const E &error() const & { TL_ASSERT(!has_value()); return err().value(); } TL_EXPECTED_11_CONSTEXPR E &error() & { TL_ASSERT(!has_value()); return err().value(); } constexpr const E &&error() const && { TL_ASSERT(!has_value()); return std::move(err().value()); } TL_EXPECTED_11_CONSTEXPR E &&error() && { TL_ASSERT(!has_value()); return std::move(err().value()); } template constexpr T value_or(U &&v) const & { static_assert(std::is_copy_constructible::value && std::is_convertible::value, "T must be copy-constructible and convertible to from U&&"); return bool(*this) ? **this : static_cast(std::forward(v)); } template TL_EXPECTED_11_CONSTEXPR T value_or(U &&v) && { static_assert(std::is_move_constructible::value && std::is_convertible::value, "T must be move-constructible and convertible to from U&&"); return bool(*this) ? std::move(**this) : static_cast(std::forward(v)); } }; namespace detail { template using exp_t = typename detail::decay_t::value_type; template using err_t = typename detail::decay_t::error_type; template using ret_t = expected>; #ifdef TL_EXPECTED_CXX14 template >::value> * = nullptr, class Ret = decltype(detail::invoke(std::declval(), *std::declval()))> constexpr auto and_then_impl(Exp &&exp, F &&f) { static_assert(detail::is_expected::value, "F must return an expected"); return exp.has_value() ? detail::invoke(std::forward(f), *std::forward(exp)) : Ret(unexpect, std::forward(exp).error()); } template >::value> * = nullptr, class Ret = decltype(detail::invoke(std::declval()))> constexpr auto and_then_impl(Exp &&exp, F &&f) { static_assert(detail::is_expected::value, "F must return an expected"); return exp.has_value() ? detail::invoke(std::forward(f)) : Ret(unexpect, std::forward(exp).error()); } #else template struct TC; template (), *std::declval())), detail::enable_if_t>::value> * = nullptr> auto and_then_impl(Exp &&exp, F &&f) -> Ret { static_assert(detail::is_expected::value, "F must return an expected"); return exp.has_value() ? detail::invoke(std::forward(f), *std::forward(exp)) : Ret(unexpect, std::forward(exp).error()); } template ())), detail::enable_if_t>::value> * = nullptr> constexpr auto and_then_impl(Exp &&exp, F &&f) -> Ret { static_assert(detail::is_expected::value, "F must return an expected"); return exp.has_value() ? detail::invoke(std::forward(f)) : Ret(unexpect, std::forward(exp).error()); } #endif #ifdef TL_EXPECTED_CXX14 template >::value> * = nullptr, class Ret = decltype(detail::invoke(std::declval(), *std::declval())), detail::enable_if_t::value> * = nullptr> constexpr auto expected_map_impl(Exp &&exp, F &&f) { using result = ret_t>; return exp.has_value() ? result(detail::invoke(std::forward(f), *std::forward(exp))) : result(unexpect, std::forward(exp).error()); } template >::value> * = nullptr, class Ret = decltype(detail::invoke(std::declval(), *std::declval())), detail::enable_if_t::value> * = nullptr> auto expected_map_impl(Exp &&exp, F &&f) { using result = expected>; if (exp.has_value()) { detail::invoke(std::forward(f), *std::forward(exp)); return result(); } return result(unexpect, std::forward(exp).error()); } template >::value> * = nullptr, class Ret = decltype(detail::invoke(std::declval())), detail::enable_if_t::value> * = nullptr> constexpr auto expected_map_impl(Exp &&exp, F &&f) { using result = ret_t>; return exp.has_value() ? result(detail::invoke(std::forward(f))) : result(unexpect, std::forward(exp).error()); } template >::value> * = nullptr, class Ret = decltype(detail::invoke(std::declval())), detail::enable_if_t::value> * = nullptr> auto expected_map_impl(Exp &&exp, F &&f) { using result = expected>; if (exp.has_value()) { detail::invoke(std::forward(f)); return result(); } return result(unexpect, std::forward(exp).error()); } #else template >::value> * = nullptr, class Ret = decltype(detail::invoke(std::declval(), *std::declval())), detail::enable_if_t::value> * = nullptr> constexpr auto expected_map_impl(Exp &&exp, F &&f) -> ret_t> { using result = ret_t>; return exp.has_value() ? result(detail::invoke(std::forward(f), *std::forward(exp))) : result(unexpect, std::forward(exp).error()); } template >::value> * = nullptr, class Ret = decltype(detail::invoke(std::declval(), *std::declval())), detail::enable_if_t::value> * = nullptr> auto expected_map_impl(Exp &&exp, F &&f) -> expected> { if (exp.has_value()) { detail::invoke(std::forward(f), *std::forward(exp)); return {}; } return unexpected>(std::forward(exp).error()); } template >::value> * = nullptr, class Ret = decltype(detail::invoke(std::declval())), detail::enable_if_t::value> * = nullptr> constexpr auto expected_map_impl(Exp &&exp, F &&f) -> ret_t> { using result = ret_t>; return exp.has_value() ? result(detail::invoke(std::forward(f))) : result(unexpect, std::forward(exp).error()); } template >::value> * = nullptr, class Ret = decltype(detail::invoke(std::declval())), detail::enable_if_t::value> * = nullptr> auto expected_map_impl(Exp &&exp, F &&f) -> expected> { if (exp.has_value()) { detail::invoke(std::forward(f)); return {}; } return unexpected>(std::forward(exp).error()); } #endif #if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \ !defined(TL_EXPECTED_GCC54) && !defined(TL_EXPECTED_GCC55) template >::value> * = nullptr, class Ret = decltype(detail::invoke(std::declval(), std::declval().error())), detail::enable_if_t::value> * = nullptr> constexpr auto map_error_impl(Exp &&exp, F &&f) { using result = expected, detail::decay_t>; return exp.has_value() ? result(*std::forward(exp)) : result(unexpect, detail::invoke(std::forward(f), std::forward(exp).error())); } template >::value> * = nullptr, class Ret = decltype(detail::invoke(std::declval(), std::declval().error())), detail::enable_if_t::value> * = nullptr> auto map_error_impl(Exp &&exp, F &&f) { using result = expected, monostate>; if (exp.has_value()) { return result(*std::forward(exp)); } detail::invoke(std::forward(f), std::forward(exp).error()); return result(unexpect, monostate{}); } template >::value> * = nullptr, class Ret = decltype(detail::invoke(std::declval(), std::declval().error())), detail::enable_if_t::value> * = nullptr> constexpr auto map_error_impl(Exp &&exp, F &&f) { using result = expected, detail::decay_t>; return exp.has_value() ? result() : result(unexpect, detail::invoke(std::forward(f), std::forward(exp).error())); } template >::value> * = nullptr, class Ret = decltype(detail::invoke(std::declval(), std::declval().error())), detail::enable_if_t::value> * = nullptr> auto map_error_impl(Exp &&exp, F &&f) { using result = expected, monostate>; if (exp.has_value()) { return result(); } detail::invoke(std::forward(f), std::forward(exp).error()); return result(unexpect, monostate{}); } #else template >::value> * = nullptr, class Ret = decltype(detail::invoke(std::declval(), std::declval().error())), detail::enable_if_t::value> * = nullptr> constexpr auto map_error_impl(Exp &&exp, F &&f) -> expected, detail::decay_t> { using result = expected, detail::decay_t>; return exp.has_value() ? result(*std::forward(exp)) : result(unexpect, detail::invoke(std::forward(f), std::forward(exp).error())); } template >::value> * = nullptr, class Ret = decltype(detail::invoke(std::declval(), std::declval().error())), detail::enable_if_t::value> * = nullptr> auto map_error_impl(Exp &&exp, F &&f) -> expected, monostate> { using result = expected, monostate>; if (exp.has_value()) { return result(*std::forward(exp)); } detail::invoke(std::forward(f), std::forward(exp).error()); return result(unexpect, monostate{}); } template >::value> * = nullptr, class Ret = decltype(detail::invoke(std::declval(), std::declval().error())), detail::enable_if_t::value> * = nullptr> constexpr auto map_error_impl(Exp &&exp, F &&f) -> expected, detail::decay_t> { using result = expected, detail::decay_t>; return exp.has_value() ? result() : result(unexpect, detail::invoke(std::forward(f), std::forward(exp).error())); } template >::value> * = nullptr, class Ret = decltype(detail::invoke(std::declval(), std::declval().error())), detail::enable_if_t::value> * = nullptr> auto map_error_impl(Exp &&exp, F &&f) -> expected, monostate> { using result = expected, monostate>; if (exp.has_value()) { return result(); } detail::invoke(std::forward(f), std::forward(exp).error()); return result(unexpect, monostate{}); } #endif #ifdef TL_EXPECTED_CXX14 template (), std::declval().error())), detail::enable_if_t::value> * = nullptr> constexpr auto or_else_impl(Exp &&exp, F &&f) { static_assert(detail::is_expected::value, "F must return an expected"); return exp.has_value() ? std::forward(exp) : detail::invoke(std::forward(f), std::forward(exp).error()); } template (), std::declval().error())), detail::enable_if_t::value> * = nullptr> detail::decay_t or_else_impl(Exp &&exp, F &&f) { return exp.has_value() ? std::forward(exp) : (detail::invoke(std::forward(f), std::forward(exp).error()), std::forward(exp)); } #else template (), std::declval().error())), detail::enable_if_t::value> * = nullptr> auto or_else_impl(Exp &&exp, F &&f) -> Ret { static_assert(detail::is_expected::value, "F must return an expected"); return exp.has_value() ? std::forward(exp) : detail::invoke(std::forward(f), std::forward(exp).error()); } template (), std::declval().error())), detail::enable_if_t::value> * = nullptr> detail::decay_t or_else_impl(Exp &&exp, F &&f) { return exp.has_value() ? std::forward(exp) : (detail::invoke(std::forward(f), std::forward(exp).error()), std::forward(exp)); } #endif } // namespace detail template constexpr bool operator==(const expected &lhs, const expected &rhs) { return (lhs.has_value() != rhs.has_value()) ? false : (!lhs.has_value() ? lhs.error() == rhs.error() : *lhs == *rhs); } template constexpr bool operator!=(const expected &lhs, const expected &rhs) { return (lhs.has_value() != rhs.has_value()) ? true : (!lhs.has_value() ? lhs.error() != rhs.error() : *lhs != *rhs); } template constexpr bool operator==(const expected &lhs, const expected &rhs) { return (lhs.has_value() != rhs.has_value()) ? false : (!lhs.has_value() ? lhs.error() == rhs.error() : true); } template constexpr bool operator!=(const expected &lhs, const expected &rhs) { return (lhs.has_value() != rhs.has_value()) ? true : (!lhs.has_value() ? lhs.error() == rhs.error() : false); } template constexpr bool operator==(const expected &x, const U &v) { return x.has_value() ? *x == v : false; } template constexpr bool operator==(const U &v, const expected &x) { return x.has_value() ? *x == v : false; } template constexpr bool operator!=(const expected &x, const U &v) { return x.has_value() ? *x != v : true; } template constexpr bool operator!=(const U &v, const expected &x) { return x.has_value() ? *x != v : true; } template constexpr bool operator==(const expected &x, const unexpected &e) { return x.has_value() ? false : x.error() == e.value(); } template constexpr bool operator==(const unexpected &e, const expected &x) { return x.has_value() ? false : x.error() == e.value(); } template constexpr bool operator!=(const expected &x, const unexpected &e) { return x.has_value() ? true : x.error() != e.value(); } template constexpr bool operator!=(const unexpected &e, const expected &x) { return x.has_value() ? true : x.error() != e.value(); } template ::value || std::is_move_constructible::value) && detail::is_swappable::value && std::is_move_constructible::value && detail::is_swappable::value> * = nullptr> void swap(expected &lhs, expected &rhs) noexcept(noexcept(lhs.swap(rhs))) { lhs.swap(rhs); } } // namespace tl #endif expected-1.1.0/tests/000077500000000000000000000000001440431670300144565ustar00rootroot00000000000000expected-1.1.0/tests/assertions.cpp000066400000000000000000000010371440431670300173550ustar00rootroot00000000000000#include #include #define TL_ASSERT(cond) if (!(cond)) { throw std::runtime_error(std::string("assertion failure")); } #include TEST_CASE("Assertions", "[assertions]") { tl::expected o1 = 42; REQUIRE_THROWS_WITH(o1.error(), "assertion failure"); tl::expected o2 {tl::unexpect, 0}; REQUIRE_THROWS_WITH(*o2, "assertion failure"); struct foo { int bar; }; tl::expected o3 {tl::unexpect, 0}; REQUIRE_THROWS_WITH(o3->bar, "assertion failure"); } expected-1.1.0/tests/assignment.cpp000066400000000000000000000032501440431670300173320ustar00rootroot00000000000000#include #include TEST_CASE("Simple assignment", "[assignment.simple]") { tl::expected e1 = 42; tl::expected e2 = 17; tl::expected e3 = 21; tl::expected e4 = tl::make_unexpected(42); tl::expected e5 = tl::make_unexpected(17); tl::expected e6 = tl::make_unexpected(21); e1 = e2; REQUIRE(e1); REQUIRE(*e1 == 17); REQUIRE(e2); REQUIRE(*e2 == 17); e1 = std::move(e2); REQUIRE(e1); REQUIRE(*e1 == 17); REQUIRE(e2); REQUIRE(*e2 == 17); e1 = 42; REQUIRE(e1); REQUIRE(*e1 == 42); auto unex = tl::make_unexpected(12); e1 = unex; REQUIRE(!e1); REQUIRE(e1.error() == 12); e1 = tl::make_unexpected(42); REQUIRE(!e1); REQUIRE(e1.error() == 42); e1 = e3; REQUIRE(e1); REQUIRE(*e1 == 21); e4 = e5; REQUIRE(!e4); REQUIRE(e4.error() == 17); e4 = std::move(e6); REQUIRE(!e4); REQUIRE(e4.error() == 21); e4 = e1; REQUIRE(e4); REQUIRE(*e4 == 21); } TEST_CASE("Assignment deletion", "[assignment.deletion]") { struct has_all { has_all() = default; has_all(const has_all &) = default; has_all(has_all &&) noexcept = default; has_all &operator=(const has_all &) = default; }; tl::expected e1 = {}; tl::expected e2 = {}; e1 = e2; struct except_move { except_move() = default; except_move(const except_move &) = default; except_move(except_move &&) noexcept(false){}; except_move &operator=(const except_move &) = default; }; tl::expected e3 = {}; tl::expected e4 = {}; // e3 = e4; should not compile } expected-1.1.0/tests/bases.cpp000066400000000000000000000163671440431670300162740ustar00rootroot00000000000000#include #include #include // Old versions of GCC don't have the correct trait names. Could fix them up if needs be. #if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && \ !defined(__clang__)) // nothing for now #else TEST_CASE("Triviality", "[bases.triviality]") { REQUIRE(std::is_trivially_copy_constructible>::value); REQUIRE(std::is_trivially_copy_assignable>::value); REQUIRE(std::is_trivially_move_constructible>::value); REQUIRE(std::is_trivially_move_assignable>::value); REQUIRE(std::is_trivially_destructible>::value); REQUIRE(std::is_trivially_copy_constructible>::value); REQUIRE(std::is_trivially_move_constructible>::value); REQUIRE(std::is_trivially_destructible>::value); { struct T { T(const T&) = default; T(T&&) = default; T& operator=(const T&) = default; T& operator=(T&&) = default; ~T() = default; }; REQUIRE(std::is_trivially_copy_constructible>::value); REQUIRE(std::is_trivially_copy_assignable>::value); REQUIRE(std::is_trivially_move_constructible>::value); REQUIRE(std::is_trivially_move_assignable>::value); REQUIRE(std::is_trivially_destructible>::value); } { struct T { T(const T&){} T(T&&) {} T& operator=(const T&) { return *this; } T& operator=(T&&) { return *this; } ~T(){} }; REQUIRE(!std::is_trivially_copy_constructible>::value); REQUIRE(!std::is_trivially_copy_assignable>::value); REQUIRE(!std::is_trivially_move_constructible>::value); REQUIRE(!std::is_trivially_move_assignable>::value); REQUIRE(!std::is_trivially_destructible>::value); } } TEST_CASE("Deletion", "[bases.deletion]") { REQUIRE(std::is_copy_constructible>::value); REQUIRE(std::is_copy_assignable>::value); REQUIRE(std::is_move_constructible>::value); REQUIRE(std::is_move_assignable>::value); REQUIRE(std::is_destructible>::value); { struct T { T()=default; }; REQUIRE(std::is_default_constructible>::value); } { struct T { T(int){} }; REQUIRE(!std::is_default_constructible>::value); } { struct T { T(const T&) = default; T(T&&) = default; T& operator=(const T&) = default; T& operator=(T&&) = default; ~T() = default; }; REQUIRE(std::is_copy_constructible>::value); REQUIRE(std::is_copy_assignable>::value); REQUIRE(std::is_move_constructible>::value); REQUIRE(std::is_move_assignable>::value); REQUIRE(std::is_destructible>::value); } { struct T { T(const T&)=delete; T(T&&)=delete; T& operator=(const T&)=delete; T& operator=(T&&)=delete; }; REQUIRE(!std::is_copy_constructible>::value); REQUIRE(!std::is_copy_assignable>::value); REQUIRE(!std::is_move_constructible>::value); REQUIRE(!std::is_move_assignable>::value); } { struct T { T(const T&)=delete; T(T&&)=default; T& operator=(const T&)=delete; T& operator=(T&&)=default; }; REQUIRE(!std::is_copy_constructible>::value); REQUIRE(!std::is_copy_assignable>::value); REQUIRE(std::is_move_constructible>::value); REQUIRE(std::is_move_assignable>::value); } { struct T { T(const T&)=default; T(T&&)=delete; T& operator=(const T&)=default; T& operator=(T&&)=delete; }; REQUIRE(std::is_copy_constructible>::value); REQUIRE(std::is_copy_assignable>::value); } { tl::expected e; REQUIRE(std::is_default_constructible::value); REQUIRE(std::is_copy_constructible::value); REQUIRE(std::is_move_constructible::value); REQUIRE(std::is_copy_assignable::value); REQUIRE(std::is_move_assignable::value); REQUIRE(TL_EXPECTED_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e))::value); REQUIRE(TL_EXPECTED_IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e))::value); # if !defined(TL_EXPECTED_GCC49) REQUIRE(std::is_trivially_move_constructible::value); REQUIRE(std::is_trivially_move_assignable::value); # endif } { tl::expected e; REQUIRE(std::is_default_constructible::value); REQUIRE(std::is_copy_constructible::value); REQUIRE(std::is_move_constructible::value); REQUIRE(std::is_copy_assignable::value); REQUIRE(std::is_move_assignable::value); REQUIRE(!TL_EXPECTED_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e))::value); REQUIRE(!TL_EXPECTED_IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e))::value); # if !defined(TL_EXPECTED_GCC49) REQUIRE(!std::is_trivially_move_constructible::value); REQUIRE(!std::is_trivially_move_assignable::value); # endif } { tl::expected e; REQUIRE(std::is_default_constructible::value); REQUIRE(std::is_copy_constructible::value); REQUIRE(std::is_move_constructible::value); REQUIRE(std::is_copy_assignable::value); REQUIRE(std::is_move_assignable::value); REQUIRE(!TL_EXPECTED_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e))::value); REQUIRE(!TL_EXPECTED_IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e))::value); # if !defined(TL_EXPECTED_GCC49) REQUIRE(!std::is_trivially_move_constructible::value); REQUIRE(!std::is_trivially_move_assignable::value); # endif } { tl::expected e; REQUIRE(std::is_default_constructible::value); REQUIRE(std::is_copy_constructible::value); REQUIRE(std::is_move_constructible::value); REQUIRE(std::is_copy_assignable::value); REQUIRE(std::is_move_assignable::value); REQUIRE(!TL_EXPECTED_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e))::value); REQUIRE(!TL_EXPECTED_IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e))::value); # if !defined(TL_EXPECTED_GCC49) REQUIRE(!std::is_trivially_move_constructible::value); REQUIRE(!std::is_trivially_move_assignable::value); # endif } } #endif expected-1.1.0/tests/constexpr.cpp000066400000000000000000000001551440431670300172100ustar00rootroot00000000000000#include #include TEST_CASE("Constexpr", "[constexpr]") { //TODO } expected-1.1.0/tests/constructors.cpp000066400000000000000000000103051440431670300177310ustar00rootroot00000000000000#include #include #include #include #include struct takes_init_and_variadic { std::vector v; std::tuple t; template takes_init_and_variadic(std::initializer_list l, Args &&... args) : v(l), t(std::forward(args)...) {} }; TEST_CASE("Constructors", "[constructors]") { { tl::expected e; REQUIRE(e); REQUIRE(e == 0); } { tl::expected e = tl::make_unexpected(0); REQUIRE(!e); REQUIRE(e.error() == 0); } { tl::expected e (tl::unexpect, 0); REQUIRE(!e); REQUIRE(e.error() == 0); } { tl::expected e (tl::in_place, 42); REQUIRE(e); REQUIRE(e == 42); } { tl::expected,int> e (tl::in_place, {0,1}); REQUIRE(e); REQUIRE((*e)[0] == 0); REQUIRE((*e)[1] == 1); } { tl::expected,int> e (tl::in_place, 0, 1); REQUIRE(e); REQUIRE(std::get<0>(*e) == 0); REQUIRE(std::get<1>(*e) == 1); } { tl::expected e (tl::in_place, {0,1}, 2, 3); REQUIRE(e); REQUIRE(e->v[0] == 0); REQUIRE(e->v[1] == 1); REQUIRE(std::get<0>(e->t) == 2); REQUIRE(std::get<1>(e->t) == 3); } { tl::expected e; REQUIRE(std::is_default_constructible::value); REQUIRE(std::is_copy_constructible::value); REQUIRE(std::is_move_constructible::value); REQUIRE(std::is_copy_assignable::value); REQUIRE(std::is_move_assignable::value); REQUIRE(TL_EXPECTED_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e))::value); REQUIRE(TL_EXPECTED_IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e))::value); # if !defined(TL_EXPECTED_GCC49) REQUIRE(std::is_trivially_move_constructible::value); REQUIRE(std::is_trivially_move_assignable::value); # endif } { tl::expected e; REQUIRE(std::is_default_constructible::value); REQUIRE(std::is_copy_constructible::value); REQUIRE(std::is_move_constructible::value); REQUIRE(std::is_copy_assignable::value); REQUIRE(std::is_move_assignable::value); REQUIRE(!TL_EXPECTED_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e))::value); REQUIRE(!TL_EXPECTED_IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e))::value); # if !defined(TL_EXPECTED_GCC49) REQUIRE(!std::is_trivially_move_constructible::value); REQUIRE(!std::is_trivially_move_assignable::value); # endif } { tl::expected e; REQUIRE(std::is_default_constructible::value); REQUIRE(std::is_copy_constructible::value); REQUIRE(std::is_move_constructible::value); REQUIRE(std::is_copy_assignable::value); REQUIRE(std::is_move_assignable::value); REQUIRE(!TL_EXPECTED_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e))::value); REQUIRE(!TL_EXPECTED_IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e))::value); # if !defined(TL_EXPECTED_GCC49) REQUIRE(!std::is_trivially_move_constructible::value); REQUIRE(!std::is_trivially_move_assignable::value); # endif } { tl::expected e; REQUIRE(std::is_default_constructible::value); REQUIRE(std::is_copy_constructible::value); REQUIRE(std::is_move_constructible::value); REQUIRE(std::is_copy_assignable::value); REQUIRE(std::is_move_assignable::value); REQUIRE(!TL_EXPECTED_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e))::value); REQUIRE(!TL_EXPECTED_IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e))::value); # if !defined(TL_EXPECTED_GCC49) REQUIRE(!std::is_trivially_move_constructible::value); REQUIRE(!std::is_trivially_move_assignable::value); # endif } { tl::expected e; REQUIRE(e); } { tl::expected e (tl::unexpect, 42); REQUIRE(!e); REQUIRE(e.error() == 42); } } expected-1.1.0/tests/emplace.cpp000066400000000000000000000022261440431670300165720ustar00rootroot00000000000000#include #include #include #include #include namespace { struct takes_init_and_variadic { std::vector v; std::tuple t; template takes_init_and_variadic(std::initializer_list l, Args &&... args) : v(l), t(std::forward(args)...) {} }; } TEST_CASE("Emplace", "[emplace]") { { tl::expected,int> e; e.emplace(new int{42}); REQUIRE(e); REQUIRE(**e == 42); } { tl::expected,int> e; e.emplace({0,1}); REQUIRE(e); REQUIRE((*e)[0] == 0); REQUIRE((*e)[1] == 1); } { tl::expected,int> e; e.emplace(2,3); REQUIRE(e); REQUIRE(std::get<0>(*e) == 2); REQUIRE(std::get<1>(*e) == 3); } { tl::expected e = tl::make_unexpected(0); e.emplace({0,1}, 2, 3); REQUIRE(e); REQUIRE(e->v[0] == 0); REQUIRE(e->v[1] == 1); REQUIRE(std::get<0>(e->t) == 2); REQUIRE(std::get<1>(e->t) == 3); } } expected-1.1.0/tests/extensions.cpp000066400000000000000000000433571440431670300173750ustar00rootroot00000000000000#include #include #define TOKENPASTE(x, y) x##y #define TOKENPASTE2(x, y) TOKENPASTE(x, y) #undef STATIC_REQUIRE #define STATIC_REQUIRE(e) \ constexpr bool TOKENPASTE2(rqure, __LINE__) = e; \ (void)TOKENPASTE2(rqure, __LINE__); \ REQUIRE(e); TEST_CASE("Map extensions", "[extensions.map]") { auto mul2 = [](int a) { return a * 2; }; auto ret_void = [](int a) { (void)a; }; { tl::expected e = 21; auto ret = e.map(mul2); REQUIRE(ret); REQUIRE(*ret == 42); } { const tl::expected e = 21; auto ret = e.map(mul2); REQUIRE(ret); REQUIRE(*ret == 42); } { tl::expected e = 21; auto ret = std::move(e).map(mul2); REQUIRE(ret); REQUIRE(*ret == 42); } { const tl::expected e = 21; auto ret = std::move(e).map(mul2); REQUIRE(ret); REQUIRE(*ret == 42); } { tl::expected e(tl::unexpect, 21); auto ret = e.map(mul2); REQUIRE(!ret); REQUIRE(ret.error() == 21); } { const tl::expected e(tl::unexpect, 21); auto ret = e.map(mul2); REQUIRE(!ret); REQUIRE(ret.error() == 21); } { tl::expected e(tl::unexpect, 21); auto ret = std::move(e).map(mul2); REQUIRE(!ret); REQUIRE(ret.error() == 21); } { const tl::expected e(tl::unexpect, 21); auto ret = std::move(e).map(mul2); REQUIRE(!ret); REQUIRE(ret.error() == 21); } { tl::expected e = 21; auto ret = e.map(ret_void); REQUIRE(ret); STATIC_REQUIRE( (std::is_same>::value)); } { const tl::expected e = 21; auto ret = e.map(ret_void); REQUIRE(ret); STATIC_REQUIRE( (std::is_same>::value)); } { tl::expected e = 21; auto ret = std::move(e).map(ret_void); REQUIRE(ret); STATIC_REQUIRE( (std::is_same>::value)); } { const tl::expected e = 21; auto ret = std::move(e).map(ret_void); REQUIRE(ret); STATIC_REQUIRE( (std::is_same>::value)); } { tl::expected e(tl::unexpect, 21); auto ret = e.map(ret_void); REQUIRE(!ret); STATIC_REQUIRE( (std::is_same>::value)); } { const tl::expected e(tl::unexpect, 21); auto ret = e.map(ret_void); REQUIRE(!ret); STATIC_REQUIRE( (std::is_same>::value)); } { tl::expected e(tl::unexpect, 21); auto ret = std::move(e).map(ret_void); REQUIRE(!ret); STATIC_REQUIRE( (std::is_same>::value)); } { const tl::expected e(tl::unexpect, 21); auto ret = std::move(e).map(ret_void); REQUIRE(!ret); STATIC_REQUIRE( (std::is_same>::value)); } // mapping functions which return references { tl::expected e(42); auto ret = e.map([](int& i) -> int& { return i; }); REQUIRE(ret); REQUIRE(ret == 42); } } TEST_CASE("Map error extensions", "[extensions.map_error]") { auto mul2 = [](int a) { return a * 2; }; auto ret_void = [](int a) { (void)a; }; { tl::expected e = 21; auto ret = e.map_error(mul2); REQUIRE(ret); REQUIRE(*ret == 21); } { const tl::expected e = 21; auto ret = e.map_error(mul2); REQUIRE(ret); REQUIRE(*ret == 21); } { tl::expected e = 21; auto ret = std::move(e).map_error(mul2); REQUIRE(ret); REQUIRE(*ret == 21); } { const tl::expected e = 21; auto ret = std::move(e).map_error(mul2); REQUIRE(ret); REQUIRE(*ret == 21); } { tl::expected e(tl::unexpect, 21); auto ret = e.map_error(mul2); REQUIRE(!ret); REQUIRE(ret.error() == 42); } { const tl::expected e(tl::unexpect, 21); auto ret = e.map_error(mul2); REQUIRE(!ret); REQUIRE(ret.error() == 42); } { tl::expected e(tl::unexpect, 21); auto ret = std::move(e).map_error(mul2); REQUIRE(!ret); REQUIRE(ret.error() == 42); } { const tl::expected e(tl::unexpect, 21); auto ret = std::move(e).map_error(mul2); REQUIRE(!ret); REQUIRE(ret.error() == 42); } { tl::expected e = 21; auto ret = e.map_error(ret_void); REQUIRE(ret); } { const tl::expected e = 21; auto ret = e.map_error(ret_void); REQUIRE(ret); } { tl::expected e = 21; auto ret = std::move(e).map_error(ret_void); REQUIRE(ret); } { const tl::expected e = 21; auto ret = std::move(e).map_error(ret_void); REQUIRE(ret); } { tl::expected e(tl::unexpect, 21); auto ret = e.map_error(ret_void); REQUIRE(!ret); } { const tl::expected e(tl::unexpect, 21); auto ret = e.map_error(ret_void); REQUIRE(!ret); } { tl::expected e(tl::unexpect, 21); auto ret = std::move(e).map_error(ret_void); REQUIRE(!ret); } { const tl::expected e(tl::unexpect, 21); auto ret = std::move(e).map_error(ret_void); REQUIRE(!ret); } } TEST_CASE("And then extensions", "[extensions.and_then]") { auto succeed = [](int a) { (void)a; return tl::expected(21 * 2); }; auto fail = [](int a) { (void)a; return tl::expected(tl::unexpect, 17); }; { tl::expected e = 21; auto ret = e.and_then(succeed); REQUIRE(ret); REQUIRE(*ret == 42); } { const tl::expected e = 21; auto ret = e.and_then(succeed); REQUIRE(ret); REQUIRE(*ret == 42); } { tl::expected e = 21; auto ret = std::move(e).and_then(succeed); REQUIRE(ret); REQUIRE(*ret == 42); } { const tl::expected e = 21; auto ret = std::move(e).and_then(succeed); REQUIRE(ret); REQUIRE(*ret == 42); } { tl::expected e = 21; auto ret = e.and_then(fail); REQUIRE(!ret); REQUIRE(ret.error() == 17); } { const tl::expected e = 21; auto ret = e.and_then(fail); REQUIRE(!ret); REQUIRE(ret.error() == 17); } { tl::expected e = 21; auto ret = std::move(e).and_then(fail); REQUIRE(!ret); REQUIRE(ret.error() == 17); } { const tl::expected e = 21; auto ret = std::move(e).and_then(fail); REQUIRE(!ret); REQUIRE(ret.error() == 17); } { tl::expected e(tl::unexpect, 21); auto ret = e.and_then(succeed); REQUIRE(!ret); REQUIRE(ret.error() == 21); } { const tl::expected e(tl::unexpect, 21); auto ret = e.and_then(succeed); REQUIRE(!ret); REQUIRE(ret.error() == 21); } { tl::expected e(tl::unexpect, 21); auto ret = std::move(e).and_then(succeed); REQUIRE(!ret); REQUIRE(ret.error() == 21); } { const tl::expected e(tl::unexpect, 21); auto ret = std::move(e).and_then(succeed); REQUIRE(!ret); REQUIRE(ret.error() == 21); } { tl::expected e(tl::unexpect, 21); auto ret = e.and_then(fail); REQUIRE(!ret); REQUIRE(ret.error() == 21); } { const tl::expected e(tl::unexpect, 21); auto ret = e.and_then(fail); REQUIRE(!ret); REQUIRE(ret.error() == 21); } { tl::expected e(tl::unexpect, 21); auto ret = std::move(e).and_then(fail); REQUIRE(!ret); REQUIRE(ret.error() == 21); } { const tl::expected e(tl::unexpect, 21); auto ret = std::move(e).and_then(fail); REQUIRE(!ret); REQUIRE(ret.error() == 21); } } TEST_CASE("or_else", "[extensions.or_else]") { using eptr = std::unique_ptr; auto succeed = [](int a) { (void)a; return tl::expected(21 * 2); }; auto succeedptr = [](eptr e) { (void)e; return tl::expected(21*2);}; auto fail = [](int a) { (void)a; return tl::expected(tl::unexpect, 17);}; auto failptr = [](eptr e) { *e = 17;return tl::expected(tl::unexpect, std::move(e));}; auto failvoid = [](int) {}; auto failvoidptr = [](const eptr&) { /* don't consume */}; auto consumeptr = [](eptr) {}; auto make_u_int = [](int n) { return std::unique_ptr(new int(n));}; { tl::expected e = 21; auto ret = e.or_else(succeed); REQUIRE(ret); REQUIRE(*ret == 21); } { const tl::expected e = 21; auto ret = e.or_else(succeed); REQUIRE(ret); REQUIRE(*ret == 21); } { tl::expected e = 21; auto ret = std::move(e).or_else(succeed); REQUIRE(ret); REQUIRE(*ret == 21); } { tl::expected e = 21; auto ret = std::move(e).or_else(succeedptr); REQUIRE(ret); REQUIRE(*ret == 21); } { const tl::expected e = 21; auto ret = std::move(e).or_else(succeed); REQUIRE(ret); REQUIRE(*ret == 21); } { tl::expected e = 21; auto ret = e.or_else(fail); REQUIRE(ret); REQUIRE(*ret == 21); } { const tl::expected e = 21; auto ret = e.or_else(fail); REQUIRE(ret); REQUIRE(*ret == 21); } { tl::expected e = 21; auto ret = std::move(e).or_else(fail); REQUIRE(ret); REQUIRE(ret == 21); } { tl::expected e = 21; auto ret = std::move(e).or_else(failptr); REQUIRE(ret); REQUIRE(ret == 21); } { const tl::expected e = 21; auto ret = std::move(e).or_else(fail); REQUIRE(ret); REQUIRE(*ret == 21); } { tl::expected e(tl::unexpect, 21); auto ret = e.or_else(succeed); REQUIRE(ret); REQUIRE(*ret == 42); } { const tl::expected e(tl::unexpect, 21); auto ret = e.or_else(succeed); REQUIRE(ret); REQUIRE(*ret == 42); } { tl::expected e(tl::unexpect, 21); auto ret = std::move(e).or_else(succeed); REQUIRE(ret); REQUIRE(*ret == 42); } { tl::expected e(tl::unexpect, make_u_int(21)); auto ret = std::move(e).or_else(succeedptr); REQUIRE(ret); REQUIRE(*ret == 42); } { const tl::expected e(tl::unexpect, 21); auto ret = std::move(e).or_else(succeed); REQUIRE(ret); REQUIRE(*ret == 42); } { tl::expected e(tl::unexpect, 21); auto ret = e.or_else(fail); REQUIRE(!ret); REQUIRE(ret.error() == 17); } { tl::expected e(tl::unexpect, 21); auto ret = e.or_else(failvoid); REQUIRE(!ret); REQUIRE(ret.error() == 21); } { const tl::expected e(tl::unexpect, 21); auto ret = e.or_else(fail); REQUIRE(!ret); REQUIRE(ret.error() == 17); } { const tl::expected e(tl::unexpect, 21); auto ret = e.or_else(failvoid); REQUIRE(!ret); REQUIRE(ret.error() == 21); } { tl::expected e(tl::unexpect, 21); auto ret = std::move(e).or_else(fail); REQUIRE(!ret); REQUIRE(ret.error() == 17); } { tl::expected e(tl::unexpect, 21); auto ret = std::move(e).or_else(failvoid); REQUIRE(!ret); REQUIRE(ret.error() == 21); } { tl::expected e(tl::unexpect, make_u_int(21)); auto ret = std::move(e).or_else(failvoidptr); REQUIRE(!ret); REQUIRE(*ret.error() == 21); } { tl::expected e(tl::unexpect, make_u_int(21)); auto ret = std::move(e).or_else(consumeptr); REQUIRE(!ret); REQUIRE(ret.error() == nullptr); } { const tl::expected e(tl::unexpect, 21); auto ret = std::move(e).or_else(fail); REQUIRE(!ret); REQUIRE(ret.error() == 17); } { const tl::expected e(tl::unexpect, 21); auto ret = std::move(e).or_else(failvoid); REQUIRE(!ret); REQUIRE(ret.error() == 21); } } TEST_CASE("Transform extensions", "[extensions.tronsfarm]") { auto mul2 = [](int a) { return a * 2; }; auto ret_void = [](int a) { (void)a; }; { tl::expected e = 21; auto ret = e.transform(mul2); REQUIRE(ret); REQUIRE(*ret == 42); } { const tl::expected e = 21; auto ret = e.transform(mul2); REQUIRE(ret); REQUIRE(*ret == 42); } { tl::expected e = 21; auto ret = std::move(e).transform(mul2); REQUIRE(ret); REQUIRE(*ret == 42); } { const tl::expected e = 21; auto ret = std::move(e).transform(mul2); REQUIRE(ret); REQUIRE(*ret == 42); } { tl::expected e(tl::unexpect, 21); auto ret = e.transform(mul2); REQUIRE(!ret); REQUIRE(ret.error() == 21); } { const tl::expected e(tl::unexpect, 21); auto ret = e.transform(mul2); REQUIRE(!ret); REQUIRE(ret.error() == 21); } { tl::expected e(tl::unexpect, 21); auto ret = std::move(e).transform(mul2); REQUIRE(!ret); REQUIRE(ret.error() == 21); } { const tl::expected e(tl::unexpect, 21); auto ret = std::move(e).transform(mul2); REQUIRE(!ret); REQUIRE(ret.error() == 21); } { tl::expected e = 21; auto ret = e.transform(ret_void); REQUIRE(ret); STATIC_REQUIRE( (std::is_same>::value)); } { const tl::expected e = 21; auto ret = e.transform(ret_void); REQUIRE(ret); STATIC_REQUIRE( (std::is_same>::value)); } { tl::expected e = 21; auto ret = std::move(e).transform(ret_void); REQUIRE(ret); STATIC_REQUIRE( (std::is_same>::value)); } { const tl::expected e = 21; auto ret = std::move(e).transform(ret_void); REQUIRE(ret); STATIC_REQUIRE( (std::is_same>::value)); } { tl::expected e(tl::unexpect, 21); auto ret = e.transform(ret_void); REQUIRE(!ret); STATIC_REQUIRE( (std::is_same>::value)); } { const tl::expected e(tl::unexpect, 21); auto ret = e.transform(ret_void); REQUIRE(!ret); STATIC_REQUIRE( (std::is_same>::value)); } { tl::expected e(tl::unexpect, 21); auto ret = std::move(e).transform(ret_void); REQUIRE(!ret); STATIC_REQUIRE( (std::is_same>::value)); } { const tl::expected e(tl::unexpect, 21); auto ret = std::move(e).transform(ret_void); REQUIRE(!ret); STATIC_REQUIRE( (std::is_same>::value)); } // mapping functions which return references { tl::expected e(42); auto ret = e.transform([](int& i) -> int& { return i; }); REQUIRE(ret); REQUIRE(ret == 42); } } TEST_CASE("Transform error extensions", "[extensions.transform_error]") { auto mul2 = [](int a) { return a * 2; }; auto ret_void = [](int a) { (void)a; }; { tl::expected e = 21; auto ret = e.transform_error(mul2); REQUIRE(ret); REQUIRE(*ret == 21); } { const tl::expected e = 21; auto ret = e.transform_error(mul2); REQUIRE(ret); REQUIRE(*ret == 21); } { tl::expected e = 21; auto ret = std::move(e).transform_error(mul2); REQUIRE(ret); REQUIRE(*ret == 21); } { const tl::expected e = 21; auto ret = std::move(e).transform_error(mul2); REQUIRE(ret); REQUIRE(*ret == 21); } { tl::expected e(tl::unexpect, 21); auto ret = e.transform_error(mul2); REQUIRE(!ret); REQUIRE(ret.error() == 42); } { const tl::expected e(tl::unexpect, 21); auto ret = e.transform_error(mul2); REQUIRE(!ret); REQUIRE(ret.error() == 42); } { tl::expected e(tl::unexpect, 21); auto ret = std::move(e).transform_error(mul2); REQUIRE(!ret); REQUIRE(ret.error() == 42); } { const tl::expected e(tl::unexpect, 21); auto ret = std::move(e).transform_error(mul2); REQUIRE(!ret); REQUIRE(ret.error() == 42); } { tl::expected e = 21; auto ret = e.transform_error(ret_void); REQUIRE(ret); } { const tl::expected e = 21; auto ret = e.transform_error(ret_void); REQUIRE(ret); } { tl::expected e = 21; auto ret = std::move(e).transform_error(ret_void); REQUIRE(ret); } { const tl::expected e = 21; auto ret = std::move(e).transform_error(ret_void); REQUIRE(ret); } { tl::expected e(tl::unexpect, 21); auto ret = e.transform_error(ret_void); REQUIRE(!ret); } { const tl::expected e(tl::unexpect, 21); auto ret = e.transform_error(ret_void); REQUIRE(!ret); } { tl::expected e(tl::unexpect, 21); auto ret = std::move(e).transform_error(ret_void); REQUIRE(!ret); } { const tl::expected e(tl::unexpect, 21); auto ret = std::move(e).transform_error(ret_void); REQUIRE(!ret); } } struct S { int x; }; struct F { int x; }; TEST_CASE("14", "[issue.14]") { auto res = tl::expected{tl::unexpect, F{}}; res.map_error([](F f) { (void)f; }); } TEST_CASE("32", "[issue.32]") { int i = 0; tl::expected a; a.map([&i]{i = 42;}); REQUIRE(i == 42); auto x = a.map([]{return 42;}); REQUIRE(*x == 42); } expected-1.1.0/tests/issues.cpp000066400000000000000000000112521440431670300164760ustar00rootroot00000000000000#include #include #include #include using std::string; tl::expected getInt3(int val) { return val; } tl::expected getInt2(int val) { return val; } tl::expected getInt1() { return getInt2(5).and_then(getInt3); } TEST_CASE("Issue 1", "[issues.1]") { getInt1(); } tl::expected operation1() { return 42; } tl::expected operation2(int const val) { (void)val; return "Bananas"; } TEST_CASE("Issue 17", "[issues.17]") { auto const intermediate_result = operation1(); intermediate_result.and_then(operation2); } struct a {}; struct b : a {}; auto doit() -> tl::expected, int> { return tl::make_unexpected(0); } TEST_CASE("Issue 23", "[issues.23]") { tl::expected, int> msg = doit(); REQUIRE(!msg.has_value()); } TEST_CASE("Issue 26", "[issues.26]") { tl::expected exp = tl::expected(tl::unexpect, 0); REQUIRE(!exp.has_value()); } struct foo { foo() = default; foo(foo &) = delete; foo(foo &&){}; }; TEST_CASE("Issue 29", "[issues.29]") { std::vector v; v.emplace_back(); tl::expected, int> ov = std::move(v); REQUIRE(ov->size() == 1); } tl::expected error() { return tl::make_unexpected(std::string("error1 ")); } std::string maperror(std::string s) { return s + "maperror "; } TEST_CASE("Issue 30", "[issues.30]") { error().map_error(maperror); } struct i31{ int i; }; TEST_CASE("Issue 31", "[issues.31]") { const tl::expected a = i31{42}; (void)a->i; tl::expected< void, std::string > result; tl::expected< void, std::string > result2 = result; result2 = result; } TEST_CASE("Issue 33", "[issues.33]") { tl::expected res {tl::unexpect, 0}; REQUIRE(!res); res = res.map_error([](int i) { (void)i; return 42; }); REQUIRE(res.error() == 42); } tl::expected voidWork() { return {}; } tl::expected work2() { return 42; } void errorhandling(std::string){} TEST_CASE("Issue 34", "[issues.34]") { tl::expected result = voidWork () .and_then (work2); result.map_error ([&] (std::string result) {errorhandling (result);}); } struct non_copyable { non_copyable(non_copyable&&) = default; non_copyable(non_copyable const&) = delete; non_copyable() = default; }; TEST_CASE("Issue 42", "[issues.42]") { tl::expected{}.map([](non_copyable) {}); } TEST_CASE("Issue 43", "[issues.43]") { auto result = tl::expected{}; result = tl::make_unexpected(std::string{ "foo" }); } #if !(__GNUC__ <= 5) #include using MaybeDataPtr = tl::expected>; MaybeDataPtr test(int i) noexcept { return std::move(i); } MaybeDataPtr test2(int i) noexcept { return std::move(i); } TEST_CASE("Issue 49", "[issues.49]") { auto m = test(10) .and_then(test2); } #endif tl::expected> func() { return 1; } TEST_CASE("Issue 61", "[issues.61]") { REQUIRE(func().value() == 1); } struct move_tracker { int moved = 0; move_tracker() = default; move_tracker(move_tracker const &other) noexcept {}; move_tracker(move_tracker &&orig) noexcept : moved(orig.moved + 1) {} move_tracker & operator=(move_tracker const &other) noexcept {}; move_tracker &operator=(move_tracker &&orig) noexcept { moved = orig.moved + 1; return *this; } }; TEST_CASE("Issue 122", "[issues.122]") { tl::expected res; res.emplace(); REQUIRE(res.value().moved == 0); } #ifdef __cpp_deduction_guides TEST_CASE("Issue 89", "[issues.89]") { auto s = tl::unexpected("Some string"); REQUIRE(s.value() == std::string("Some string")); } #endif struct S { int i = 0; int j = 0; S(int i) : i(i) {} S(int i, int j) : i(i), j(j) {} }; TEST_CASE("Issue 107", "[issues.107]") { tl::expected ex1(tl::unexpect, 2); tl::expected ex2(tl::unexpect, 2, 2); REQUIRE(ex1.error().i == 2); REQUIRE(ex1.error().j == 0); REQUIRE(ex2.error().i == 2); REQUIRE(ex2.error().j == 2); } TEST_CASE("Issue 129", "[issues.129]") { tl::expected, int> x1 {std::unique_ptr(new int(4))}; tl::expected, int> y1 {std::unique_ptr(new int(2))}; x1 = std::move(y1); REQUIRE(**x1 == 2); } expected-1.1.0/tests/main.cpp000066400000000000000000000000661440431670300161100ustar00rootroot00000000000000#define CATCH_CONFIG_MAIN #include expected-1.1.0/tests/noexcept.cpp000066400000000000000000000001531440431670300170060ustar00rootroot00000000000000#include #include TEST_CASE("Noexcept", "[noexcept]") { //TODO } expected-1.1.0/tests/observers.cpp000066400000000000000000000020641440431670300171760ustar00rootroot00000000000000#include #include struct move_detector { move_detector() = default; move_detector(move_detector &&rhs) { rhs.been_moved = true; } bool been_moved = false; }; TEST_CASE("Observers", "[observers]") { tl::expected o1 = 42; tl::expected o2 {tl::unexpect, 0}; const tl::expected o3 = 42; REQUIRE(*o1 == 42); REQUIRE(*o1 == o1.value()); REQUIRE(o2.value_or(42) == 42); REQUIRE(o2.error() == 0); REQUIRE(o3.value() == 42); auto success = std::is_same::value; REQUIRE(success); success = std::is_same::value; REQUIRE(success); success = std::is_same::value; REQUIRE(success); #ifndef TL_EXPECTED_NO_CONSTRR success = std::is_same::value; REQUIRE(success); #endif tl::expected o4{tl::in_place}; move_detector o5 = std::move(o4).value(); REQUIRE(o4->been_moved); REQUIRE(!o5.been_moved); } expected-1.1.0/tests/relops.cpp000066400000000000000000000005541440431670300164720ustar00rootroot00000000000000#include #include TEST_CASE("Relational operators", "[relops]") { tl::expected o1 = 42; tl::expected o2{tl::unexpect, 0}; const tl::expected o3 = 42; REQUIRE(o1 == o1); REQUIRE(o1 != o2); REQUIRE(o1 == o3); REQUIRE(o3 == o3); tl::expected o6; REQUIRE(o6 == o6); } expected-1.1.0/tests/swap.cpp000066400000000000000000000050751440431670300161430ustar00rootroot00000000000000#include #include struct no_throw { no_throw(std::string i) : i(i) {} std::string i; }; struct canthrow_move { canthrow_move(std::string i) : i(i) {} canthrow_move(canthrow_move const &) = default; canthrow_move(canthrow_move &&other) noexcept(false) : i(other.i) {} canthrow_move &operator=(canthrow_move &&) = default; std::string i; }; bool should_throw = false; #ifdef TL_EXPECTED_EXCEPTIONS_ENABLED struct willthrow_move { willthrow_move(std::string i) : i(i) {} willthrow_move(willthrow_move const &) = default; willthrow_move(willthrow_move &&other) : i(other.i) { if (should_throw) throw 0; } willthrow_move &operator=(willthrow_move &&) = default; std::string i; }; #endif // TL_EXPECTED_EXCEPTIONS_ENABLED static_assert(tl::detail::is_swappable::value, ""); template void swap_test() { std::string s1 = "abcdefghijklmnopqrstuvwxyz"; std::string s2 = "zyxwvutsrqponmlkjihgfedcba"; tl::expected a{s1}; tl::expected b{s2}; swap(a, b); REQUIRE(a->i == s2); REQUIRE(b->i == s1); a = s1; b = tl::unexpected(s2); swap(a, b); REQUIRE(a.error().i == s2); REQUIRE(b->i == s1); a = tl::unexpected(s1); b = s2; swap(a, b); REQUIRE(a->i == s2); REQUIRE(b.error().i == s1); a = tl::unexpected(s1); b = tl::unexpected(s2); swap(a, b); REQUIRE(a.error().i == s2); REQUIRE(b.error().i == s1); a = s1; b = s2; a.swap(b); REQUIRE(a->i == s2); REQUIRE(b->i == s1); a = s1; b = tl::unexpected(s2); a.swap(b); REQUIRE(a.error().i == s2); REQUIRE(b->i == s1); a = tl::unexpected(s1); b = s2; a.swap(b); REQUIRE(a->i == s2); REQUIRE(b.error().i == s1); a = tl::unexpected(s1); b = tl::unexpected(s2); a.swap(b); REQUIRE(a.error().i == s2); REQUIRE(b.error().i == s1); } #ifdef TL_EXPECTED_EXCEPTIONS_ENABLED TEST_CASE("swap") { swap_test(); swap_test(); swap_test(); std::string s1 = "abcdefghijklmnopqrstuvwxyz"; std::string s2 = "zyxwvutsrqponmlkjihgfedcbaxxx"; tl::expected a{s1}; tl::expected b{tl::unexpect, s2}; should_throw = 1; #ifdef _MSC_VER //this seems to break catch on GCC and Clang REQUIRE_THROWS(swap(a, b)); #endif REQUIRE(a->i == s1); REQUIRE(b.error().i == s2); } #endif // TL_EXPECTED_EXCEPTIONS_ENABLED expected-1.1.0/tests/test.cpp000066400000000000000000000016631440431670300161470ustar00rootroot00000000000000struct no_throw { no_throw(std::string i) : i(i) {} std::string i; }; struct canthrow_move { canthrow_move(std::string i) : i(i) {} canthrow_move(canthrow_move const &) = default; canthrow_move(canthrow_move &&other) noexcept(false) : i(other.i) {} canthrow_move &operator=(canthrow_move &&) = default; std::string i; }; bool should_throw = false; struct willthrow_move { willthrow_move(std::string i) : i(i) {} willthrow_move(willthrow_move const &) = default; willthrow_move(willthrow_move &&other) : i(other.i) { if (should_throw) throw 0; } willthrow_move &operator=(willthrow_move &&) = default; std::string i; }; int main() { std::string s1 = "abcdefghijklmnopqrstuvwxyz"; std::string s2 = "zyxwvutsrqponmlkjihgfedcbaxxx"; tl::expected a{s1}; tl::expected b{tl::unexpect, s2}; should_throw = 1; swap(a, b); }