Skip to content

Commit

Permalink
Add preprocessor guards to disable extensions (TartanLlama#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisHLAV committed Aug 15, 2022
1 parent 2b462c4 commit e361589
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ endif()

option(EXPECTED_BUILD_PACKAGE "Build package files as well" ON)

option(EXPECTED_NO_EXT "Build with extensions disabled" OFF)

if (EXPECTED_NO_EXT)
add_definitions(-DTL_EXPECTED_NO_EXT)
endif()

cmake_dependent_option(EXPECTED_BUILD_TESTS
"Enable tl::expected tests" ON
"BUILD_TESTING" OFF)
Expand Down Expand Up @@ -73,6 +79,11 @@ if(EXPECTED_BUILD_TESTS)

file(GLOB test-sources CONFIGURE_DEPENDS tests/*.cpp)
list(FILTER test-sources EXCLUDE REGEX "tests/test.cpp")

if (EXPECTED_NO_EXT)
list(FILTER test-sources EXCLUDE REGEX "tests/extensions.cpp")
endif()

add_executable(${PROJECT_NAME}-tests "${test-sources}")
target_compile_options(${PROJECT_NAME}-tests PRIVATE
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall -Wextra>)
Expand Down
4 changes: 4 additions & 0 deletions include/tl/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,7 @@ class expected : private detail::expected_move_assign_base<T, E>,
typedef E error_type;
typedef unexpected<E> unexpected_type;

#ifndef TL_EXPECTED_NO_EXT
#if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \
!defined(TL_EXPECTED_GCC54) && !defined(TL_EXPECTED_GCC55)
template <class F> TL_EXPECTED_11_CONSTEXPR auto and_then(F &&f) & {
Expand Down Expand Up @@ -1442,6 +1443,7 @@ class expected : private detail::expected_move_assign_base<T, E>,
template <class F> expected constexpr or_else(F &&f) const && {
return or_else_impl(std::move(*this), std::forward<F>(f));
}
#endif
#endif
constexpr expected() = default;
constexpr expected(const expected &rhs) = default;
Expand Down Expand Up @@ -1942,6 +1944,7 @@ template <class Exp> using exp_t = typename detail::decay_t<Exp>::value_type;
template <class Exp> using err_t = typename detail::decay_t<Exp>::error_type;
template <class Exp, class Ret> using ret_t = expected<Ret, err_t<Exp>>;

#ifndef TL_EXPECTED_NO_EXT
#ifdef TL_EXPECTED_CXX14
template <class Exp, class F,
detail::enable_if_t<!std::is_void<exp_t<Exp>>::value> * = nullptr,
Expand Down Expand Up @@ -2260,6 +2263,7 @@ detail::decay_t<Exp> or_else_impl(Exp &&exp, F &&f) {
std::forward<Exp>(exp));
}
#endif
#endif
} // namespace detail

template <class T, class E, class U, class F>
Expand Down
12 changes: 12 additions & 0 deletions tests/issues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using std::string;

#ifndef TL_EXPECTED_NO_EXT
tl::expected<int, string> getInt3(int val) { return val; }

tl::expected<int, string> getInt2(int val) { return val; }
Expand All @@ -22,6 +23,7 @@ TEST_CASE("Issue 17", "[issues.17]") {

intermediate_result.and_then(operation2);
}
#endif

struct a {};
struct b : a {};
Expand Down Expand Up @@ -53,6 +55,7 @@ TEST_CASE("Issue 29", "[issues.29]") {
REQUIRE(ov->size() == 1);
}

#ifndef TL_EXPECTED_NO_EXT
tl::expected<int, std::string> error() {
return tl::make_unexpected(std::string("error1 "));
}
Expand All @@ -61,6 +64,7 @@ std::string maperror(std::string s) { return s + "maperror "; }
TEST_CASE("Issue 30", "[issues.30]") {
error().map_error(maperror);
}
#endif

struct i31{
int i;
Expand All @@ -74,14 +78,17 @@ TEST_CASE("Issue 31", "[issues.31]") {
result2 = result;
}

#ifndef TL_EXPECTED_NO_EXT
TEST_CASE("Issue 33", "[issues.33]") {
tl::expected<void, int> res {tl::unexpect, 0};
REQUIRE(!res);
res = res.map_error([](int i) { return 42; });
REQUIRE(res.error() == 42);
}
#endif


#ifndef TL_EXPECTED_NO_EXT
tl::expected<void, std::string> voidWork() { return {}; }
tl::expected<int, std::string> work2() { return 42; }
void errorhandling(std::string){}
Expand All @@ -91,7 +98,9 @@ TEST_CASE("Issue 34", "[issues.34]") {
.and_then (work2);
result.map_error ([&] (std::string result) {errorhandling (result);});
}
#endif

#ifndef TL_EXPECTED_NO_EXT
struct non_copyable {
non_copyable(non_copyable&&) = default;
non_copyable(non_copyable const&) = delete;
Expand All @@ -101,12 +110,14 @@ struct non_copyable {
TEST_CASE("Issue 42", "[issues.42]") {
tl::expected<non_copyable,int>{}.map([](non_copyable) {});
}
#endif

TEST_CASE("Issue 43", "[issues.43]") {
auto result = tl::expected<void, std::string>{};
result = tl::make_unexpected(std::string{ "foo" });
}

#ifndef TL_EXPECTED_NO_EXT
#if !(__GNUC__ <= 5)
#include <memory>

Expand All @@ -127,6 +138,7 @@ TEST_CASE("Issue 49", "[issues.49]") {
.and_then(test2);
}
#endif
#endif

tl::expected<int, std::unique_ptr<std::string>> func()
{
Expand Down

0 comments on commit e361589

Please sign in to comment.