Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
Go
Haskell
HLSL
Hook
Hylo
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Zig
Javascript
GIMPLE
c++ source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
6502-c++ 11.1.0
ARM GCC 10.2.0
ARM GCC 10.3.0
ARM GCC 10.4.0
ARM GCC 10.5.0
ARM GCC 11.1.0
ARM GCC 11.2.0
ARM GCC 11.3.0
ARM GCC 11.4.0
ARM GCC 12.1.0
ARM GCC 12.2.0
ARM GCC 12.3.0
ARM GCC 13.1.0
ARM GCC 13.2.0
ARM GCC 13.2.0 (unknown-eabi)
ARM GCC 4.5.4
ARM GCC 4.6.4
ARM GCC 5.4
ARM GCC 6.3.0
ARM GCC 6.4.0
ARM GCC 7.3.0
ARM GCC 7.5.0
ARM GCC 8.2.0
ARM GCC 8.5.0
ARM GCC 9.3.0
ARM GCC 9.4.0
ARM GCC 9.5.0
ARM GCC trunk
ARM gcc 10.2.1 (none)
ARM gcc 10.3.1 (2021.07 none)
ARM gcc 10.3.1 (2021.10 none)
ARM gcc 11.2.1 (none)
ARM gcc 5.4.1 (none)
ARM gcc 7.2.1 (none)
ARM gcc 8.2 (WinCE)
ARM gcc 8.3.1 (none)
ARM gcc 9.2.1 (none)
ARM msvc v19.0 (WINE)
ARM msvc v19.10 (WINE)
ARM msvc v19.14 (WINE)
ARM64 Morello gcc 10.1 Alpha 2
ARM64 gcc 10.2
ARM64 gcc 10.3
ARM64 gcc 10.4
ARM64 gcc 10.5.0
ARM64 gcc 11.1
ARM64 gcc 11.2
ARM64 gcc 11.3
ARM64 gcc 11.4.0
ARM64 gcc 12.1
ARM64 gcc 12.2.0
ARM64 gcc 12.3.0
ARM64 gcc 13.1.0
ARM64 gcc 13.2.0
ARM64 gcc 5.4
ARM64 gcc 6.3
ARM64 gcc 6.4
ARM64 gcc 7.3
ARM64 gcc 7.5
ARM64 gcc 8.2
ARM64 gcc 8.5
ARM64 gcc 9.3
ARM64 gcc 9.4
ARM64 gcc 9.5
ARM64 gcc trunk
ARM64 msvc v19.14 (WINE)
AVR gcc 10.3.0
AVR gcc 11.1.0
AVR gcc 12.1.0
AVR gcc 12.2.0
AVR gcc 12.3.0
AVR gcc 13.1.0
AVR gcc 13.2.0
AVR gcc 4.5.4
AVR gcc 4.6.4
AVR gcc 5.4.0
AVR gcc 9.2.0
AVR gcc 9.3.0
Arduino Mega (1.8.9)
Arduino Uno (1.8.9)
BPF clang (trunk)
BPF clang 13.0.0
BPF clang 14.0.0
BPF clang 15.0.0
BPF clang 16.0.0
BPF clang 17.0.1
BPF clang 18.1.0
BPF gcc 13.1.0
BPF gcc 13.2.0
BPF gcc trunk
EDG (experimental reflection)
EDG 6.5
EDG 6.5 (GNU mode gcc 13)
EDG 6.6
EDG 6.6 (GNU mode gcc 13)
FRC 2019
FRC 2020
FRC 2023
KVX ACB 4.1.0 (GCC 7.5.0)
KVX ACB 4.1.0-cd1 (GCC 7.5.0)
KVX ACB 4.10.0 (GCC 10.3.1)
KVX ACB 4.11.1 (GCC 10.3.1)
KVX ACB 4.12.0 (GCC 11.3.0)
KVX ACB 4.2.0 (GCC 7.5.0)
KVX ACB 4.3.0 (GCC 7.5.0)
KVX ACB 4.4.0 (GCC 7.5.0)
KVX ACB 4.6.0 (GCC 9.4.1)
KVX ACB 4.8.0 (GCC 9.4.1)
KVX ACB 4.9.0 (GCC 9.4.1)
M68K gcc 13.1.0
M68K gcc 13.2.0
M68k clang (trunk)
MRISC32 gcc (trunk)
MSP430 gcc 4.5.3
MSP430 gcc 5.3.0
MSP430 gcc 6.2.1
MinGW clang 14.0.3
MinGW clang 14.0.6
MinGW clang 15.0.7
MinGW clang 16.0.0
MinGW clang 16.0.2
MinGW gcc 11.3.0
MinGW gcc 12.1.0
MinGW gcc 12.2.0
MinGW gcc 13.1.0
RISC-V (32-bits) gcc (trunk)
RISC-V (32-bits) gcc 10.2.0
RISC-V (32-bits) gcc 10.3.0
RISC-V (32-bits) gcc 11.2.0
RISC-V (32-bits) gcc 11.3.0
RISC-V (32-bits) gcc 11.4.0
RISC-V (32-bits) gcc 12.1.0
RISC-V (32-bits) gcc 12.2.0
RISC-V (32-bits) gcc 12.3.0
RISC-V (32-bits) gcc 13.1.0
RISC-V (32-bits) gcc 13.2.0
RISC-V (32-bits) gcc 8.2.0
RISC-V (32-bits) gcc 8.5.0
RISC-V (32-bits) gcc 9.4.0
RISC-V (64-bits) gcc (trunk)
RISC-V (64-bits) gcc 10.2.0
RISC-V (64-bits) gcc 10.3.0
RISC-V (64-bits) gcc 11.2.0
RISC-V (64-bits) gcc 11.3.0
RISC-V (64-bits) gcc 11.4.0
RISC-V (64-bits) gcc 12.1.0
RISC-V (64-bits) gcc 12.2.0
RISC-V (64-bits) gcc 12.3.0
RISC-V (64-bits) gcc 13.1.0
RISC-V (64-bits) gcc 13.2.0
RISC-V (64-bits) gcc 8.2.0
RISC-V (64-bits) gcc 8.5.0
RISC-V (64-bits) gcc 9.4.0
RISC-V rv32gc clang (trunk)
RISC-V rv32gc clang 10.0.0
RISC-V rv32gc clang 10.0.1
RISC-V rv32gc clang 11.0.0
RISC-V rv32gc clang 11.0.1
RISC-V rv32gc clang 12.0.0
RISC-V rv32gc clang 12.0.1
RISC-V rv32gc clang 13.0.0
RISC-V rv32gc clang 13.0.1
RISC-V rv32gc clang 14.0.0
RISC-V rv32gc clang 15.0.0
RISC-V rv32gc clang 16.0.0
RISC-V rv32gc clang 17.0.1
RISC-V rv32gc clang 18.1.0
RISC-V rv32gc clang 9.0.0
RISC-V rv32gc clang 9.0.1
RISC-V rv64gc clang (trunk)
RISC-V rv64gc clang 10.0.0
RISC-V rv64gc clang 10.0.1
RISC-V rv64gc clang 11.0.0
RISC-V rv64gc clang 11.0.1
RISC-V rv64gc clang 12.0.0
RISC-V rv64gc clang 12.0.1
RISC-V rv64gc clang 13.0.0
RISC-V rv64gc clang 13.0.1
RISC-V rv64gc clang 14.0.0
RISC-V rv64gc clang 15.0.0
RISC-V rv64gc clang 16.0.0
RISC-V rv64gc clang 17.0.1
RISC-V rv64gc clang 18.1.0
RISC-V rv64gc clang 9.0.0
RISC-V rv64gc clang 9.0.1
Raspbian Buster
Raspbian Stretch
SPARC LEON gcc 12.2.0
SPARC LEON gcc 12.3.0
SPARC LEON gcc 13.1.0
SPARC LEON gcc 13.2.0
SPARC gcc 12.2.0
SPARC gcc 12.3.0
SPARC gcc 13.1.0
SPARC gcc 13.2.0
SPARC64 gcc 12.2.0
SPARC64 gcc 12.3.0
SPARC64 gcc 13.1.0
SPARC64 gcc 13.2.0
TI C6x gcc 12.2.0
TI C6x gcc 12.3.0
TI C6x gcc 13.1.0
TI C6x gcc 13.2.0
TI CL430 21.6.1
VAX gcc NetBSDELF 10.4.0
VAX gcc NetBSDELF 10.5.0 (Nov 15 03:50:22 2023)
WebAssembly clang (trunk)
Xtensa ESP32 gcc 11.2.0 (2022r1)
Xtensa ESP32 gcc 12.2.0 (20230208)
Xtensa ESP32 gcc 8.2.0 (2019r2)
Xtensa ESP32 gcc 8.2.0 (2020r1)
Xtensa ESP32 gcc 8.2.0 (2020r2)
Xtensa ESP32 gcc 8.4.0 (2020r3)
Xtensa ESP32 gcc 8.4.0 (2021r1)
Xtensa ESP32 gcc 8.4.0 (2021r2)
Xtensa ESP32-S2 gcc 11.2.0 (2022r1)
Xtensa ESP32-S2 gcc 12.2.0 (20230208)
Xtensa ESP32-S2 gcc 8.2.0 (2019r2)
Xtensa ESP32-S2 gcc 8.2.0 (2020r1)
Xtensa ESP32-S2 gcc 8.2.0 (2020r2)
Xtensa ESP32-S2 gcc 8.4.0 (2020r3)
Xtensa ESP32-S2 gcc 8.4.0 (2021r1)
Xtensa ESP32-S2 gcc 8.4.0 (2021r2)
Xtensa ESP32-S3 gcc 11.2.0 (2022r1)
Xtensa ESP32-S3 gcc 12.2.0 (20230208)
Xtensa ESP32-S3 gcc 8.4.0 (2020r3)
Xtensa ESP32-S3 gcc 8.4.0 (2021r1)
Xtensa ESP32-S3 gcc 8.4.0 (2021r2)
arm64 msvc v19.28 VS16.9
arm64 msvc v19.29 VS16.10
arm64 msvc v19.29 VS16.11
arm64 msvc v19.30
arm64 msvc v19.31
arm64 msvc v19.32
arm64 msvc v19.33
arm64 msvc v19.34
arm64 msvc v19.35
arm64 msvc v19.36
arm64 msvc v19.37
arm64 msvc v19.38
arm64 msvc v19.latest
armv7-a clang (trunk)
armv7-a clang 10.0.0
armv7-a clang 10.0.1
armv7-a clang 11.0.0
armv7-a clang 11.0.1
armv7-a clang 9.0.0
armv7-a clang 9.0.1
armv8-a clang (all architectural features, trunk)
armv8-a clang (trunk)
armv8-a clang 10.0.0
armv8-a clang 10.0.1
armv8-a clang 11.0.0
armv8-a clang 11.0.1
armv8-a clang 12.0.0
armv8-a clang 13.0.0
armv8-a clang 14.0.0
armv8-a clang 15.0.0
armv8-a clang 16.0.0
armv8-a clang 17.0.1
armv8-a clang 18.1.0
armv8-a clang 9.0.0
armv8-a clang 9.0.1
ellcc 0.1.33
ellcc 0.1.34
ellcc 2017-07-16
hexagon-clang 16.0.5
llvm-mos atari2600-3e
llvm-mos atari2600-4k
llvm-mos atari2600-common
llvm-mos atari5200-supercart
llvm-mos atari8-cart-megacart
llvm-mos atari8-cart-std
llvm-mos atari8-cart-xegs
llvm-mos atari8-common
llvm-mos atari8-dos
llvm-mos c128
llvm-mos c64
llvm-mos commodore
llvm-mos cpm65
llvm-mos cx16
llvm-mos dodo
llvm-mos eater
llvm-mos mega65
llvm-mos nes
llvm-mos nes-action53
llvm-mos nes-cnrom
llvm-mos nes-gtrom
llvm-mos nes-mmc1
llvm-mos nes-mmc3
llvm-mos nes-nrom
llvm-mos nes-unrom
llvm-mos nes-unrom-512
llvm-mos osi-c1p
llvm-mos pce
llvm-mos pce-cd
llvm-mos pce-common
llvm-mos pet
llvm-mos rp6502
llvm-mos rpc8e
llvm-mos supervision
llvm-mos vic20
loongarch64 gcc 12.2.0
loongarch64 gcc 12.3.0
loongarch64 gcc 13.1.0
loongarch64 gcc 13.2.0
mips clang 13.0.0
mips clang 14.0.0
mips clang 15.0.0
mips clang 16.0.0
mips clang 17.0.1
mips clang 18.1.0
mips gcc 11.2.0
mips gcc 12.1.0
mips gcc 12.2.0
mips gcc 12.3.0
mips gcc 13.1.0
mips gcc 13.2.0
mips gcc 4.9.4
mips gcc 5.4
mips gcc 5.5.0
mips gcc 9.3.0 (codescape)
mips gcc 9.5.0
mips64 (el) gcc 12.1.0
mips64 (el) gcc 12.2.0
mips64 (el) gcc 12.3.0
mips64 (el) gcc 13.1.0
mips64 (el) gcc 13.2.0
mips64 (el) gcc 4.9.4
mips64 (el) gcc 5.4.0
mips64 (el) gcc 5.5.0
mips64 (el) gcc 9.5.0
mips64 clang 13.0.0
mips64 clang 14.0.0
mips64 clang 15.0.0
mips64 clang 16.0.0
mips64 clang 17.0.1
mips64 clang 18.1.0
mips64 gcc 11.2.0
mips64 gcc 12.1.0
mips64 gcc 12.2.0
mips64 gcc 12.3.0
mips64 gcc 13.1.0
mips64 gcc 13.2.0
mips64 gcc 4.9.4
mips64 gcc 5.4.0
mips64 gcc 5.5.0
mips64 gcc 9.5.0
mips64el clang 13.0.0
mips64el clang 14.0.0
mips64el clang 15.0.0
mips64el clang 16.0.0
mips64el clang 17.0.1
mips64el clang 18.1.0
mipsel clang 13.0.0
mipsel clang 14.0.0
mipsel clang 15.0.0
mipsel clang 16.0.0
mipsel clang 17.0.1
mipsel clang 18.1.0
mipsel gcc 12.1.0
mipsel gcc 12.2.0
mipsel gcc 12.3.0
mipsel gcc 13.1.0
mipsel gcc 13.2.0
mipsel gcc 4.9.4
mipsel gcc 5.4.0
mipsel gcc 5.5.0
mipsel gcc 9.5.0
nanoMIPS gcc 6.3.0 (mtk)
power gcc 11.2.0
power gcc 12.1.0
power gcc 12.2.0
power gcc 12.3.0
power gcc 13.1.0
power gcc 13.2.0
power gcc 4.8.5
power64 AT12.0 (gcc8)
power64 AT13.0 (gcc9)
power64 gcc 11.2.0
power64 gcc 12.1.0
power64 gcc 12.2.0
power64 gcc 12.3.0
power64 gcc 13.1.0
power64 gcc 13.2.0
power64 gcc trunk
power64le AT12.0 (gcc8)
power64le AT13.0 (gcc9)
power64le clang (trunk)
power64le gcc 11.2.0
power64le gcc 12.1.0
power64le gcc 12.2.0
power64le gcc 12.3.0
power64le gcc 13.1.0
power64le gcc 13.2.0
power64le gcc 6.3.0
power64le gcc trunk
powerpc64 clang (trunk)
s390x gcc 11.2.0
s390x gcc 12.1.0
s390x gcc 12.2.0
s390x gcc 12.3.0
s390x gcc 13.1.0
s390x gcc 13.2.0
sh gcc 12.2.0
sh gcc 12.3.0
sh gcc 13.1.0
sh gcc 13.2.0
sh gcc 4.9.4
sh gcc 9.5.0
vast (trunk)
x64 msvc v19.0 (WINE)
x64 msvc v19.10 (WINE)
x64 msvc v19.14
x64 msvc v19.14 (WINE)
x64 msvc v19.15
x64 msvc v19.16
x64 msvc v19.20
x64 msvc v19.21
x64 msvc v19.22
x64 msvc v19.23
x64 msvc v19.24
x64 msvc v19.25
x64 msvc v19.26
x64 msvc v19.27
x64 msvc v19.28
x64 msvc v19.28 VS16.9
x64 msvc v19.29 VS16.10
x64 msvc v19.29 VS16.11
x64 msvc v19.30
x64 msvc v19.31
x64 msvc v19.32
x64 msvc v19.33
x64 msvc v19.34
x64 msvc v19.35
x64 msvc v19.36
x64 msvc v19.37
x64 msvc v19.38
x64 msvc v19.latest
x86 djgpp 4.9.4
x86 djgpp 5.5.0
x86 djgpp 6.4.0
x86 djgpp 7.2.0
x86 msvc v19.0 (WINE)
x86 msvc v19.10 (WINE)
x86 msvc v19.14
x86 msvc v19.14 (WINE)
x86 msvc v19.15
x86 msvc v19.16
x86 msvc v19.20
x86 msvc v19.21
x86 msvc v19.22
x86 msvc v19.23
x86 msvc v19.24
x86 msvc v19.25
x86 msvc v19.26
x86 msvc v19.27
x86 msvc v19.28
x86 msvc v19.28 VS16.9
x86 msvc v19.29 VS16.10
x86 msvc v19.29 VS16.11
x86 msvc v19.30
x86 msvc v19.31
x86 msvc v19.32
x86 msvc v19.33
x86 msvc v19.34
x86 msvc v19.35
x86 msvc v19.36
x86 msvc v19.37
x86 msvc v19.38
x86 msvc v19.latest
x86 nvc++ 22.11
x86 nvc++ 22.7
x86 nvc++ 22.9
x86 nvc++ 23.1
x86 nvc++ 23.11
x86 nvc++ 23.3
x86 nvc++ 23.5
x86 nvc++ 23.7
x86 nvc++ 23.9
x86 nvc++ 24.1
x86 nvc++ 24.3
x86-64 Zapcc 190308
x86-64 clang (amd-stg-open)
x86-64 clang (assertions trunk)
x86-64 clang (clangir)
x86-64 clang (experimental -Wlifetime)
x86-64 clang (experimental P1061)
x86-64 clang (experimental P1144)
x86-64 clang (experimental P1221)
x86-64 clang (experimental P2996)
x86-64 clang (experimental metaprogramming - P2632)
x86-64 clang (experimental pattern matching)
x86-64 clang (old concepts branch)
x86-64 clang (reflection)
x86-64 clang (resugar)
x86-64 clang (thephd.dev)
x86-64 clang (trunk)
x86-64 clang (variadic friends - P2893)
x86-64 clang (widberg)
x86-64 clang 10.0.0
x86-64 clang 10.0.0 (assertions)
x86-64 clang 10.0.1
x86-64 clang 11.0.0
x86-64 clang 11.0.0 (assertions)
x86-64 clang 11.0.1
x86-64 clang 12.0.0
x86-64 clang 12.0.0 (assertions)
x86-64 clang 12.0.1
x86-64 clang 13.0.0
x86-64 clang 13.0.0 (assertions)
x86-64 clang 13.0.1
x86-64 clang 14.0.0
x86-64 clang 14.0.0 (assertions)
x86-64 clang 15.0.0
x86-64 clang 15.0.0 (assertions)
x86-64 clang 16.0.0
x86-64 clang 16.0.0 (assertions)
x86-64 clang 17.0.1
x86-64 clang 17.0.1 (assertions)
x86-64 clang 18.1.0
x86-64 clang 18.1.0 (assertions)
x86-64 clang 2.6.0 (assertions)
x86-64 clang 2.7.0 (assertions)
x86-64 clang 2.8.0 (assertions)
x86-64 clang 2.9.0 (assertions)
x86-64 clang 3.0.0
x86-64 clang 3.0.0 (assertions)
x86-64 clang 3.1
x86-64 clang 3.1 (assertions)
x86-64 clang 3.2
x86-64 clang 3.2 (assertions)
x86-64 clang 3.3
x86-64 clang 3.3 (assertions)
x86-64 clang 3.4 (assertions)
x86-64 clang 3.4.1
x86-64 clang 3.5
x86-64 clang 3.5 (assertions)
x86-64 clang 3.5.1
x86-64 clang 3.5.2
x86-64 clang 3.6
x86-64 clang 3.6 (assertions)
x86-64 clang 3.7
x86-64 clang 3.7 (assertions)
x86-64 clang 3.7.1
x86-64 clang 3.8
x86-64 clang 3.8 (assertions)
x86-64 clang 3.8.1
x86-64 clang 3.9.0
x86-64 clang 3.9.0 (assertions)
x86-64 clang 3.9.1
x86-64 clang 4.0.0
x86-64 clang 4.0.0 (assertions)
x86-64 clang 4.0.1
x86-64 clang 5.0.0
x86-64 clang 5.0.0 (assertions)
x86-64 clang 5.0.1
x86-64 clang 5.0.2
x86-64 clang 6.0.0
x86-64 clang 6.0.0 (assertions)
x86-64 clang 6.0.1
x86-64 clang 7.0.0
x86-64 clang 7.0.0 (assertions)
x86-64 clang 7.0.1
x86-64 clang 7.1.0
x86-64 clang 8.0.0
x86-64 clang 8.0.0 (assertions)
x86-64 clang 8.0.1
x86-64 clang 9.0.0
x86-64 clang 9.0.0 (assertions)
x86-64 clang 9.0.1
x86-64 clang rocm-4.5.2
x86-64 clang rocm-5.0.2
x86-64 clang rocm-5.1.3
x86-64 clang rocm-5.2.3
x86-64 clang rocm-5.3.3
x86-64 clang rocm-5.7.0
x86-64 gcc (contract labels)
x86-64 gcc (contracts natural syntax)
x86-64 gcc (contracts)
x86-64 gcc (coroutines)
x86-64 gcc (modules)
x86-64 gcc (trunk)
x86-64 gcc 10.1
x86-64 gcc 10.2
x86-64 gcc 10.3
x86-64 gcc 10.4
x86-64 gcc 10.5
x86-64 gcc 11.1
x86-64 gcc 11.2
x86-64 gcc 11.3
x86-64 gcc 11.4
x86-64 gcc 12.1
x86-64 gcc 12.2
x86-64 gcc 12.3
x86-64 gcc 13.1
x86-64 gcc 13.2
x86-64 gcc 4.1.2
x86-64 gcc 4.4.7
x86-64 gcc 4.5.3
x86-64 gcc 4.6.4
x86-64 gcc 4.7.1
x86-64 gcc 4.7.2
x86-64 gcc 4.7.3
x86-64 gcc 4.7.4
x86-64 gcc 4.8.1
x86-64 gcc 4.8.2
x86-64 gcc 4.8.3
x86-64 gcc 4.8.4
x86-64 gcc 4.8.5
x86-64 gcc 4.9.0
x86-64 gcc 4.9.1
x86-64 gcc 4.9.2
x86-64 gcc 4.9.3
x86-64 gcc 4.9.4
x86-64 gcc 5.1
x86-64 gcc 5.2
x86-64 gcc 5.3
x86-64 gcc 5.4
x86-64 gcc 5.5
x86-64 gcc 6.1
x86-64 gcc 6.2
x86-64 gcc 6.3
x86-64 gcc 6.4
x86-64 gcc 7.1
x86-64 gcc 7.2
x86-64 gcc 7.3
x86-64 gcc 7.4
x86-64 gcc 7.5
x86-64 gcc 8.1
x86-64 gcc 8.2
x86-64 gcc 8.3
x86-64 gcc 8.4
x86-64 gcc 8.5
x86-64 gcc 9.1
x86-64 gcc 9.2
x86-64 gcc 9.3
x86-64 gcc 9.4
x86-64 gcc 9.5
x86-64 icc 13.0.1
x86-64 icc 16.0.3
x86-64 icc 17.0.0
x86-64 icc 18.0.0
x86-64 icc 19.0.0
x86-64 icc 19.0.1
x86-64 icc 2021.1.2
x86-64 icc 2021.10.0
x86-64 icc 2021.2.0
x86-64 icc 2021.3.0
x86-64 icc 2021.4.0
x86-64 icc 2021.5.0
x86-64 icc 2021.6.0
x86-64 icc 2021.7.0
x86-64 icc 2021.7.1
x86-64 icc 2021.8.0
x86-64 icc 2021.9.0
x86-64 icx (latest)
x86-64 icx 2021.1.2
x86-64 icx 2021.2.0
x86-64 icx 2021.3.0
x86-64 icx 2021.4.0
x86-64 icx 2022.0.0
x86-64 icx 2022.1.0
x86-64 icx 2022.2.0
x86-64 icx 2022.2.1
x86-64 icx 2023.0.0
x86-64 icx 2023.1.0
x86-64 icx 2023.2.1
x86-64 icx 2024.0.0
zig c++ 0.10.0
zig c++ 0.11.0
zig c++ 0.12.0
zig c++ 0.6.0
zig c++ 0.7.0
zig c++ 0.7.1
zig c++ 0.8.0
zig c++ 0.9.0
zig c++ trunk
Options
Source code
#include <string> #include <type_traits> #include <vector> namespace stx { inline namespace v1 { template <typename Fn, typename... Args> using invoke_result = typename std::invoke_result<Fn, Args...>::type; template <typename T> inline constexpr bool destructible = std::is_destructible<T>::value; template <typename T> inline constexpr bool nothrow_destructible = std::is_nothrow_destructible<T>::value; template <typename T, typename U> inline constexpr bool assignable = std::is_assignable<T, U>::value; template <typename T, typename U> inline constexpr bool nothrow_assignable = std::is_nothrow_assignable<T, U>::value; namespace internal { namespace traits { enum expr_trait { invalid, valid, nothrow_valid }; template <typename Enable, typename T, typename... Args> inline constexpr expr_trait _constructible = invalid; template <typename T, typename... Args> inline constexpr expr_trait _constructible< std::void_t<decltype(new (static_cast<void*>(nullptr)) T(std::declval<Args>()...))>, T, Args...> = noexcept(new (static_cast<void*>(nullptr)) T(std::declval<Args>()...)) ? nothrow_valid : valid; } } template <typename T, typename... Args> inline constexpr bool constructible = internal::traits::_constructible<void, T, Args...> != internal::traits::invalid; template <typename T, typename... Args> inline constexpr bool nothrow_constructible = internal::traits::_constructible<void, T, Args...> == internal::traits::nothrow_valid; template <typename Fn, typename... Args> constexpr bool invocable = std::is_invocable<Fn, Args...>::value; template <typename T> constexpr bool movable = std::is_object<T>::value&& std::is_move_constructible<T>::value&& std::is_assignable<T&, T>::value&& std::is_swappable<T>::value; template <typename T> constexpr bool copy_constructible = std::is_copy_constructible<T>::value; template <typename T> constexpr bool default_constructible = std::is_default_constructible<T>::value; template <typename From, typename To> constexpr bool convertible = std::is_convertible<From, To>::value; template <typename T, typename Cmp = T, typename = void> struct is_equality_comparable : std::false_type {}; template <typename T, typename Cmp> struct is_equality_comparable< T, Cmp, typename std::enable_if_t< true, decltype((std::declval<std::remove_reference_t<T> const&>() == std::declval<std::remove_reference_t<Cmp> const&>()) && (std::declval<std::remove_reference_t<T> const&>() != std::declval<std::remove_reference_t<Cmp> const&>()), (void)0)>> : std::true_type {}; template <typename T, typename Cmp = T> constexpr bool equality_comparable = is_equality_comparable<T, Cmp>::value; template <typename T> constexpr bool is_reference = std::is_reference<T>::value; template <typename T> using Ref = std::reference_wrapper<T>; template <typename T> using ConstRef = std::reference_wrapper<std::add_const_t<std::remove_reference_t<T>>>; template <typename T> using MutRef = std::reference_wrapper<std::remove_const_t<std::remove_reference_t<T>>>; template <typename T, typename Base> concept Impl = std::is_base_of<Base, T>::value; namespace internal { constexpr int kxPtrFmtSize = static_cast<int>((sizeof(void*) << 1) + 2 + 2); constexpr int kI32FmtSize = 11; constexpr int kU32FmtSize = 10; constexpr int kI16FmtSize = 6; constexpr int kU16FmtSize = 5; constexpr int kI8FmtSize = 4; constexpr int kU8FmtSize = 3; } } } namespace stx { inline namespace v1 { constexpr size_t kReportReserveSize = 128; constexpr char const kReportTruncationMessage[] = "... (error report truncated)"; static_assert(kReportReserveSize > (sizeof(kReportTruncationMessage) - 1), "STX_REPORT_RESERVE_SIZE must contain truncation message"); struct ReportQuery {}; constexpr ReportQuery const report_query{}; struct [[nodiscard]] FixedReport { using storage_type = char[kReportReserveSize]; constexpr FixedReport() noexcept : reserve_{}, report_size_{0} {} constexpr FixedReport(const char* str, size_t size) noexcept : reserve_{}, report_size_{size} { *this = FixedReport(std::string_view(str, size)); } explicit constexpr FixedReport(std::string_view const& info) noexcept : reserve_{}, report_size_{0} { size_t const str_size = info.size(); bool const should_truncate = str_size > kReportReserveSize; size_t const str_clip_size = should_truncate ? (kReportReserveSize - (sizeof(kReportTruncationMessage) - 1)) : str_size; size_t pos = 0; for (; pos < str_clip_size; pos++) { reserve_[pos] = info[pos]; } if (should_truncate) { for (; pos < kReportReserveSize; pos++) { reserve_[pos] = kReportTruncationMessage[pos - str_clip_size]; } } report_size_ = pos; } constexpr FixedReport(FixedReport const&) noexcept = default; constexpr FixedReport(FixedReport &&) noexcept = default; constexpr FixedReport& operator=(FixedReport const&) noexcept = default; constexpr FixedReport& operator=(FixedReport&&) noexcept = default; ~FixedReport() noexcept = default; [[nodiscard]] constexpr std::string_view what() const noexcept { return std::string_view(reserve_, report_size_); }; private: storage_type reserve_; size_t report_size_; }; struct [[nodiscard]] SpanReport { constexpr SpanReport() noexcept : custom_payload_{nullptr}, report_size_{0} {} constexpr SpanReport(const char* str, size_t size) noexcept : custom_payload_{str}, report_size_{size} {} explicit constexpr SpanReport(std::string_view const& str) noexcept : custom_payload_{str.data()}, report_size_{str.size()} {} [[nodiscard]] constexpr std::string_view what() const noexcept { return std::string_view(custom_payload_, report_size_); }; constexpr SpanReport(SpanReport const&) noexcept = default; constexpr SpanReport(SpanReport &&) noexcept = default; constexpr SpanReport& operator=(SpanReport const&) noexcept = default; constexpr SpanReport& operator=(SpanReport&&) noexcept = default; ~SpanReport() noexcept = default; private: const char* custom_payload_; size_t report_size_; }; class [[nodiscard]] ReportPayload { public: explicit constexpr ReportPayload(FixedReport const& report) noexcept : content_{report.what()} {} explicit constexpr ReportPayload(SpanReport const& report) noexcept : content_{report.what()} {} constexpr ReportPayload(ReportPayload const&) noexcept = default; constexpr ReportPayload(ReportPayload &&) noexcept = default; constexpr ReportPayload& operator=(ReportPayload const&) noexcept = default; constexpr ReportPayload& operator=(ReportPayload&&) noexcept = default; ~ReportPayload() noexcept = default; [[nodiscard]] constexpr std::string_view const& data() const noexcept { return content_; } private: std::string_view content_; }; constexpr char const kFormatError[] = "<format error>"; constexpr size_t const kFormatErrorSize = sizeof(kFormatError) - 1; template <typename T> [[nodiscard]] inline SpanReport operator>>(ReportQuery, T const&) noexcept { return SpanReport(); } template <typename T> [[nodiscard]] inline FixedReport operator>>(ReportQuery, T const* const& ptr) noexcept { char fmt_buffer[internal::kxPtrFmtSize + 1]; int fmt_buffer_size = std::snprintf(fmt_buffer, internal::kxPtrFmtSize + 1, "0x%" "l" "x", reinterpret_cast<uintptr_t const>(ptr)); if (fmt_buffer_size < 0 || fmt_buffer_size >= internal::kxPtrFmtSize + 1) { return FixedReport(kFormatError, kFormatErrorSize); } else { return FixedReport(fmt_buffer, fmt_buffer_size); }; } template <typename T> [[nodiscard]] inline FixedReport operator>>(ReportQuery, T* const& ptr) noexcept { char fmt_buffer[internal::kxPtrFmtSize + 1]; int fmt_buffer_size = std::snprintf(fmt_buffer, internal::kxPtrFmtSize + 1, "0x%" "l" "x", reinterpret_cast<uintptr_t>(ptr)); if (fmt_buffer_size < 0 || fmt_buffer_size >= internal::kxPtrFmtSize + 1) { return FixedReport(kFormatError, kFormatErrorSize); } else { return FixedReport(fmt_buffer, fmt_buffer_size); }; } [[nodiscard]] inline FixedReport operator>>(ReportQuery, int8_t const& v) noexcept { char fmt_buffer[internal::kI8FmtSize + 1]; int fmt_buffer_size = std::snprintf(fmt_buffer, internal::kI8FmtSize + 1, "%" "i", v); if (fmt_buffer_size < 0 || fmt_buffer_size >= internal::kI8FmtSize + 1) { return FixedReport(kFormatError, kFormatErrorSize); } else { return FixedReport(fmt_buffer, fmt_buffer_size); }; } [[nodiscard]] inline FixedReport operator>>(ReportQuery, uint8_t const& v) noexcept { char fmt_buffer[internal::kU8FmtSize + 1]; int fmt_buffer_size = std::snprintf(fmt_buffer, internal::kU8FmtSize + 1, "%" "u", v); if (fmt_buffer_size < 0 || fmt_buffer_size >= internal::kU8FmtSize + 1) { return FixedReport(kFormatError, kFormatErrorSize); } else { return FixedReport(fmt_buffer, fmt_buffer_size); }; } [[nodiscard]] inline FixedReport operator>>(ReportQuery, int16_t const& v) noexcept { char fmt_buffer[internal::kI16FmtSize + 1]; int fmt_buffer_size = std::snprintf(fmt_buffer, internal::kI16FmtSize + 1, "%" "i", v); if (fmt_buffer_size < 0 || fmt_buffer_size >= internal::kI16FmtSize + 1) { return FixedReport(kFormatError, kFormatErrorSize); } else { return FixedReport(fmt_buffer, fmt_buffer_size); }; } [[nodiscard]] inline FixedReport operator>>(ReportQuery, uint16_t const& v) noexcept { char fmt_buffer[internal::kU16FmtSize + 1]; int fmt_buffer_size = std::snprintf(fmt_buffer, internal::kU16FmtSize + 1, "%" "u", v); if (fmt_buffer_size < 0 || fmt_buffer_size >= internal::kU16FmtSize + 1) { return FixedReport(kFormatError, kFormatErrorSize); } else { return FixedReport(fmt_buffer, fmt_buffer_size); }; } [[nodiscard]] inline FixedReport operator>>(ReportQuery, int32_t const& v) noexcept { char fmt_buffer[internal::kI32FmtSize + 1]; int fmt_buffer_size = std::snprintf(fmt_buffer, internal::kI32FmtSize + 1, "%" "i", v); if (fmt_buffer_size < 0 || fmt_buffer_size >= internal::kI32FmtSize + 1) { return FixedReport(kFormatError, kFormatErrorSize); } else { return FixedReport(fmt_buffer, fmt_buffer_size); }; } [[nodiscard]] inline FixedReport operator>>(ReportQuery, uint32_t const& v) noexcept { char fmt_buffer[internal::kU32FmtSize + 1]; int fmt_buffer_size = std::snprintf(fmt_buffer, internal::kU32FmtSize + 1, "%" "u", v); if (fmt_buffer_size < 0 || fmt_buffer_size >= internal::kU32FmtSize + 1) { return FixedReport(kFormatError, kFormatErrorSize); } else { return FixedReport(fmt_buffer, fmt_buffer_size); }; } [[nodiscard]] inline SpanReport operator>>(ReportQuery, std::string_view const& v) noexcept { return SpanReport(v); } [[nodiscard]] inline SpanReport operator>>(ReportQuery, std::string const& v) noexcept { return SpanReport(v); } } } namespace stx { inline namespace v1 { struct [[nodiscard]] SourceLocation { static constexpr SourceLocation current( const char* file = __builtin_FILE(), const char* func = __builtin_FUNCTION(), uint_least32_t line = __builtin_LINE(), uint_least32_t column = __builtin_COLUMN() ) noexcept { SourceLocation loc{}; loc.line_ = line; loc.column_ = column; loc.file_ = file; loc.func_ = func; return loc; } constexpr SourceLocation() noexcept : line_(), column_(), file_("\0"), func_("\0") {} constexpr SourceLocation(SourceLocation const& other) noexcept = default; constexpr SourceLocation(SourceLocation && other) noexcept = default; constexpr SourceLocation& operator=(SourceLocation const& other) noexcept = default; constexpr SourceLocation& operator=(SourceLocation&& other) noexcept = default; ~SourceLocation() noexcept = default; constexpr uint_least32_t column() const noexcept { return column_; } constexpr uint_least32_t line() const noexcept { return line_; } constexpr const char* file_name() const noexcept { return file_; } constexpr const char* function_name() const noexcept { return func_; } private: uint_least32_t line_; uint_least32_t column_; const char* file_; const char* func_; }; } } namespace stx { inline namespace v1 { void panic_handler(std::string_view const& info, ReportPayload const& payload, SourceLocation const& location) noexcept; [[noreturn]] [[gnu::visibility("hidden")]] void begin_panic(std::string_view const& info, ReportPayload const& payload, SourceLocation const& location) noexcept; template <typename T> [[noreturn]] [[gnu::always_inline]] inline void panic( std::string_view const& info, T const& value, SourceLocation const& location = SourceLocation::current()) noexcept { begin_panic(info, ReportPayload(report_query >> value), location); } template <typename T = void> [[noreturn]] [[gnu::always_inline]] inline void panic( std::string_view const& info = "explicit panic", SourceLocation const& location = SourceLocation::current()) noexcept { begin_panic(info, ReportPayload(SpanReport()), location); } } } namespace stx { inline namespace v1 { namespace internal { namespace option { [[noreturn]] [[gnu::always_inline]] inline void expect_value_failed( std::string_view const& msg, SourceLocation const& location = SourceLocation::current()) noexcept { panic(msg, location); } [[noreturn]] [[gnu::always_inline]] inline void expect_none_failed( std::string_view const& msg, SourceLocation const& location = SourceLocation::current()) noexcept { panic(msg, location); } [[noreturn]] [[gnu::always_inline]] inline void no_value( SourceLocation const& location = SourceLocation::current()) noexcept { panic("called `Option::unwrap()` on a `None` value", location); } [[noreturn]] [[gnu::always_inline]] inline void no_lref( SourceLocation const& location = SourceLocation::current()) noexcept { panic("called `Option::value()` on a `None` value", location); } [[noreturn]] [[gnu::always_inline]] inline void no_none( SourceLocation const& location = SourceLocation::current()) noexcept { panic("called `Option::unwrap_none()` on a `Some` value", location); } } namespace result { template <typename T> [[noreturn]] [[gnu::always_inline]] inline void expect_value_failed( std::string_view const& msg, T const& err, SourceLocation const& location = SourceLocation::current()) noexcept { panic(msg, err, location); } [[noreturn]] [[gnu::always_inline]] inline void expect_err_failed( std::string_view const& msg, SourceLocation const& location = SourceLocation::current()) noexcept { panic(msg, location); } template <typename T> [[noreturn]] [[gnu::always_inline]] inline void no_value( T const& err, SourceLocation const& location = SourceLocation::current()) noexcept { panic("called `Result::unwrap()` on an `Err` value", err, location); } template <typename T> [[noreturn]] [[gnu::always_inline]] inline void no_lref( T const& err, SourceLocation const& location = SourceLocation::current()) noexcept { panic("called `Result::value()` on an `Err` value", err, location); } [[noreturn]] [[gnu::always_inline]] inline void no_err( SourceLocation const& location = SourceLocation::current()) noexcept { panic("called `Result::unwrap_err()` on an `Ok` value", location); } [[noreturn]] [[gnu::always_inline]] inline void no_err_lref( SourceLocation const& location = SourceLocation::current()) noexcept { panic("called `Result::err_value()` on an `Ok` value", location); } } } } } namespace stx { inline namespace v1 { template <typename T> struct Some; struct NoneType; template <typename T> struct Option; template <typename T> struct Ok; template <typename E> struct Err; template <typename T, typename E> struct Result; namespace internal { namespace option { template <typename T, bool Copyable> struct OptionStorageBasePre; } } struct [[nodiscard]] NoneType { constexpr NoneType() noexcept = default; constexpr NoneType(NoneType const&) noexcept = default; constexpr NoneType(NoneType &&) noexcept = default; constexpr NoneType& operator=(NoneType const&) noexcept = default; constexpr NoneType& operator=(NoneType&&) noexcept = default; constexpr ~NoneType() noexcept = default; [[nodiscard]] constexpr bool operator==(NoneType const&) const noexcept { return true; } [[nodiscard]] constexpr bool operator!=(NoneType const&) const noexcept { return false; } template <typename T> [[nodiscard]] constexpr bool operator==(Some<T> const&) const noexcept { return false; } template <typename T> [[nodiscard]] constexpr bool operator!=(Some<T> const&) const noexcept { return true; } }; constexpr NoneType const None{}; template <typename T> struct [[nodiscard]] Some { static_assert(movable<T>, "Value type 'T' for 'Option<T>' must be movable"); static_assert( !is_reference<T>, "Cannot use a reference for value type 'T' of 'Option<T>' , To prevent " "subtleties use " "type wrappers like std::reference_wrapper (stx::Ref) or any of the " "`stx::ConstRef` or `stx::MutRef` specialized aliases instead"); using value_type = T; explicit constexpr Some(T && value) : value_(std::forward<T&&>(value)) {} constexpr Some(Some && rhs) = default; constexpr Some& operator=(Some&& rhs) = default; constexpr Some(Some const&) = default; constexpr Some& operator=(Some const&) = default; constexpr ~Some() = default; [[nodiscard]] constexpr T const& value() const& noexcept { return value_; } [[nodiscard]] constexpr T& value()& noexcept { return value_; } [[nodiscard]] constexpr T const value() const&& { return std::move(value_); } [[nodiscard]] constexpr T value()&& { return std::move(value_); } template <typename U> [[nodiscard]] constexpr bool operator==(Some<U> const& cmp) const { static_assert(equality_comparable<T, U>); return value() == cmp.value(); } template <typename U> [[nodiscard]] constexpr bool operator!=(Some<U> const& cmp) const { static_assert(equality_comparable<T, U>); return value() != cmp.value(); } [[nodiscard]] constexpr bool operator==(NoneType const&) const noexcept { return false; } [[nodiscard]] constexpr bool operator!=(NoneType const&) const noexcept { return true; } private: T value_; template <typename Tp> friend struct Option; template <typename Tp, bool Copyable> friend struct internal::option::OptionStorageBasePre; }; template <typename T> struct [[nodiscard]] Ok { static_assert(movable<T>, "Value type 'T' for 'Ok<T>' must be movable"); static_assert( !is_reference<T>, "Cannot use a reference for value type 'T' of 'Ok<T>' , To prevent " "subtleties use " "type wrappers like std::reference_wrapper (stx::Ref) or any of the " "`stx::ConstRef` or `stx::MutRef` specialized aliases instead"); using value_type = T; explicit constexpr Ok(T && value) : value_(std::forward<T&&>(value)) {} constexpr Ok(Ok && rhs) = default; constexpr Ok& operator=(Ok&& rhs) = default; constexpr Ok(Ok const&) = default; constexpr Ok& operator=(Ok const&) = default; constexpr ~Ok() = default; template <typename U> [[nodiscard]] constexpr bool operator==(Ok<U> const& cmp) const { static_assert(equality_comparable<T, U>); return value() == cmp.value(); } template <typename U> [[nodiscard]] constexpr bool operator!=(Ok<U> const& cmp) const { static_assert(equality_comparable<T, U>); return value() != cmp.value(); } template <typename U> [[nodiscard]] constexpr bool operator==(Err<U> const&) const noexcept { return false; } template <typename U> [[nodiscard]] constexpr bool operator!=(Err<U> const&) const noexcept { return true; } [[nodiscard]] constexpr T const& value() const& noexcept { return value_; } [[nodiscard]] constexpr T& value()& noexcept { return value_; } [[nodiscard]] constexpr T const value() const&& { return std::move(value_); } [[nodiscard]] constexpr T value()&& { return std::move(value_); } private: T value_; template <typename Tp, typename Er> friend struct Result; }; template <typename E> struct [[nodiscard]] Err { static_assert(movable<E>, "Error type 'E' for 'Err<E>' must be movable"); static_assert( !is_reference<E>, "Cannot use a reference for error type 'E' of 'Err<E>' , To prevent " "subtleties use " "type wrappers like std::reference_wrapper (stx::Ref) or any of the " "`stx::ConstRef` or `stx::MutRef` specialized aliases instead"); using value_type = E; explicit constexpr Err(E && value) : value_(std::forward<E&&>(value)) {} constexpr Err(Err && rhs) = default; constexpr Err& operator=(Err&& rhs) = default; constexpr Err(Err const&) = default; constexpr Err& operator=(Err const&) = default; constexpr ~Err() = default; template <typename F> [[nodiscard]] constexpr bool operator==(Err<F> const& cmp) const { static_assert(equality_comparable<E, F>); return value() == cmp.value(); } template <typename F> [[nodiscard]] constexpr bool operator!=(Err<F> const& cmp) const { static_assert(equality_comparable<E, F>); return value() != cmp.value(); } template <typename F> [[nodiscard]] constexpr bool operator==(Ok<F> const&) const noexcept { return false; } template <typename F> [[nodiscard]] constexpr bool operator!=(Ok<F> const&) const noexcept { return true; } [[nodiscard]] constexpr E const& value() const& noexcept { return value_; } [[nodiscard]] constexpr E& value()& noexcept { return value_; } [[nodiscard]] constexpr E const value() const&& { return std::move(value_); } [[nodiscard]] constexpr E value()&& { return std::move(value_); } private: E value_; template <typename Tp, typename Er> friend struct Result; }; namespace internal { template <typename Fn> struct Finally { Fn fn; constexpr ~Finally() noexcept(noexcept(fn())) { fn(); } }; template <typename Fn> Finally(Fn) -> Finally<Fn>; using std::construct_at; namespace option { template <typename Tp> inline Tp&& unsafe_value_move(Option<Tp>&); template <typename Fn> struct Ftor_If { Fn fn; bool cond = true; constexpr void operator()() noexcept { if (cond) { fn(); } } }; template <typename Fn> Ftor_If(Fn, bool = true) -> Ftor_If<Fn>; struct Ctor { char& ref; constexpr void operator()() noexcept { internal::construct_at(std::addressof(ref)); } }; struct Setter { bool& ref; bool const value; constexpr void operator()() noexcept { ref = value; } }; template <typename T, bool = std::is_trivial_v<T> && constructible<T>> struct OptionStorageBase { private: T storage_value_{}; bool engaged_ = false; public: constexpr OptionStorageBase() noexcept = default; constexpr OptionStorageBase(Some<T> const& val) noexcept : storage_value_{Some<T>(val).value()}, engaged_{true} {} template <typename... Args> constexpr void _emplace(Args&&... args) noexcept(nothrow_constructible<T, Args&&...>) { (void)0; storage_value_ = T(std::forward<Args&&>(args)...); engaged_ = true; } template <typename U> constexpr void _assign(U&& rhs) noexcept(nothrow_assignable<T, U&&>) { (void)0; storage_value_ = std::forward<U&&>(rhs); } constexpr void _destroy() noexcept { (void)0; engaged_ = false; } constexpr T const& _get() const noexcept { (void)0; return storage_value_; } constexpr T& _get_mut() noexcept { (void)0; return storage_value_; } constexpr T&& _get_move()&& noexcept { (void)0; return static_cast<T&&>(storage_value_); } constexpr bool _engaged() const noexcept { return engaged_; } }; template <typename T, bool = std::is_trivially_destructible_v<T>> struct OptionStorageBasePre { union { T storage_value_; char none_{}; }; bool engaged_ = false; constexpr OptionStorageBasePre(){}; template <typename U> constexpr OptionStorageBasePre(U&& val) noexcept( nothrow_constructible<T, decltype((std::declval<U&&>().value_))>) : storage_value_{std::forward<U&&>(val).value_}, engaged_{true} {} }; template <typename T> struct OptionStorageBasePre<T, false> { union { T storage_value_; char none_{}; }; bool engaged_ = false; constexpr ~OptionStorageBasePre() noexcept( nothrow_destructible<T>) { if (engaged_) { static_cast<OptionStorageBase<T>&>(*this)._destroy(); } } constexpr OptionStorageBasePre(){}; template <typename U> constexpr OptionStorageBasePre(U&& val) noexcept( nothrow_constructible<T, decltype((std::declval<U&&>().value_))>) : storage_value_{std::forward<U&&>(val).value_}, engaged_{true} {} }; template <typename T> struct OptionStorageBase<T, false> : OptionStorageBasePre<T> { using OptionStorageBasePre<T>::OptionStorageBasePre; private: using OptionStorageBasePre<T>::storage_value_; using OptionStorageBasePre<T>::none_; using OptionStorageBasePre<T>::engaged_; public: OptionStorageBase() = default; template <typename... Args> constexpr void _emplace(Args&&... args) noexcept(nothrow_constructible<T, Args&&...>) { (void)0; auto r = Finally{Ftor_If{Ctor{none_}, true}}; std::destroy_at(std::addressof(none_)); internal::construct_at( std::addressof(storage_value_), std::forward<Args&&>(args)...); r.fn.cond = false; engaged_ = true; } template <typename U> constexpr void _assign(U&& rhs) noexcept(nothrow_assignable<T, U&&>) { (void)0; storage_value_ = std::forward<U&&>(rhs); } constexpr void _destroy() noexcept(nothrow_destructible<T>) { (void)0; auto r = Finally{Ctor{none_}}; engaged_ = false; std::destroy_at(std::addressof(storage_value_)); } constexpr T const& _get() const noexcept { (void)0; return *std::launder(std::addressof(storage_value_)); } constexpr T& _get_mut() noexcept { (void)0; return *std::launder(std::addressof(storage_value_)); } constexpr T&& _get_move() && noexcept { (void)0; return static_cast<T&&>(*std::launder(std::addressof(storage_value_))); } constexpr bool _engaged() const noexcept { return engaged_; } }; template < typename T, bool = std::is_trivially_copy_constructible_v<T> || !constructible<T, T const&>> struct OptionCopyCtorBase : OptionStorageBase<T> { using OptionStorageBase<T>::OptionStorageBase; OptionCopyCtorBase() = default; OptionCopyCtorBase(OptionCopyCtorBase const& rhs) = default; OptionCopyCtorBase(OptionCopyCtorBase&& rhs) = default; OptionCopyCtorBase& operator=(OptionCopyCtorBase const& rhs) = default; OptionCopyCtorBase& operator=(OptionCopyCtorBase&& rhs) = default; }; template <typename T> struct OptionCopyCtorBase<T, false> : OptionStorageBase<T> { using OptionStorageBase<T>::OptionStorageBase; OptionCopyCtorBase() = default; OptionCopyCtorBase(OptionCopyCtorBase&& rhs) = default; OptionCopyCtorBase& operator=(OptionCopyCtorBase const& rhs) = default; OptionCopyCtorBase& operator=(OptionCopyCtorBase&& rhs) = default; constexpr OptionCopyCtorBase(OptionCopyCtorBase const& rhs) noexcept( nothrow_constructible<T, T const&>) { if (rhs._engaged()) { this->_emplace(rhs._get()); } } }; template < typename T, bool = std::is_trivially_move_constructible_v<T> || !constructible<T, T&&>> struct OptionMoveCtorBase : OptionCopyCtorBase<T> { using OptionCopyCtorBase<T>::OptionCopyCtorBase; OptionMoveCtorBase() = default; OptionMoveCtorBase(OptionMoveCtorBase const& rhs) = default; OptionMoveCtorBase(OptionMoveCtorBase&& rhs) = default; OptionMoveCtorBase& operator=(OptionMoveCtorBase const& rhs) = default; OptionMoveCtorBase& operator=(OptionMoveCtorBase&& rhs) = default; }; template <typename T> struct OptionMoveCtorBase<T, false> : OptionCopyCtorBase<T> { using OptionCopyCtorBase<T>::OptionCopyCtorBase; OptionMoveCtorBase() = default; OptionMoveCtorBase(OptionMoveCtorBase const& rhs) = default; OptionMoveCtorBase& operator=(OptionMoveCtorBase const& rhs) = default; OptionMoveCtorBase& operator=(OptionMoveCtorBase&& rhs) = default; constexpr OptionMoveCtorBase(OptionMoveCtorBase&& rhs) noexcept( nothrow_constructible<T, T&&>&& nothrow_destructible<T>) { if (rhs._engaged()) { this->_emplace(std::move(rhs)._get_move()); rhs._destroy(); } } }; template < typename T, bool = std::is_trivially_copy_assignable_v<T> || !assignable<T, T const&> > struct OptionCopyAssignBase : OptionMoveCtorBase<T> { using OptionMoveCtorBase<T>::OptionMoveCtorBase; OptionCopyAssignBase() = default; OptionCopyAssignBase(OptionCopyAssignBase const& rhs) = default; OptionCopyAssignBase(OptionCopyAssignBase&& rhs) = default; OptionCopyAssignBase& operator=(OptionCopyAssignBase const& rhs) = default; OptionCopyAssignBase& operator=(OptionCopyAssignBase&& rhs) = default; }; template <typename T> struct OptionCopyAssignBase<T, false> : OptionMoveCtorBase<T> { using OptionMoveCtorBase<T>::OptionMoveCtorBase; OptionCopyAssignBase() = default; OptionCopyAssignBase(OptionCopyAssignBase const& rhs) = default; OptionCopyAssignBase(OptionCopyAssignBase&& rhs) = default; OptionCopyAssignBase& operator=(OptionCopyAssignBase&& rhs) = default; constexpr OptionCopyAssignBase& operator=(OptionCopyAssignBase const& rhs) noexcept( nothrow_assignable<T, T const&>&& nothrow_constructible<T, T const&>&& nothrow_destructible<T>) { if (rhs._engaged() && this->_engaged()) { this->_assign(rhs._get()); } else if (this->_engaged()) { this->_destroy(); } else { this->_emplace(rhs._get()); } return *this; } }; template < typename T, bool = std::is_trivially_move_assignable_v<T> || !assignable<T, T&&> > struct OptionMoveAssignBase : OptionCopyAssignBase<T> { using OptionCopyAssignBase<T>::OptionCopyAssignBase; OptionMoveAssignBase() = default; OptionMoveAssignBase(OptionMoveAssignBase const& rhs) = default; OptionMoveAssignBase(OptionMoveAssignBase&& rhs) = default; OptionMoveAssignBase& operator=(OptionMoveAssignBase const& rhs) = default; OptionMoveAssignBase& operator=(OptionMoveAssignBase&& rhs) = default; }; template <typename T> struct OptionMoveAssignBase<T, false> : OptionCopyAssignBase<T> { using OptionCopyAssignBase<T>::OptionCopyAssignBase; OptionMoveAssignBase() = default; OptionMoveAssignBase(OptionMoveAssignBase const& rhs) = default; OptionMoveAssignBase(OptionMoveAssignBase&& rhs) = default; OptionMoveAssignBase& operator=(OptionMoveAssignBase const& rhs) = default; constexpr OptionMoveAssignBase& operator=(OptionMoveAssignBase&& rhs) noexcept( nothrow_assignable<T, T&&>&& nothrow_constructible<T, T&&>&& nothrow_destructible<T>) { if (rhs._engaged()) { if (this->_engaged()) { this->_assign(std::move(rhs)._get_move()); } else { this->_emplace(std::move(rhs)._get_move()); } rhs._destroy(); } else { if (this->_engaged()) { this->_destroy(); } } return *this; } }; } } template <typename T> struct [[nodiscard]] Option: private internal::option::OptionMoveAssignBase<T> { public: using value_type = T; static_assert(movable<T>, "Value type 'T' for 'Option<T>' must be movable"); static_assert( !is_reference<T>, "Cannot use a reference for value type 'T' of 'Option<T>' , To prevent " "subtleties use " "type wrappers like std::reference_wrapper (stx::Ref) or any of the " "`stx::ConstRef` or `stx::MutRef` specialized aliases instead"); constexpr Option() noexcept = default; using internal::option::OptionMoveAssignBase<T>::OptionMoveAssignBase; constexpr Option(NoneType const&) noexcept : Option{} {} template <typename U> [[nodiscard]] constexpr bool operator==(Option<U> const& cmp) const { static_assert(equality_comparable<T, U>); if (is_some() && cmp.is_some()) { return value_cref_() == cmp.value_cref_(); } else if (is_none() && cmp.is_none()) { return true; } else { return false; } } template <typename U> [[nodiscard]] constexpr bool operator!=(Option<U> const& cmp) const { static_assert(equality_comparable<T, U>); if (is_some() && cmp.is_some()) { return value_cref_() != cmp.value_cref_(); } else if (is_none() && cmp.is_none()) { return false; } else { return true; } } template <typename U> [[nodiscard]] constexpr bool operator==(Some<U> const& cmp) const { static_assert(equality_comparable<T, U>); if (is_some()) { return value_cref_() == cmp.value(); } else { return false; } } template <typename U> [[nodiscard]] constexpr bool operator!=(Some<U> const& cmp) const { static_assert(equality_comparable<T, U>); if (is_some()) { return value_cref_() != cmp.value(); } else { return true; } } [[nodiscard]] constexpr bool operator==(NoneType const&) const noexcept { return is_none(); } [[nodiscard]] constexpr bool operator!=(NoneType const&) const noexcept { return is_some(); } [[nodiscard]] constexpr bool is_some() const noexcept { return !is_none(); } [[nodiscard]] constexpr bool is_none() const noexcept { return !this->_engaged(); } [[nodiscard]] constexpr operator bool() const noexcept { return is_some(); } template <typename CmpType> [[nodiscard]] constexpr bool contains(CmpType const& cmp) const { static_assert(equality_comparable<T, CmpType>); if (is_some()) { return value_cref_() == cmp; } else { return false; } } template <typename UnaryPredicate> [[nodiscard]] constexpr bool exists(UnaryPredicate && predicate) const { static_assert(invocable<UnaryPredicate&&, T const&>); static_assert(convertible<invoke_result<UnaryPredicate&&, T const&>, bool>); if (is_some()) { return std::forward<UnaryPredicate&&>(predicate)(value_cref_()); } else { return false; } } [[nodiscard]] constexpr T& value()& noexcept { if (is_none()) internal::option::no_lref(); return value_ref_(); } [[nodiscard]] constexpr T const& value() const& noexcept { if (is_none()) internal::option::no_lref(); return value_cref_(); } [[deprecated("Use `unwrap()` instead")]] T value()&& = delete; [[deprecated("Use `unwrap()` instead")]] T const value() const&& = delete; [[nodiscard]] constexpr auto as_cref() const& noexcept->Option<ConstRef<T>> { if (is_some()) { return Some<ConstRef<T>>(ConstRef<T>(value_cref_())); } else { return None; } } [[deprecated( "calling Option::as_cref() on an r-value, and therefore binding a " "reference to an object that is marked to be moved")]] [[nodiscard]] constexpr auto as_cref() const&& noexcept->Option<ConstRef<T>> = delete; [[nodiscard]] constexpr auto as_ref()& noexcept->Option<MutRef<T>> { if (is_some()) { return Some<MutRef<T>>(MutRef<T>(value_ref_())); } else { return None; } } [[nodiscard]] constexpr auto as_ref() const& noexcept->Option<ConstRef<T>> { return as_cref(); } [[deprecated( "calling Option::as_ref() on an r-value, and therefore binding a " "reference to an object that is marked to be moved")]] [[nodiscard]] auto as_ref()&& noexcept->Option<MutRef<T>> = delete; [[deprecated( "calling Option::as_ref() on an r-value, and therefore binding a " "reference to an object that is marked to be moved")]] [[nodiscard]] auto as_ref() const&& noexcept->Option<ConstRef<T>> = delete; [[nodiscard]] constexpr auto expect(std::string_view const& msg)&&->T { if (is_some()) { return std::move(value_ref_()); } else { internal::option::expect_value_failed(msg); } } [[nodiscard]] constexpr auto unwrap()&&->T { if (is_some()) { return std::move(value_ref_()); } else { internal::option::no_value(); } } [[nodiscard]] constexpr auto unwrap_or(T && alt)&&->T { if (is_some()) { return std::move(value_ref_()); } else { return std::move(alt); } } template <typename Fn> [[nodiscard]] constexpr auto unwrap_or_else(Fn && op)&&->T { static_assert(invocable<Fn&&>); if (is_some()) { return std::move(value_ref_()); } else { return std::forward<Fn&&>(op)(); } } template <typename Fn> [[nodiscard]] constexpr auto map(Fn && op)&&->Option<invoke_result<Fn&&, T&&>> { static_assert(invocable<Fn&&, T&&>); if (is_some()) { return Some<invoke_result<Fn&&, T&&>>( std::forward<Fn&&>(op)(std::move(value_ref_()))); } else { return None; } } template <typename Fn, typename A> [[nodiscard]] constexpr auto map_or(Fn && op, A && alt)&&->invoke_result<Fn&&, T&&> { static_assert(invocable<Fn&&, T&&>); if (is_some()) { return std::forward<Fn&&>(op)(std::move(value_ref_())); } else { return std::forward<A&&>(alt); } } template <typename Fn, typename AltFn> [[nodiscard]] constexpr auto map_or_else( Fn && op, AltFn && alt)&&->invoke_result<Fn&&, T&&> { static_assert(invocable<Fn&&, T&&>); static_assert(invocable<AltFn&&>); if (is_some()) { return std::forward<Fn&&>(op)(std::move(value_ref_())); } else { return std::forward<AltFn&&>(alt)(); } } template <typename E> [[nodiscard]] constexpr auto ok_or(E error)&&->Result<T, E> { if (is_some()) { return Ok<T>(std::move(value_ref_())); } else { return Err<E>(std::forward<E>(error)); } } template <typename Fn> [[nodiscard]] constexpr auto ok_or_else( Fn && op)&&->Result<T, invoke_result<Fn&&>> { static_assert(invocable<Fn&&>); if (is_some()) { return Ok<T>(std::move(value_ref_())); } else { return Err<invoke_result<Fn&&>>(std::forward<Fn&&>(op)()); } } template <typename U> [[nodiscard]] constexpr auto AND(Option<U> && cmp)&&->Option<U> { if (is_some()) { return std::forward<Option<U>&&>(cmp); } else { return None; } } template <typename Fn> [[nodiscard]] constexpr auto and_then(Fn && op)&&->invoke_result<Fn&&, T&&> { static_assert(invocable<Fn&&, T&&>); if (is_some()) { return std::forward<Fn&&>(op)(std::move(value_ref_())); } else { return None; } } template <typename UnaryPredicate> [[nodiscard]] constexpr auto filter(UnaryPredicate && predicate)&&->Option { static_assert(invocable<UnaryPredicate&&, T const&>); static_assert(convertible<invoke_result<UnaryPredicate&&, T const&>, bool>); if (is_some() && std::forward<UnaryPredicate&&>(predicate)(value_cref_())) { return std::move(*this); } else { return None; } } template <typename UnaryPredicate> [[nodiscard]] constexpr auto filter_not(UnaryPredicate && predicate)&&->Option { static_assert(invocable<UnaryPredicate&&, T const&>); static_assert(convertible<invoke_result<UnaryPredicate&&, T const&>, bool>); if (is_some() && !std::forward<UnaryPredicate&&>(predicate)(value_cref_())) { return std::move(*this); } else { return None; } } [[nodiscard]] constexpr auto OR(Option && alt)&&->Option { if (is_some()) { return std::move(*this); } else { return std::move(alt); } } template <typename Fn> [[nodiscard]] constexpr auto or_else(Fn && op)&&->Option { static_assert(invocable<Fn&&>); if (is_some()) { return std::move(*this); } else { return std::forward<Fn&&>(op)(); } } [[nodiscard]] constexpr auto XOR(Option && alt)&&->Option { if (is_some() && alt.is_none()) { return std::move(*this); } else if (is_none() && alt.is_some()) { return std::move(alt); } else { return None; } } [[nodiscard]] constexpr auto take()->Option { if (is_some()) { auto some = Some<T>(std::move(value_ref_())); this->_destroy(); return some; } else { return None; } } [[nodiscard]] constexpr auto replace(T && replacement)->Option { if (is_some()) { std::swap(replacement, value_ref_()); return Some<T>(std::move(replacement)); } else { this->_emplace(std::move(replacement)); return None; } } [[nodiscard]] constexpr auto replace(T const& replacement)->Option { static_assert(copy_constructible<T>); if (is_some()) { T copy = replacement; std::swap(copy, value_ref_()); return Some<T>(std::move(copy)); } else { this->_emplace(replacement); return None; } } [[nodiscard]] constexpr auto clone() const->Option { static_assert(copy_constructible<T>); if (is_some()) { return Some<T>(std::move(T(value_cref_()))); } else { return None; } } void constexpr expect_none(std::string_view const& msg)&& { if (is_some()) { internal::option::expect_none_failed(msg); } } void constexpr unwrap_none()&& { if (is_some()) { internal::option::no_none(); } } [[nodiscard]] constexpr auto unwrap_or_default()&&->T { static_assert(default_constructible<T>); if (is_some()) { return std::move(value_ref_()); } else { return T(); } } template <typename SomeFn, typename NoneFn> [[nodiscard]] constexpr auto match( SomeFn && some_fn, NoneFn && none_fn)&&->invoke_result<SomeFn&&, T&&> { static_assert(invocable<SomeFn&&, T&&>); static_assert(invocable<NoneFn&&>); if (is_some()) { return std::forward<SomeFn&&>(some_fn)(std::move(value_ref_())); } else { return std::forward<NoneFn&&>(none_fn)(); } } template <typename SomeFn, typename NoneFn> [[nodiscard]] constexpr auto match( SomeFn && some_fn, NoneFn && none_fn)&->invoke_result<SomeFn&&, T&> { static_assert(invocable<SomeFn&&, T&>); static_assert(invocable<NoneFn&&>); if (is_some()) { return std::forward<SomeFn&&>(some_fn)(value_ref_()); } else { return std::forward<NoneFn&&>(none_fn)(); } } template <typename SomeFn, typename NoneFn> [[nodiscard]] constexpr auto match(SomeFn && some_fn, NoneFn && none_fn) const&->invoke_result<SomeFn&&, T const&> { static_assert(invocable<SomeFn&&, T const&>); static_assert(invocable<NoneFn&&>); if (is_some()) { return std::forward<SomeFn&&>(some_fn)(value_cref_()); } else { return std::forward<NoneFn&&>(none_fn)(); } } private: [[nodiscard]] constexpr T& value_ref_() { return this->_get_mut(); } [[nodiscard]] constexpr T const& value_cref_() const { return this->_get(); } template <typename Tp> friend Tp&& internal::option::unsafe_value_move(Option<Tp>&); }; template <typename T> Option(Some<T>) -> Option<T>; template <typename U, typename T> [[nodiscard]] [[gnu::always_inline]] inline constexpr bool operator==( Some<U> const& cmp, Option<T> const& option) { return option == cmp; } template <typename U, typename T> [[nodiscard]] [[gnu::always_inline]] inline constexpr bool operator!=( Some<U> const& cmp, Option<T> const& option) { return option != cmp; } template <typename T> [[nodiscard]] [[gnu::always_inline]] inline constexpr bool operator==( NoneType const&, Option<T> const& option) noexcept { return option.is_none(); } template <typename T> [[nodiscard]] [[gnu::always_inline]] inline constexpr bool operator!=( NoneType const&, Option<T> const& option) noexcept { return option.is_some(); } namespace internal { namespace result { template <typename Tp, typename Er> inline Tp&& unsafe_value_move(Result<Tp, Er>&); template <typename Tp, typename Er> inline Er&& unsafe_err_move(Result<Tp, Er>&); } } template <typename T, typename E> struct [[nodiscard]] Result { public: static_assert(movable<T>, "Value type 'T' for 'Result<T, E>' must be movable"); static_assert(movable<E>, "Error type 'E' for 'Result<T, E>' must be movable"); static_assert( !is_reference<T>, "Cannot use a reference for value type 'T' of 'Result<T, E>', To prevent " "subtleties use " "type wrappers like std::reference_wrapper (stx::Ref) or any of the " "`stx::ConstRef` or `stx::MutRef` specialized aliases instead"); static_assert( !is_reference<E>, "Cannot use a reference for error type 'E' of 'Result<T, E>', To prevent " "subtleties use " "type wrappers like std::reference_wrapper (stx::Ref) or any of the " "`stx::ConstRef` or `stx::MutRef` specialized aliases instead"); using value_type = T; using error_type = E; constexpr Result(Ok<T> && result) : storage_value_(std::forward<T>(result.value_)), is_ok_(true) {} constexpr Result(Err<E> && err) : storage_err_(std::forward<E>(err.value_)), is_ok_(false) {} constexpr Result(Result && rhs) : is_ok_(rhs.is_ok_) { if (rhs.is_ok()) { internal::construct_at(std::addressof(storage_value_), std::move(rhs.storage_value_)); } else { internal::construct_at(std::addressof(storage_err_), std::move(rhs.storage_err_)); } } constexpr Result& operator=(Result&& rhs) { if (is_ok() && rhs.is_ok()) { std::swap(value_ref_(), rhs.value_ref_()); } else if (is_ok() && rhs.is_err()) { storage_value_.~T(); internal::construct_at(std::addressof(storage_err_), std::move(rhs.storage_err_)); is_ok_ = false; } else if (is_err() && rhs.is_ok()) { storage_err_.~E(); internal::construct_at(std::addressof(storage_value_), std::move(rhs.storage_value_)); is_ok_ = true; } else { std::swap(err_ref_(), rhs.err_ref_()); } return *this; } Result() = delete; Result(Result const& rhs) = delete; Result& operator=(Result const& rhs) = delete; constexpr ~Result() noexcept { if (is_ok()) { storage_value_.~T(); } else { storage_err_.~E(); } }; template <typename U> [[nodiscard]] constexpr bool operator==(Ok<U> const& cmp) const { static_assert(equality_comparable<T, U>); if (is_ok()) { return value_cref_() == cmp.value(); } else { return false; } } template <typename U> [[nodiscard]] constexpr bool operator!=(Ok<U> const& cmp) const { static_assert(equality_comparable<T, U>); if (is_ok()) { return value_cref_() != cmp.value(); } else { return true; } } template <typename F> [[nodiscard]] constexpr bool operator==(Err<F> const& cmp) const { static_assert(equality_comparable<E, F>); if (is_ok()) { return false; } else { return err_cref_() == cmp.value(); } } template <typename F> [[nodiscard]] constexpr bool operator!=(Err<F> const& cmp) const { static_assert(equality_comparable<E, F>); if (is_ok()) { return true; } else { return err_cref_() != cmp.value(); } } template <typename U, typename F> [[nodiscard]] constexpr bool operator==(Result<U, F> const& cmp) const { static_assert(equality_comparable<T, U>); static_assert(equality_comparable<E, F>); if (is_ok() && cmp.is_ok()) { return value_cref_() == cmp.value_cref_(); } else if (is_err() && cmp.is_err()) { return err_cref_() == cmp.err_cref_(); } else { return false; } } template <typename U, typename F> [[nodiscard]] constexpr bool operator!=(Result<U, F> const& cmp) const { static_assert(equality_comparable<T, U>); static_assert(equality_comparable<E, F>); if (is_ok() && cmp.is_ok()) { return value_cref_() != cmp.value_cref_(); } else if (is_err() && cmp.is_err()) { return err_cref_() != cmp.err_cref_(); } else { return true; } } [[nodiscard]] constexpr bool is_ok() const noexcept { return is_ok_; } [[nodiscard]] constexpr bool is_err() const noexcept { return !is_ok(); } [[nodiscard]] constexpr operator bool() const noexcept { return is_ok(); } template <typename CmpType> [[nodiscard]] constexpr bool contains(CmpType const& cmp) const { static_assert(equality_comparable<T, CmpType>); if (is_ok()) { return value_cref_() == cmp; } else { return false; } } template <typename ErrCmp> [[nodiscard]] constexpr bool contains_err(ErrCmp const& cmp) const { static_assert(equality_comparable<E, ErrCmp>); if (is_ok()) { return false; } else { return err_cref_() == cmp; } } template <typename UnaryPredicate> [[nodiscard]] constexpr bool exists(UnaryPredicate && predicate) const { static_assert(invocable<UnaryPredicate&&, T const&>); static_assert(convertible<invoke_result<UnaryPredicate&&, T const&>, bool>); if (is_ok()) { return std::forward<UnaryPredicate&&>(predicate)(value_cref_()); } else { return false; } } template <typename UnaryPredicate> [[nodiscard]] constexpr bool err_exists(UnaryPredicate && predicate) const { static_assert(invocable<UnaryPredicate&&, E const&>); static_assert(convertible<invoke_result<UnaryPredicate&&, E const&>, bool>); if (is_err()) { return std::forward<UnaryPredicate&&>(predicate)(err_cref_()); } else { return false; } } [[nodiscard]] T& value()& noexcept { if (is_err()) internal::result::no_lref(err_cref_()); return value_ref_(); } [[nodiscard]] T const& value() const& noexcept { if (is_err()) internal::result::no_lref(err_cref_()); return value_cref_(); } [[deprecated("Use `unwrap()` instead")]] T value()&& = delete; [[deprecated("Use `unwrap()` instead")]] T const value() const&& = delete; [[nodiscard]] E& err_value()& noexcept { if (is_ok()) internal::result::no_err_lref(); return err_ref_(); } [[nodiscard]] E const& err_value() const& noexcept { if (is_ok()) internal::result::no_err_lref(); return err_cref_(); } [[deprecated("Use `unwrap_err()` instead")]] E err_value()&& = delete; [[deprecated("Use `unwrap_err()` instead")]] E const err_value() const&& = delete; [[nodiscard]] constexpr auto ok()&&->Option<T> { if (is_ok()) { return Some<T>(std::move(value_ref_())); } else { return None; } } [[nodiscard]] constexpr auto err()&&->Option<E> { if (is_ok()) { return None; } else { return Some<E>(std::move(err_ref_())); } } [[nodiscard]] constexpr auto as_cref() const& noexcept->Result<ConstRef<T>, ConstRef<E>> { if (is_ok()) { return Ok<ConstRef<T>>(ConstRef<T>(value_cref_())); } else { return Err<ConstRef<E>>(ConstRef<E>(err_cref_())); } } [[deprecated( "calling Result::as_cref() on an r-value, and " "therefore binding an l-value reference to an object that is marked to " "be moved")]] [[nodiscard]] constexpr auto as_cref() const&& noexcept->Result<ConstRef<T>, ConstRef<E>> = delete; [[nodiscard]] constexpr auto as_ref()& noexcept ->Result<MutRef<T>, MutRef<E>> { if (is_ok()) { return Ok<MutRef<T>>(MutRef<T>(value_ref_())); } else { return Err<MutRef<E>>(MutRef<E>(err_ref_())); } } [[nodiscard]] constexpr auto as_ref() const& noexcept->Result<ConstRef<T>, ConstRef<E>> { return as_cref(); } [[deprecated( "calling Result::as_ref() on an r-value, and therefore binding a " "reference to an object that is marked to be moved")]] [[nodiscard]] constexpr auto as_ref()&& noexcept->Result<MutRef<T>, MutRef<E>> = delete; [[deprecated( "calling Result::as_ref() on an r-value, and therefore binding a " "reference to an object that is marked to be moved")]] [[nodiscard]] constexpr auto as_ref() const&& noexcept->Result<ConstRef<T>, ConstRef<E>> = delete; template <typename Fn> [[nodiscard]] constexpr auto map(Fn && op)&&->Result<invoke_result<Fn&&, T&&>, E> { static_assert(invocable<Fn&&, T&&>); if (is_ok()) { return Ok<invoke_result<Fn&&, T&&>>( std::forward<Fn&&>(op)(std::move(value_ref_()))); } else { return Err<E>(std::move(err_ref_())); } } template <typename Fn, typename AltType> [[nodiscard]] constexpr auto map_or( Fn && op, AltType && alt)&&->invoke_result<Fn&&, T&&> { static_assert(invocable<Fn&&, T&&>); if (is_ok()) { return std::forward<Fn&&>(op)(std::move(value_ref_())); } else { return std::forward<AltType&&>(alt); } } template <typename Fn, typename A> [[nodiscard]] constexpr auto map_or_else( Fn && op, A && alt_op)&&->invoke_result<Fn&&, T&&> { static_assert(invocable<Fn&&, T&&>); static_assert(invocable<A&&, E&&>); if (is_ok()) { return std::forward<Fn&&>(op)(std::move(value_ref_())); } else { return std::forward<A&&>(alt_op)(std::move(err_ref_())); } } template <typename Fn> [[nodiscard]] constexpr auto map_err( Fn && op)&&->Result<T, invoke_result<Fn&&, E&&>> { static_assert(invocable<Fn&&, E&&>); if (is_ok()) { return Ok<T>(std::move(value_ref_())); } else { return Err<invoke_result<Fn&&, E&&>>( std::forward<Fn&&>(op)(std::move(err_ref_()))); } } template <typename U, typename F> [[nodiscard]] constexpr auto AND(Result<U, F> && res)&&->Result<U, F> { static_assert(convertible<E&&, F>); if (is_ok()) { return std::forward<Result<U, F>&&>(res); } else { return Err<F>(std::move(static_cast<F>(std::move(err_ref_())))); } } template <typename Fn> [[nodiscard]] constexpr auto and_then( Fn && op)&&->Result<invoke_result<Fn&&, T&&>, E> { static_assert(invocable<Fn&&, T&&>); if (is_ok()) { return Ok<invoke_result<Fn&&, T&&>>( std::forward<Fn&&>(op)(std::move(value_ref_()))); } else { return Err<E>(std::move(err_ref_())); } } template <typename U, typename F> [[nodiscard]] constexpr auto OR(Result<U, F> && alt)&&->Result<U, F> { static_assert(convertible<T&&, U>); if (is_ok()) { return Ok<U>(std::move(static_cast<U>(std::move(value_ref_())))); } else { return std::forward<Result<U, F>&&>(alt); } } template <typename Fn> [[nodiscard]] constexpr auto or_else(Fn && op)&&->invoke_result<Fn&&, E&&> { static_assert(invocable<Fn&&, E&&>); if (is_ok()) { return Ok<T>(std::move(value_ref_())); } else { return std::forward<Fn&&>(op)(std::move(err_ref_())); } } [[nodiscard]] constexpr auto unwrap_or(T && alt)&&->T { if (is_ok()) { return std::move(value_ref_()); } else { return std::move(alt); } } template <typename Fn> [[nodiscard]] constexpr auto unwrap_or_else(Fn && op)&&->T { static_assert(invocable<Fn&&, E&&>); if (is_ok()) { return std::move(value_ref_()); } else { return std::forward<Fn&&>(op)(std::move(err_ref_())); } } [[nodiscard]] constexpr auto unwrap()&&->T { if (is_err()) { internal::result::no_value(err_cref_()); } return std::move(value_ref_()); } [[nodiscard]] constexpr auto expect(std::string_view const& msg)&&->T { if (is_err()) { internal::result::expect_value_failed(msg, err_cref_()); } return std::move(value_ref_()); } [[nodiscard]] constexpr auto unwrap_err()&&->E { if (is_ok()) { internal::result::no_err(); } return std::move(err_ref_()); } [[nodiscard]] constexpr auto expect_err(std::string_view const& msg)&&->E { if (is_ok()) { internal::result::expect_err_failed(msg); } return std::move(err_ref_()); } [[nodiscard]] constexpr auto unwrap_or_default()&&->T { static_assert(default_constructible<T>); if (is_ok()) { return std::move(value_ref_()); } else { return T(); } } template <typename OkFn, typename ErrFn> [[nodiscard]] constexpr auto match( OkFn && ok_fn, ErrFn && err_fn)&&->invoke_result<OkFn&&, T&&> { static_assert(invocable<OkFn&&, T&&>); static_assert(invocable<ErrFn&&, E&&>); if (is_ok()) { return std::forward<OkFn&&>(ok_fn)(std::move(value_ref_())); } else { return std::forward<ErrFn&&>(err_fn)(std::move(err_ref_())); } } template <typename OkFn, typename ErrFn> [[nodiscard]] constexpr auto match( OkFn && ok_fn, ErrFn && err_fn)&->invoke_result<OkFn&&, T&> { static_assert(invocable<OkFn&&, T&>); static_assert(invocable<ErrFn&&, E&>); if (is_ok()) { return std::forward<OkFn&&>(ok_fn)(value_ref_()); } else { return std::forward<ErrFn&&>(err_fn)(err_ref_()); } } template <typename OkFn, typename ErrFn> [[nodiscard]] constexpr auto match(OkFn && ok_fn, ErrFn && err_fn) const&->invoke_result<OkFn&&, T const&> { static_assert(invocable<OkFn&&, T const&>); static_assert(invocable<ErrFn&&, E const&>); if (is_ok()) { return std::forward<OkFn&&>(ok_fn)(value_cref_()); } else { return std::forward<ErrFn&&>(err_fn)(err_cref_()); } } [[nodiscard]] constexpr auto clone() const->Result<T, E> { static_assert(copy_constructible<T>); static_assert(copy_constructible<E>); if (is_ok()) { return Ok<T>(std::move(T(value_cref_()))); } else { return Err<E>(std::move(E(err_cref_()))); } } private: union { T storage_value_; E storage_err_; }; bool is_ok_; [[nodiscard]] constexpr T& value_ref_() noexcept { return storage_value_; } [[nodiscard]] constexpr T const& value_cref_() const noexcept { return storage_value_; } [[nodiscard]] constexpr E& err_ref_() noexcept { return storage_err_; } [[nodiscard]] constexpr E const& err_cref_() const noexcept { return storage_err_; } template <typename Tp, typename Er> friend Tp&& internal::result::unsafe_value_move(Result<Tp, Er>&); template <typename Tp, typename Er> friend Er&& internal::result::unsafe_err_move(Result<Tp, Er>&); }; template <typename U, typename T, typename E> [[nodiscard]] [[gnu::always_inline]] inline constexpr bool operator==( Ok<U> const& cmp, Result<T, E> const& result) { return result == cmp; } template <typename U, typename T, typename E> [[nodiscard]] [[gnu::always_inline]] inline constexpr bool operator!=( Ok<U> const& cmp, Result<T, E> const& result) { return result != cmp; } template <typename F, typename T, typename E> [[nodiscard]] [[gnu::always_inline]] inline constexpr bool operator==( Err<F> const& cmp, Result<T, E> const& result) { return result == cmp; } template <typename F, typename T, typename E> [[nodiscard]] [[gnu::always_inline]] inline constexpr bool operator!=( Err<F> const& cmp, Result<T, E> const& result) { return result != cmp; } template <typename T> [[nodiscard]] [[gnu::always_inline]] inline constexpr auto make_some(T value) -> Option<T> { return Some<T>(std::forward<T>(value)); } template <typename T> [[nodiscard]] [[gnu::always_inline]] inline constexpr auto make_none() noexcept -> Option<T> { return None; } template <typename T, typename E> [[nodiscard]] [[gnu::always_inline]] inline constexpr auto make_ok(T value) -> Result<T, E> { return Ok<T>(std::forward<T>(value)); } template <typename T, typename E> [[nodiscard]] [[gnu::always_inline]] inline constexpr auto make_err(E err) -> Result<T, E> { return Err<E>(std::forward<E>(err)); } template <typename T> [[gnu::always_inline]] inline constexpr auto some_ref(T& value) noexcept { return Some<Ref<T>>(std::forward<T&>(value)); } template <typename T> [[gnu::always_inline]] inline constexpr auto ok_ref(T& value) noexcept { return Ok<Ref<T>>(std::forward<T&>(value)); } template <typename E> [[gnu::always_inline]] inline constexpr auto err_ref(E& value) noexcept { return Err<Ref<E>>(std::forward<E&>(value)); } } } namespace stx { inline namespace v1 { template <typename Tp, typename Er> [[gnu::always_inline]] inline Tp&& internal::result::unsafe_value_move( Result<Tp, Er>& result) { return std::move(result.value_ref_()); } template <typename Tp, typename Er> [[gnu::always_inline]] inline Er&& internal::result::unsafe_err_move( Result<Tp, Er>& result) { return std::move(result.err_ref_()); } template <typename Tp> [[gnu::always_inline]] inline Tp&& internal::option::unsafe_value_move(Option<Tp>& option) { return std::move(option.value_ref_()); } } } using std::vector; using stx::Option; void f(vector<Option<int>>& v1, vector<Option<int>>& v2) { v1 = v2; }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree