Skip to content

Commit

Permalink
Add a cmake check whether std::expected is supported or not
Browse files Browse the repository at this point in the history
Note. Perhaps a wrong way. Please let me know if there is something better.
  • Loading branch information
vt4a2h committed Jan 6, 2024
1 parent 4df0e5c commit 01e1520
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 3,
"version": 6,
"cmakeMinimumRequired": {
"major": 3,
"minor": 17,
Expand Down
10 changes: 10 additions & 0 deletions cmake/helpers.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function(has_expected OUT_RESULT)
try_compile(COMPILE_RESULT
${CMAKE_BINARY_DIR}/test_apps
${CMAKE_SOURCE_DIR}/cmake/try-compile-apps/expected.cpp
CXX_STANDARD 23
CXX_STANDARD_REQUIRED TRUE
CXX_EXTENSIONS FALSE
)
set(${OUT_RESULT} ${COMPILE_RESULT} PARENT_SCOPE)
endfunction()
12 changes: 12 additions & 0 deletions cmake/try-compile-apps/expected.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <expected>
#include <string>

int main(int /*argc*/, char* /*argv*/[])
{
using Expected = std::expected<int, std::string>;

Expected result{0};
Expected unxepectedResult = std::unexpected(std::string{"foo"});

return result != unxepectedResult ? *result : -1;
}
5 changes: 5 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
include(${CMAKE_SOURCE_DIR}/cmake/helpers.cmake)

has_expected(HAS_EXPECTED_RESULT)
message(STATUS "Has std::expected support: ${HAS_EXPECTED_RESULT}")

find_package(Catch2 CONFIG REQUIRED)
find_package(fmt CONFIG REQUIRED)
find_package(spdlog CONFIG REQUIRED)
Expand Down

0 comments on commit 01e1520

Please sign in to comment.