int main() { std::expected<int, std::string> def; std::cout << def.has_value() << ", " << def.value() << '\n'; }| godbolt.org
struct DatabaseRecord { bool valid; }; struct Database { DatabaseRecord query(int id) { return DatabaseRecord{.valid = id == 10}; } bool connected() { return true; } }; std::expected<DatabaseRecord, std::string> findRecord(Database& db, int recordId) { if (!db.connected()) return std::unexpected("DB not connected"); auto record = db.query(recordId); if (record.valid) return record; return std::unexpected("Record not found"); } int main() { Database myDatabase; auto result = findRecord(myDatab...| godbolt.org
int main() { std::expected<std::vector<int>, std::string> success(std::in_place, {1, 2, 3}); std::expected<std::pair<bool, bool>, int> pack(std::in_place, true, false); }| godbolt.org
int main() { std::expected<int, std::string> error{std::unexpect, "error"}; }| godbolt.org
int main() { std::expected<int, std::string> success{42}; std::cout << success.has_value() << ", " << success.value() << '\n'; }| godbolt.org
int main() { std::expected<int, std::string> result{std::unexpected("")}; int value = result.value_or(0); // Returns 0 if result holds an error std::cout << "Value: " << value << '\n'; }| godbolt.org
std::expected<int, std::string> convertToInt(const std::string& input) { int value{}; auto [ptr, ec] = std::from_chars(input.data(), input.data() + input.size(), value); if (ec == std::errc()) return value; if (ec == std::errc::invalid_argument) return std::unexpected("Invalid number format"); else if (ec == std::errc::result_out_of_range) return std::unexpected("Number out of range"); return std::unexpected("Unknown conversion error"); } int main() { std::string userInput = "111111111111111"...| godbolt.org
std::expected<void, std::string> performAction(bool condition) { if (condition) return {}; else return std::unexpected("Action failed"); } int main() { if (auto result = performAction(false); result) std::cout << "Action performed successfully\n"; else std::cout << "Error: " << result.error() << '\n'; }| godbolt.org
int main() { std::expected<int, std::string> result{std::unexpected{"err"}}; if (!result) { result.error() += std::string{" in the filesystem"}; std::cout << result.error(); } }| godbolt.org
int main() { std::expected<int, std::string> result{std::unexpected("")}; try { std::cout << "Value: " << result.value() << '\n'; } catch (const std::bad_expected_access<std::string>& e) { std::cout << "Caught error: " << e.what() << '\n'; } }| godbolt.org
struct number { char* label; struct number* next; } five = { "5", NULL }, four = { "4", &five }, three = { "3", &four }, two = { "2", &three }, one = { "1", &two }, zero = { "0", &one }; struct number* succ(struct number* num) { return num->next; } struct number* pred(struct number* num) { struct number* ret = &zero; while (succ(ret) != num) ret = succ(ret); return ret; } struct number* add_numbers(struct number* num_a, struct number* num_b) { if (num_b == &zero) return num_a; return succ(add...| godbolt.org
extern int omp_get_thread_num (void); static void __attribute__((noipa)) vec_compare (svint32_t *x, svint32_t y) { svbool_t p = svnot_b_z (svptrue_b32 (), svcmpeq_s32 (svptrue_b32 (), *x, y)); if (svptest_any (svptrue_b32 (), p)) __builtin_abort (); } void __attribute__ ((noipa)) omp_firstprivate_sections () { int b[8], c[8]; svint32_t vb, vc; int i; #pragma omp parallel for for (i = 0; i < 8; i++) { b[i] = i; c[i] = i + 1; } vb = svld1_s32 (svptrue_b32 (), b); vc = svld1_s32 (svptrue_b32 (),...| godbolt.org
cmake_minimum_required(VERSION 3.30) project(json) add_executable(the_executable main.cpp)| godbolt.org
// a sample program using concepts // tested in gcc and clang template <typename T> concept has_a = requires(T v) { v.a(); }; template <typename T> requires has_a<T> void fun(T v) { v.a(); }| godbolt.org
template <typename T> struct wrapper { T _value; void operator() () requires std::is_invocable_v<T> { _value(); } void reset(T v) { _value = v; } }; // explicit instantiation: template struct wrapper<int>;| godbolt.org
Compiler Explorer is an interactive online compiler which shows the assembly output of compiled C++, Rust, Go (and many more) code.| godbolt.org
bool is_leap_year3(uint32_t y) { return (y & ((y % 25) ? 3 : 15)) == 0; }| godbolt.org
bool is_leap_year1(uint32_t y) { if ((y % 4) != 0) return false; if ((y % 25) != 0) return true; if ((y % 16) == 0) return true; return false; }| godbolt.org
#[no_mangle] pub fn compare256(src0: &[u8; 256], src1: &[u8; 256]) -> usize { src0.iter().zip(src1).take_while(|(x, y)| x == y).count() }| godbolt.org
use std::arch::x86_64::*; #[no_mangle] #[target_feature(enable = "sse2,bmi1")] pub unsafe fn compare256(src0: &[u8; 256], src1: &[u8; 256]) -> usize { let src0 = src0.chunks_exact(16); let src1 = src1.chunks_exact(16); let mut len = 0; unsafe { for (chunk0, chunk1) in src0.zip(src1) { // load the next chunks into a simd register let xmm_src0 = _mm_loadu_si128(chunk0.as_ptr() as *const __m128i); let xmm_src1 = _mm_loadu_si128(chunk1.as_ptr() as *const __m128i); // element-wise compare of the 8...| godbolt.org