Skip to content

Instantly share code, notes, and snippets.

@kbridge
Last active January 24, 2024 11:44
Show Gist options
  • Save kbridge/780c94409be831e236b94d9b40bb9800 to your computer and use it in GitHub Desktop.
Save kbridge/780c94409be831e236b94d9b40bb9800 to your computer and use it in GitHub Desktop.
play with new features added in cpp23. tested in VS2022 Version 17.8.2.
#include <iostream>
#include <version>
#ifdef __cpp_lib_print
# include <print>
#endif
#ifdef __cpp_lib_expected
# include <expected>
# include <system_error>
#endif
#if 0
# if _HAS_CXX23
# error too modern
# endif
#endif
int main()
{
std::cout << "supports print? " <<
#ifdef __cpp_lib_print
"yes"
#else
"no"
#endif
<< "\n";
std::cout << "supports expected? " <<
#ifdef __cpp_lib_expected
"yes"
#else
"no"
#endif
<< "\n";
#ifdef __cpp_lib_print
std::cout << "\n";
std::println("hello {}", 6 * 7);
#endif
#ifdef __cpp_lib_expected
std::cout << "\n";
{
std::expected<int, std::errc> result1;
std::println("[1] has_value: {}", result1.has_value());
}
{
std::expected<int, std::errc> result2(std::unexpect_t{}, std::errc::invalid_argument);
std::println("[2] has_value: {}", result2.has_value());
std::println(" errc: {}", std::underlying_type_t<std::errc>(result2.error()));
}
{
std::expected<int, std::errc> result3(std::in_place_t{}, 6);
auto derived_result = result3.transform(
[](int x)
{
return x * 7;
});
std::println("[3] {}", derived_result.value());
std::println(" {}", *derived_result);
}
#endif
return 0;
}
// https://www.youtube.com/watch?v=Dk_C_E8AtRs&t=934s
// => https://devblogs.microsoft.com/cppblog/cpp23s-optional-and-expected/
// => https://bartoszmilewski.com/2014/10/28/category-theory-for-programmers-the-preface/
supports print? yes
supports expected? yes
hello 42
[1] has_value: true
[2] has_value: false
errc: 22
[3] 42
42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment