Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
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#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
sway
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 12.4.0
ARM GCC 12.5.0
ARM GCC 13.1.0
ARM GCC 13.2.0
ARM GCC 13.2.0 (unknown-eabi)
ARM GCC 13.3.0
ARM GCC 13.3.0 (unknown-eabi)
ARM GCC 13.4.0
ARM GCC 13.4.0 (unknown-eabi)
ARM GCC 14.1.0
ARM GCC 14.1.0 (unknown-eabi)
ARM GCC 14.2.0
ARM GCC 14.2.0 (unknown-eabi)
ARM GCC 14.3.0
ARM GCC 14.3.0 (unknown-eabi)
ARM GCC 15.1.0
ARM GCC 15.1.0 (unknown-eabi)
ARM GCC 15.2.0
ARM GCC 15.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 (ex-WINE)
ARM msvc v19.10 (ex-WINE)
ARM msvc v19.14 (ex-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 12.4.0
ARM64 gcc 12.5.0
ARM64 gcc 13.1.0
ARM64 gcc 13.2.0
ARM64 gcc 13.3.0
ARM64 gcc 13.4.0
ARM64 gcc 14.1.0
ARM64 gcc 14.2.0
ARM64 gcc 14.3.0
ARM64 gcc 15.1.0
ARM64 gcc 15.2.0
ARM64 gcc 4.9.4
ARM64 gcc 5.4
ARM64 gcc 5.5.0
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 (ex-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 12.4.0
AVR gcc 12.5.0
AVR gcc 13.1.0
AVR gcc 13.2.0
AVR gcc 13.3.0
AVR gcc 13.4.0
AVR gcc 14.1.0
AVR gcc 14.2.0
AVR gcc 14.3.0
AVR gcc 15.1.0
AVR gcc 15.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 clang 19.1.0
BPF clang 20.1.0
BPF clang 21.1.0
EDG (experimental reflection)
EDG 6.5
EDG 6.5 (GNU mode gcc 13)
EDG 6.6
EDG 6.6 (GNU mode gcc 13)
EDG 6.7
EDG 6.7 (GNU mode gcc 14)
FRC 2019
FRC 2020
FRC 2023
HPPA gcc 14.2.0
HPPA gcc 14.3.0
HPPA gcc 15.1.0
HPPA gcc 15.2.0
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)
KVX ACB 5.0.0 (GCC 12.2.1)
KVX ACB 5.2.0 (GCC 13.2.1)
LoongArch64 clang (trunk)
LoongArch64 clang 17.0.1
LoongArch64 clang 18.1.0
LoongArch64 clang 19.1.0
LoongArch64 clang 20.1.0
LoongArch64 clang 21.1.0
M68K gcc 13.1.0
M68K gcc 13.2.0
M68K gcc 13.3.0
M68K gcc 13.4.0
M68K gcc 14.1.0
M68K gcc 14.2.0
M68K gcc 14.3.0
M68K gcc 15.1.0
M68K gcc 15.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
MinGW gcc 14.3.0
MinGW gcc 15.2.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 12.4.0
RISC-V (32-bits) gcc 12.5.0
RISC-V (32-bits) gcc 13.1.0
RISC-V (32-bits) gcc 13.2.0
RISC-V (32-bits) gcc 13.3.0
RISC-V (32-bits) gcc 13.4.0
RISC-V (32-bits) gcc 14.1.0
RISC-V (32-bits) gcc 14.2.0
RISC-V (32-bits) gcc 14.3.0
RISC-V (32-bits) gcc 15.1.0
RISC-V (32-bits) gcc 15.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 12.4.0
RISC-V (64-bits) gcc 12.5.0
RISC-V (64-bits) gcc 13.1.0
RISC-V (64-bits) gcc 13.2.0
RISC-V (64-bits) gcc 13.3.0
RISC-V (64-bits) gcc 13.4.0
RISC-V (64-bits) gcc 14.1.0
RISC-V (64-bits) gcc 14.2.0
RISC-V (64-bits) gcc 14.3.0
RISC-V (64-bits) gcc 15.1.0
RISC-V (64-bits) gcc 15.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 19.1.0
RISC-V rv32gc clang 20.1.0
RISC-V rv32gc clang 21.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 19.1.0
RISC-V rv64gc clang 20.1.0
RISC-V rv64gc clang 21.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 12.4.0
SPARC LEON gcc 12.5.0
SPARC LEON gcc 13.1.0
SPARC LEON gcc 13.2.0
SPARC LEON gcc 13.3.0
SPARC LEON gcc 13.4.0
SPARC LEON gcc 14.1.0
SPARC LEON gcc 14.2.0
SPARC LEON gcc 14.3.0
SPARC LEON gcc 15.1.0
SPARC LEON gcc 15.2.0
SPARC gcc 12.2.0
SPARC gcc 12.3.0
SPARC gcc 12.4.0
SPARC gcc 12.5.0
SPARC gcc 13.1.0
SPARC gcc 13.2.0
SPARC gcc 13.3.0
SPARC gcc 13.4.0
SPARC gcc 14.1.0
SPARC gcc 14.2.0
SPARC gcc 14.3.0
SPARC gcc 15.1.0
SPARC gcc 15.2.0
SPARC64 gcc 12.2.0
SPARC64 gcc 12.3.0
SPARC64 gcc 12.4.0
SPARC64 gcc 12.5.0
SPARC64 gcc 13.1.0
SPARC64 gcc 13.2.0
SPARC64 gcc 13.3.0
SPARC64 gcc 13.4.0
SPARC64 gcc 14.1.0
SPARC64 gcc 14.2.0
SPARC64 gcc 14.3.0
SPARC64 gcc 15.1.0
SPARC64 gcc 15.2.0
TI C6x gcc 12.2.0
TI C6x gcc 12.3.0
TI C6x gcc 12.4.0
TI C6x gcc 12.5.0
TI C6x gcc 13.1.0
TI C6x gcc 13.2.0
TI C6x gcc 13.3.0
TI C6x gcc 13.4.0
TI C6x gcc 14.1.0
TI C6x gcc 14.2.0
TI C6x gcc 14.3.0
TI C6x gcc 15.1.0
TI C6x gcc 15.2.0
TI CL430 21.6.1
Tricore gcc 11.3.0 (EEESlab)
VAX gcc NetBSDELF 10.4.0
VAX gcc NetBSDELF 10.5.0 (Nov 15 03:50:22 2023)
VAX gcc NetBSDELF 12.4.0 (Apr 16 05:27 2025)
WebAssembly clang (trunk)
Xtensa ESP32 gcc 11.2.0 (2022r1)
Xtensa ESP32 gcc 12.2.0 (20230208)
Xtensa ESP32 gcc 14.2.0 (20241119)
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 14.2.0 (20241119)
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 14.2.0 (20241119)
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.20 VS16.0
arm64 msvc v19.21 VS16.1
arm64 msvc v19.22 VS16.2
arm64 msvc v19.23 VS16.3
arm64 msvc v19.24 VS16.4
arm64 msvc v19.25 VS16.5
arm64 msvc v19.27 VS16.7
arm64 msvc v19.28 VS16.8
arm64 msvc v19.28 VS16.9
arm64 msvc v19.29 VS16.10
arm64 msvc v19.29 VS16.11
arm64 msvc v19.30 VS17.0
arm64 msvc v19.31 VS17.1
arm64 msvc v19.32 VS17.2
arm64 msvc v19.33 VS17.3
arm64 msvc v19.34 VS17.4
arm64 msvc v19.35 VS17.5
arm64 msvc v19.36 VS17.6
arm64 msvc v19.37 VS17.7
arm64 msvc v19.38 VS17.8
arm64 msvc v19.39 VS17.9
arm64 msvc v19.40 VS17.10
arm64 msvc v19.41 VS17.11
arm64 msvc v19.42 VS17.12
arm64 msvc v19.43 VS17.13
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 12.0.0
armv7-a clang 12.0.1
armv7-a clang 13.0.0
armv7-a clang 13.0.1
armv7-a clang 14.0.0
armv7-a clang 15.0.0
armv7-a clang 16.0.0
armv7-a clang 17.0.1
armv7-a clang 18.1.0
armv7-a clang 19.1.0
armv7-a clang 20.1.0
armv7-a clang 21.1.0
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 19.1.0
armv8-a clang 20.1.0
armv8-a clang 21.1.0
armv8-a clang 9.0.0
armv8-a clang 9.0.1
clad trunk (clang 21.1.0)
clad v1.10 (clang 20.1.0)
clad v1.8 (clang 18.1.0)
clad v1.9 (clang 19.1.0)
clad v2.00 (clang 20.1.0)
clad v2.1 (clang 21.1.0)
clang-cl 18.1.0
ellcc 0.1.33
ellcc 0.1.34
ellcc 2017-07-16
ez80-clang 15.0.0
ez80-clang 15.0.7
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 12.4.0
loongarch64 gcc 12.5.0
loongarch64 gcc 13.1.0
loongarch64 gcc 13.2.0
loongarch64 gcc 13.3.0
loongarch64 gcc 13.4.0
loongarch64 gcc 14.1.0
loongarch64 gcc 14.2.0
loongarch64 gcc 14.3.0
loongarch64 gcc 15.1.0
loongarch64 gcc 15.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 clang 19.1.0
mips clang 20.1.0
mips clang 21.1.0
mips gcc 11.2.0
mips gcc 12.1.0
mips gcc 12.2.0
mips gcc 12.3.0
mips gcc 12.4.0
mips gcc 12.5.0
mips gcc 13.1.0
mips gcc 13.2.0
mips gcc 13.3.0
mips gcc 13.4.0
mips gcc 14.1.0
mips gcc 14.2.0
mips gcc 14.3.0
mips gcc 15.1.0
mips gcc 15.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 12.4.0
mips64 (el) gcc 12.5.0
mips64 (el) gcc 13.1.0
mips64 (el) gcc 13.2.0
mips64 (el) gcc 13.3.0
mips64 (el) gcc 13.4.0
mips64 (el) gcc 14.1.0
mips64 (el) gcc 14.2.0
mips64 (el) gcc 14.3.0
mips64 (el) gcc 15.1.0
mips64 (el) gcc 15.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 clang 19.1.0
mips64 clang 20.1.0
mips64 clang 21.1.0
mips64 gcc 11.2.0
mips64 gcc 12.1.0
mips64 gcc 12.2.0
mips64 gcc 12.3.0
mips64 gcc 12.4.0
mips64 gcc 12.5.0
mips64 gcc 13.1.0
mips64 gcc 13.2.0
mips64 gcc 13.3.0
mips64 gcc 13.4.0
mips64 gcc 14.1.0
mips64 gcc 14.2.0
mips64 gcc 14.3.0
mips64 gcc 15.1.0
mips64 gcc 15.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
mips64el clang 19.1.0
mips64el clang 20.1.0
mips64el clang 21.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 clang 19.1.0
mipsel clang 20.1.0
mipsel clang 21.1.0
mipsel gcc 12.1.0
mipsel gcc 12.2.0
mipsel gcc 12.3.0
mipsel gcc 12.4.0
mipsel gcc 12.5.0
mipsel gcc 13.1.0
mipsel gcc 13.2.0
mipsel gcc 13.3.0
mipsel gcc 13.4.0
mipsel gcc 14.1.0
mipsel gcc 14.2.0
mipsel gcc 14.3.0
mipsel gcc 15.1.0
mipsel gcc 15.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 12.4.0
power gcc 12.5.0
power gcc 13.1.0
power gcc 13.2.0
power gcc 13.3.0
power gcc 13.4.0
power gcc 14.1.0
power gcc 14.2.0
power gcc 14.3.0
power gcc 15.1.0
power gcc 15.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 12.4.0
power64 gcc 12.5.0
power64 gcc 13.1.0
power64 gcc 13.2.0
power64 gcc 13.3.0
power64 gcc 13.4.0
power64 gcc 14.1.0
power64 gcc 14.2.0
power64 gcc 14.3.0
power64 gcc 15.1.0
power64 gcc 15.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 12.4.0
power64le gcc 12.5.0
power64le gcc 13.1.0
power64le gcc 13.2.0
power64le gcc 13.3.0
power64le gcc 13.4.0
power64le gcc 14.1.0
power64le gcc 14.2.0
power64le gcc 14.3.0
power64le gcc 15.1.0
power64le gcc 15.2.0
power64le gcc 6.3.0
power64le gcc trunk
powerpc64 clang (trunk)
qnx 8.0.0
s390x gcc 11.2.0
s390x gcc 12.1.0
s390x gcc 12.2.0
s390x gcc 12.3.0
s390x gcc 12.4.0
s390x gcc 12.5.0
s390x gcc 13.1.0
s390x gcc 13.2.0
s390x gcc 13.3.0
s390x gcc 13.4.0
s390x gcc 14.1.0
s390x gcc 14.2.0
s390x gcc 14.3.0
s390x gcc 15.1.0
s390x gcc 15.2.0
sh gcc 12.2.0
sh gcc 12.3.0
sh gcc 12.4.0
sh gcc 12.5.0
sh gcc 13.1.0
sh gcc 13.2.0
sh gcc 13.3.0
sh gcc 13.4.0
sh gcc 14.1.0
sh gcc 14.2.0
sh gcc 14.3.0
sh gcc 15.1.0
sh gcc 15.2.0
sh gcc 4.9.4
sh gcc 9.5.0
vast (trunk)
x64 msvc v19.0 (ex-WINE)
x64 msvc v19.10 (ex-WINE)
x64 msvc v19.14 (ex-WINE)
x64 msvc v19.20 VS16.0
x64 msvc v19.21 VS16.1
x64 msvc v19.22 VS16.2
x64 msvc v19.23 VS16.3
x64 msvc v19.24 VS16.4
x64 msvc v19.25 VS16.5
x64 msvc v19.27 VS16.7
x64 msvc v19.28 VS16.8
x64 msvc v19.28 VS16.9
x64 msvc v19.29 VS16.10
x64 msvc v19.29 VS16.11
x64 msvc v19.30 VS17.0
x64 msvc v19.31 VS17.1
x64 msvc v19.32 VS17.2
x64 msvc v19.33 VS17.3
x64 msvc v19.34 VS17.4
x64 msvc v19.35 VS17.5
x64 msvc v19.36 VS17.6
x64 msvc v19.37 VS17.7
x64 msvc v19.38 VS17.8
x64 msvc v19.39 VS17.9
x64 msvc v19.40 VS17.10
x64 msvc v19.41 VS17.11
x64 msvc v19.42 VS17.12
x64 msvc v19.43 VS17.13
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 (ex-WINE)
x86 msvc v19.10 (ex-WINE)
x86 msvc v19.14 (ex-WINE)
x86 msvc v19.20 VS16.0
x86 msvc v19.21 VS16.1
x86 msvc v19.22 VS16.2
x86 msvc v19.23 VS16.3
x86 msvc v19.24 VS16.4
x86 msvc v19.25 VS16.5
x86 msvc v19.27 VS16.7
x86 msvc v19.28 VS16.8
x86 msvc v19.28 VS16.9
x86 msvc v19.29 VS16.10
x86 msvc v19.29 VS16.11
x86 msvc v19.30 VS17.0
x86 msvc v19.31 VS17.1
x86 msvc v19.32 VS17.2
x86 msvc v19.33 VS17.3
x86 msvc v19.34 VS17.4
x86 msvc v19.35 VS17.5
x86 msvc v19.36 VS17.6
x86 msvc v19.37 VS17.7
x86 msvc v19.38 VS17.8
x86 msvc v19.39 VS17.9
x86 msvc v19.40 VS17.10
x86 msvc v19.41 VS17.11
x86 msvc v19.42 VS17.12
x86 msvc v19.43 VS17.13
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.11
x86 nvc++ 24.3
x86 nvc++ 24.5
x86 nvc++ 24.7
x86 nvc++ 24.9
x86 nvc++ 25.1
x86 nvc++ 25.3
x86 nvc++ 25.5
x86 nvc++ 25.7
x86 nvc++ 25.9
x86-64 Zapcc 190308
x86-64 clang (-fimplicit-constexpr)
x86-64 clang (Chris Bazley N3089)
x86-64 clang (EricWF contracts)
x86-64 clang (amd-staging)
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 P2998)
x86-64 clang (experimental P3068)
x86-64 clang (experimental P3309)
x86-64 clang (experimental P3367)
x86-64 clang (experimental P3372)
x86-64 clang (experimental P3385)
x86-64 clang (experimental P3776)
x86-64 clang (experimental metaprogramming - P2632)
x86-64 clang (old concepts branch)
x86-64 clang (p1974)
x86-64 clang (pattern matching - P2688)
x86-64 clang (reflection - C++26)
x86-64 clang (reflection - TS)
x86-64 clang (resugar)
x86-64 clang (string interpolation - P3412)
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 19.1.0
x86-64 clang 19.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 20.1.0
x86-64 clang 20.1.0 (assertions)
x86-64 clang 21.1.0
x86-64 clang 21.1.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 clang rocm-6.0.2
x86-64 clang rocm-6.1.2
x86-64 clang rocm-6.2.4
x86-64 clang rocm-6.3.3
x86-64 clang rocm-6.4.0
x86-64 gcc (P2034 lambdas)
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.3 (assertions)
x86-64 gcc 10.4
x86-64 gcc 10.4 (assertions)
x86-64 gcc 10.5
x86-64 gcc 10.5 (assertions)
x86-64 gcc 11.1
x86-64 gcc 11.1 (assertions)
x86-64 gcc 11.2
x86-64 gcc 11.2 (assertions)
x86-64 gcc 11.3
x86-64 gcc 11.3 (assertions)
x86-64 gcc 11.4
x86-64 gcc 11.4 (assertions)
x86-64 gcc 12.1
x86-64 gcc 12.1 (assertions)
x86-64 gcc 12.2
x86-64 gcc 12.2 (assertions)
x86-64 gcc 12.3
x86-64 gcc 12.3 (assertions)
x86-64 gcc 12.4
x86-64 gcc 12.4 (assertions)
x86-64 gcc 12.5
x86-64 gcc 12.5 (assertions)
x86-64 gcc 13.1
x86-64 gcc 13.1 (assertions)
x86-64 gcc 13.2
x86-64 gcc 13.2 (assertions)
x86-64 gcc 13.3
x86-64 gcc 13.3 (assertions)
x86-64 gcc 13.4
x86-64 gcc 13.4 (assertions)
x86-64 gcc 14.1
x86-64 gcc 14.1 (assertions)
x86-64 gcc 14.2
x86-64 gcc 14.2 (assertions)
x86-64 gcc 14.3
x86-64 gcc 14.3 (assertions)
x86-64 gcc 15.1
x86-64 gcc 15.1 (assertions)
x86-64 gcc 15.2
x86-64 gcc 15.2 (assertions)
x86-64 gcc 3.4.6
x86-64 gcc 4.0.4
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 6.5
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 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
x86-64 icx 2024.1.0
x86-64 icx 2024.2.0
x86-64 icx 2024.2.1
x86-64 icx 2025.0.0
x86-64 icx 2025.0.1
x86-64 icx 2025.0.3
x86-64 icx 2025.0.4
x86-64 icx 2025.1.0
x86-64 icx 2025.1.1
x86-64 icx 2025.2.0
x86-64 icx 2025.2.1
x86-64 icx 2025.2.1
z180-clang 15.0.0
z180-clang 15.0.7
z80-clang 15.0.0
z80-clang 15.0.7
zig c++ 0.10.0
zig c++ 0.11.0
zig c++ 0.12.0
zig c++ 0.12.1
zig c++ 0.13.0
zig c++ 0.14.0
zig c++ 0.14.1
zig c++ 0.15.1
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 <algorithm> #include <cassert> #include <cmath> #include <concepts> #include <iostream> #include <limits> #include <numeric> #include <ranges> #include <vector> // Some algorithms, like exclusive_scan, always need an initial value. // Many algorithms -- e.g., {inclusive,exclusive}_scan, reduce, and // transform_reduce -- need an identity value. That means we need a way // to distinguish the initial value from the identity value. // We have a few ways to do that. // // 1. By position and order alone // - Initial value follows immediately after the input range // (because it's a property of the input) // - Identity value follows immediately after // the binary operation to which it applies // (e.g., for binary transform_reduce, (op1, id1), op2) // // 2. By type (as well as position and order) // a. Attach identity value to binary operation as a single argument // b. Separate arguments for binary operation and identity value // // Design concerns: // // 1. What if there is no identity value? Do we intend to support that? // C++17 parallel std::reduce already does. // // 2. Should we support specifying the identity value // via constant_wrapper or some other "compile-time value"? // // Design options: // // 1. binary_operation struct, // holding both binary operator and identity value // 2. separate arguments for binary operator and identity value, // with op_identity struct holding identity value // 3. separate arguments for binary operator and identity value, // with the identity value only identified by its position // (immediately following the binary operator it describes) // // Define at most one of the following. // //#define BINARY_OPERATION_STRUCT 1 #define OP_IDENTITY_STRUCT 1 //#define POSITION_ONLY 1 namespace p3732 { // std::integral_constant doesn't accept non-integral types. // std::constant_wrapper (C++26 feature) doesn't back-port nicely. template<class T, T Value> struct constant { static constexpr T value = Value; constexpr operator T() { return Value; } constexpr static T operator() () { return Value; } }; // Tag type expressing that an identity value doesn't exist // or isn't known for the given binary operator. // Min and max on integers both have this problem // (as integers lack representations of positive and negative infinity). // // Having this lets us implement ranges min and max algorithms // using ranges::reduce. struct no_identity_t {}; inline constexpr no_identity_t no_identity{}; // std::ranges::min is defined with both input types // the same, so defining mixed operator< on // constant<T, Value> won't help. #if defined(BINARY_OPERATION_STRUCT) template<class BinaryOp> constexpr bool has_identity_value = false; template<class BinaryOp> struct binary_operation_base { template<class ... Args> constexpr auto operator() (Args&&... args) const requires std::invocable< std::add_const_t<BinaryOp>, decltype(std::forward<Args>(args))...> { return std::as_const(op)(std::forward<Args>(args)...); } template<class ... Args> constexpr auto operator() (Args&&... args) requires (! std::invocable< std::add_const_t<BinaryOp>, decltype(std::forward<Args>(args))...>) { return op(std::forward<Args>(args)...); } [[no_unique_address]] BinaryOp op; }; // Identity can be, say, constant_wrapper of the value, // not the actual value. This works because the accumulator // type is deduced from the operator result. template<class BinaryOp, class Identity> struct binary_operation : public binary_operation_base<BinaryOp> { [[no_unique_address]] Identity id; }; // Value-initialize Identity by default, // if the type supports that. // // Identity=no_identity_t means that the binary operator // does not have an identity, or the user does not know // an identity value. It still gets "stored" in the struct // so that the struct can remain an aggregate. Otherwise, // it would need a one-parameter constructor for that case. template<class BinaryOp, class Identity> requires requires { Identity{}; } struct binary_operation<BinaryOp, Identity> : public binary_operation_base<BinaryOp> { [[no_unique_address]] Identity id{}; }; // As with std::plus, Identity=void means "the algorithm (in this case, // exclusive_scan) needs to deduce the identity type and value." template<class BinaryOp> struct binary_operation<BinaryOp, void> : public binary_operation_base<BinaryOp> { [[no_unique_address]] BinaryOp op; }; template<class BinaryOp, class Identity> binary_operation(BinaryOp, Identity) -> binary_operation<BinaryOp, Identity>; template<class BinaryOp> binary_operation(BinaryOp) -> binary_operation<BinaryOp, no_identity_t>; struct test_binary_operation_nonconst { float operator() (float x, float y) { // deliberately nonconst return x + y; } }; struct test_binary_operation_const { float operator() (float x, float y) const { return x + y; } }; inline constexpr void test_binary_operation() { { test_binary_operation_nonconst op; [[maybe_unused]] binary_operation bop{op, 0.0f}; static_assert(std::is_same_v<decltype(bop(1.0f, 2.0f)), float>); } { test_binary_operation_const op; [[maybe_unused]] binary_operation bop{op, 0.0f}; static_assert(std::is_same_v<decltype(bop(1.0f, 2.0f)), float>); } } template<class BinaryOp, class Identity> constexpr bool has_identity_value< binary_operation<BinaryOp, Identity>> = true; template<class BinaryOp> constexpr bool has_identity_value< binary_operation<BinaryOp, no_identity_t>> = false; template<std::default_initializable InputRangeValueType, class BinaryOp> constexpr auto identity_value(const binary_operation<BinaryOp, void>&) { return InputRangeValueType{}; } template<class InputRangeValueType, class BinaryOp, class Identity> requires( not std::is_same_v<Identity, no_identity_t> ) constexpr auto identity_value(const binary_operation<BinaryOp, Identity>& bop) { return bop.id; } #endif // BINARY_OPERATION_STRUCT #if defined(OP_IDENTITY_STRUCT) // Identity can be, say, constant_wrapper of the value, not the actual value. // This works because the accumulator type is deduced from the operator result. // // Default template argument permits using `op_identity{}` // as an argument of `exclusive_scan`. It's up to `exclusive_scan` to figure out // what Identity=void means, but users can guess that it's like // std::plus<void> (meaning the identity value type is deduced). template<class Identity=void> struct op_identity; template<class Identity> struct op_identity { [[no_unique_address]] Identity id; }; template<std::default_initializable Identity> struct op_identity<Identity> { [[no_unique_address]] Identity id{}; }; template<> struct op_identity<void> {}; template<> struct op_identity<no_identity_t> {}; // Abbreviation so users don't have to type "identity" twice. // Otherwise, they would have to write op_identity{no_identity} // or op_identity<no_identity_t>{}. inline constexpr op_identity<no_identity_t> no_op_identity{}; template<class Identity> op_identity(Identity) -> op_identity<Identity>; template<class InputRangeValueType, class Identity> requires(! std::is_same_v<Identity, no_identity_t>) constexpr auto identity_value(op_identity<Identity> op_id) { return op_id.id; } template<std::default_initializable InputRangeValueType> constexpr auto identity_value(op_identity<void>) { return InputRangeValueType{}; } #endif // OP_IDENTITY_STRUCT #if defined(BINARY_OPERATION_STRUCT) template< std::ranges::forward_range InRange, std::ranges::forward_range OutRange, class InitialValue, class BinaryOp = std::plus<> > requires( std::is_invocable_r_v< std::ranges::range_value_t<OutRange>, BinaryOp, std::ranges::range_value_t<InRange>, std::ranges::range_value_t<InRange> > && std::is_invocable_r_v< std::ranges::range_value_t<OutRange>, BinaryOp, InitialValue, std::ranges::range_value_t<InRange> > ) std::ranges::in_out_result< std::ranges::iterator_t<InRange>, std::ranges::iterator_t<OutRange> > exclusive_scan(InRange&& in, OutRange&& out, InitialValue initial_value, BinaryOp bop) { using in_value_type = std::ranges::range_value_t<InRange>; // FIXME this probably won't work for expression templates. using result_type = std::remove_cvref_t< decltype(bop( initial_value, std::declval<in_value_type>()))>; auto in_beg = std::ranges::begin(in); auto in_end = std::ranges::end(in); auto out_beg = std::ranges::begin(out); auto out_end = std::ranges::end(out); if constexpr (has_identity_value<BinaryOp>) { // Only parallel algorithms need the identity value. // For testing, though, we exercise getting and // using it. if (in_beg != in_end) { [[maybe_unused]] auto id = identity_value<in_value_type>(bop); assert(bop.op(id, *in_beg) == *in_beg); assert(bop.op(*in_beg, id) == *in_beg); } } auto in_iter = in_beg; auto out_iter = out_beg; result_type total = initial_value; for (; in_iter != in_end && out_iter != out_end; ++in_iter, ++out_iter) { *out_iter = total; total = bop(total, *in_iter); } return {in_iter, out_iter}; } #endif // BINARY_OPERATION_STRUCT #if defined(OP_IDENTITY_STRUCT) template< std::ranges::forward_range InRange, std::ranges::forward_range OutRange, class InitialValue, class BinaryOp = std::plus<>, class IdentityValue = std::ranges::range_value_t<InRange> > requires( std::is_invocable_r_v< std::ranges::range_value_t<OutRange>, BinaryOp, std::ranges::range_value_t<InRange>, std::ranges::range_value_t<InRange> > && std::is_invocable_r_v< std::ranges::range_value_t<OutRange>, BinaryOp, InitialValue, std::ranges::range_value_t<InRange> > ) std::ranges::in_out_result< std::ranges::iterator_t<InRange>, std::ranges::iterator_t<OutRange> > exclusive_scan(InRange&& in, OutRange&& out, InitialValue initial_value, BinaryOp op, op_identity<IdentityValue> op_id) requires(std::is_same_v<IdentityValue, no_identity_t> || std::is_invocable_r_v< std::ranges::range_value_t<OutRange>, BinaryOp, decltype( identity_value<std::ranges::range_value_t<InRange>>(op_id) ), std::ranges::range_value_t<InRange> > ) { using in_value_type = std::ranges::range_value_t<InRange>; // FIXME this probably won't work for expression templates. using result_type = std::remove_cvref_t< decltype(op(initial_value, std::declval<in_value_type>()))>; auto in_beg = std::ranges::begin(in); auto in_end = std::ranges::end(in); auto out_beg = std::ranges::begin(out); auto out_end = std::ranges::end(out); if constexpr (! std::is_same_v<IdentityValue, no_identity_t>) { // Only parallel algorithms need the identity value. // For testing, though, we exercise getting and using it. if (in_beg != in_end) { [[maybe_unused]] auto id = identity_value<in_value_type>(op_id); assert(op(id, *in_beg) == *in_beg); assert(op(*in_beg, id) == *in_beg); } } auto in_iter = in_beg; auto out_iter = out_beg; result_type total = initial_value; for (; in_iter != in_end && out_iter != out_end; ++in_iter, ++out_iter) { *out_iter = total; total = op(total, *in_iter); } return {in_iter, out_iter}; } #endif // OP_IDENTITY_STRUCT #if defined(POSITION_ONLY) template< std::ranges::forward_range InRange, std::ranges::forward_range OutRange, class InitialValue, class BinaryOp = std::plus<>, class IdentityValue = std::ranges::range_value_t<InRange> > requires( std::is_invocable_r_v< std::ranges::range_value_t<OutRange>, BinaryOp, std::ranges::range_value_t<InRange>, std::ranges::range_value_t<InRange> > && std::is_invocable_r_v< std::ranges::range_value_t<OutRange>, BinaryOp, InitialValue, std::ranges::range_value_t<InRange> > ) std::ranges::in_out_result< std::ranges::iterator_t<InRange>, std::ranges::iterator_t<OutRange> > exclusive_scan(InRange&& in, OutRange&& out, InitialValue initial_value, BinaryOp op, IdentityValue id) requires(std::is_same_v<std::remove_cvref_t<IdentityValue>, no_identity_t> || std::is_invocable_r_v< std::ranges::range_value_t<OutRange>, BinaryOp, IdentityValue, std::ranges::range_value_t<InRange> > ) { using in_value_type = std::ranges::range_value_t<InRange>; // FIXME this probably won't work for expression templates. using result_type = std::remove_cvref_t< decltype(op(initial_value, std::declval<in_value_type>()))>; auto in_beg = std::ranges::begin(in); auto in_end = std::ranges::end(in); auto out_beg = std::ranges::begin(out); auto out_end = std::ranges::end(out); if constexpr (! std::is_same_v<IdentityValue, no_identity_t>) { // Only parallel algorithms need the identity value. // For testing, though, we exercise getting and using it. if (in_beg != in_end) { assert(op(id, *in_beg) == *in_beg); assert(op(*in_beg, id) == *in_beg); } } auto in_iter = in_beg; auto out_iter = out_beg; result_type total = initial_value; for (; in_iter != in_end && out_iter != out_end; ++in_iter, ++out_iter) { *out_iter = total; total = op(total, *in_iter); } return {in_iter, out_iter}; } #endif // POSITION_ONLY #if ! defined(BINARY_OPERATION_STRUCT) // User only specifies the binary operation, // not an identity value. // Algorithm determines default identity as // value-initialized value type of the input range. // // If you want no_identity_t, you have to spell it // explicitly by calling one of the overheads // that takes an identity value. template< std::ranges::forward_range InRange, std::ranges::forward_range OutRange, class InitialValue, class BinaryOp > requires( std::is_invocable_r_v< std::ranges::range_value_t<OutRange>, BinaryOp, std::ranges::range_value_t<InRange>, std::ranges::range_value_t<InRange> > && std::is_invocable_r_v< std::ranges::range_value_t<OutRange>, BinaryOp, InitialValue, std::ranges::range_value_t<InRange> > ) std::ranges::in_out_result< std::ranges::iterator_t<InRange>, std::ranges::iterator_t<OutRange> > exclusive_scan(InRange&& in, OutRange&& out, InitialValue initial_value, BinaryOp op) requires(std::is_invocable_r_v< std::ranges::range_value_t<OutRange>, BinaryOp, #if defined(POSITION_ONLY) std::ranges::range_value_t<InRange>, #else decltype( identity_value<std::ranges::range_value_t<InRange>>( # if defined(OP_IDENTITY_STRUCT) op_identity{} # endif ) ), #endif std::ranges::range_value_t<InRange> > ) { #if defined(OP_IDENTITY_STRUCT) return ::p3732::exclusive_scan(std::forward<InRange>(in), std::forward<OutRange>(out), initial_value, op, op_identity{}); #elif defined(POSITION_ONLY) return ::p3732::exclusive_scan(std::forward<InRange>(in), std::forward<OutRange>(out), initial_value, op, std::ranges::range_value_t<InRange>{}); #endif // BINARY_OPERATION_STRUCT } #endif // ! BINARY_OPERATION_STRUCT } // namespace p3732 namespace test { // Binary operator is the usual arithmetic plus; // identity value is the usual zero. void exclusive_scan_plus_and_zero() { constexpr int initial_value = 2; constexpr int flag = -100000; std::vector in{-3, 5, -7, 11, -13, 17}; // Input sequence: 2, -3, 5, -7, 11, -13, 17 // Exclusive scan: 2, -1, 4, -3, 8, -5 std::vector expected_out{2, -1, 4, -3, 8, -5}; std::vector out(in.size(), flag); std::exclusive_scan(in.begin(), in.end(), out.begin(), initial_value, std::plus{}); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); p3732::exclusive_scan(in, out, initial_value, std::plus{}); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); // This assumes that we let users omit the identity. p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; } ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); #if defined(BINARY_OPERATION_STRUCT) // User specifies identity value explicitly. // // We generally would want users to rely on CTAD // for binary_operation, since there's no way to // get the type of an inline lambda and pass the // lambda to a function at the same time // (as two inline lambdas have different types). p3732::exclusive_scan(in, out, initial_value, p3732::binary_operation{ [](auto x, auto y) { return x + y; }, 0 } ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); // User specifies identity value as a compile-time constant. p3732::exclusive_scan(in, out, initial_value, p3732::binary_operation{ [](auto x, auto y) { return x + y; }, p3732::constant<int, 0>{} } ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); // User does not specify identity value at all; // use the default identity value. p3732::exclusive_scan(in, out, initial_value, p3732::binary_operation{ [](auto x, auto y) { return x + y; } } ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); // User wants the algorithm not to assume // that an identity value exists. p3732::exclusive_scan(in, out, initial_value, p3732::binary_operation{ [](auto x, auto y) { return x + y; }, p3732::no_identity } ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); #endif // BINARY_OPERATION_STRUCT #if defined(OP_IDENTITY_STRUCT) // User specifies both template argument // (type of identity value) and identity value explicitly. p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; }, p3732::op_identity<int>{0} ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); // User relies on CTAD // and specifies identity value explicitly. // // If op_identity has a default template argument, // then it needs a deduction guide to make this work. p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; }, p3732::op_identity{0} ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); // User relies on CTAD _and_ a default identity value. // // This only works if op_identity has a default template argument. p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; }, p3732::op_identity{} ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); // User omits "op_identity" type and relies on // curly-brace initialization with an identity value. // // Should we even permit this? p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; }, {0} ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); // User omits "op_identity" type and relies on // the default identity value. // // This works even if op_identity lacks both // a default template argument and a deduction guide. // // Should we even permit this? p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; }, {} ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); // User specifies identity value as a compile-time constant // and relies on op_identity CTAD. p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; }, p3732::op_identity{p3732::constant<int, 0>{}} ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); // User specifies type of identity explicitly // (as a compile-time constant) but relies on default value. p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; }, p3732::op_identity<p3732::constant<int, 0>>{} ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); // User wants the algorithm not to assume // that an identity value exists. p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; }, p3732::no_op_identity ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); #endif // OP_IDENTITY_STRUCT #if defined(POSITION_ONLY) // User specifies identity value explicitly. p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; }, 0 ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); // User specifies identity value as a compile-time constant. p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; }, p3732::constant<int, 0>{} ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); // User wants the algorithm not to assume // that an identity value exists. p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; }, p3732::no_identity ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); #endif // POSITION_ONLY } // The "min tropical semiring" a.k.a. "min-plus semiring" // defines binary addition as minimum, and binary multiplication as addition. // Additive identity is +Inf and multiplicative identity is zero. class min_plus_semiring { public: constexpr min_plus_semiring() = default; explicit constexpr min_plus_semiring(double value) : value_(value) {} constexpr double value() const { return value_; } friend constexpr min_plus_semiring operator+(min_plus_semiring x, min_plus_semiring y) { return min_plus_semiring{std::fmin(x.value_, y.value_)}; } friend constexpr min_plus_semiring operator*(min_plus_semiring x, min_plus_semiring y) { // that's right, it's plus and not times return min_plus_semiring{x.value_ + y.value_}; } friend constexpr bool operator==(min_plus_semiring, min_plus_semiring) = default; private: static constexpr double additive_identity = std::numeric_limits<double>::infinity(); double value_ = additive_identity; }; // This is like exclusive_scan_plus_and_zero // in that it relies on plus and the default identity value, // except that it uses a custom number type. void exclusive_scan_min_plus_semiring() { constexpr auto initial_value = min_plus_semiring{2.0}; constexpr auto flag = min_plus_semiring{-100000.0}; std::vector in{ min_plus_semiring{ -3.0}, min_plus_semiring{ 5.0}, min_plus_semiring{ -7.0}, min_plus_semiring{ 11.0}, min_plus_semiring{-13.0}, min_plus_semiring{ 17.0} }; // Input sequence: 2, -3, 5, -7, 11, -13, 17 // Exclusive plus scan: 2, -3, -3, -7, -7, -13 std::vector expected_out{ min_plus_semiring{ 2.0}, min_plus_semiring{ -3.0}, min_plus_semiring{ -3.0}, min_plus_semiring{ -7.0}, min_plus_semiring{ -7.0}, min_plus_semiring{-13.0} }; std::vector out(in.size(), flag); std::exclusive_scan(in.begin(), in.end(), out.begin(), initial_value, std::plus{}); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); p3732::exclusive_scan(in, out, initial_value, std::plus{}); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); // This assumes that we let users omit the identity. p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; } ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); #if defined(BINARY_OPERATION_STRUCT) // Specify identity value p3732::exclusive_scan(in, out, initial_value, p3732::binary_operation{ [](auto x, auto y) { return x + y; }, min_plus_semiring() } ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); // Use default identity value p3732::exclusive_scan(in, out, initial_value, p3732::binary_operation{ [](auto x, auto y) { return x + y; } } ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); #endif // BINARY_OPERATION_STRUCT #if defined(OP_IDENTITY_STRUCT) // Identity argument is explicitly wrapped. // All of these cases need to work in this design. p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; }, p3732::op_identity<min_plus_semiring>{min_plus_semiring()} ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); // If op_identity has a default template argument, // then it needs a deduction guide to make this work. p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; }, p3732::op_identity{min_plus_semiring()} ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); // op_identity needs a default template argument to make this work. p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; }, p3732::op_identity{} ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); // Identity argument is implicit but requires curly braces. // Should this be permitted? p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; }, {min_plus_semiring()} ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); // This works even if op_identity lacks both // a default template argument and a deduction guide. p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; }, {} ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); #endif // OP_IDENTITY_STRUCT } // "Manual" means instead of writing a type that implements // the min-plus semiring, we use plain double and supply the // binary operator and identity value by hand. // // This use case REQUIRES an identity value, // at least in the parallel case. void exclusive_scan_min_plus_semiring_manual() { constexpr auto initial_value = double{2.0}; constexpr auto flag = double{-100000.0}; std::vector in{ double{ -3.0}, double{ 5.0}, double{ -7.0}, double{ 11.0}, double{-13.0}, double{ 17.0} }; // Input sequence: 2, -3, 5, -7, 11, -13, 17 // Exclusive plus scan: 2, -3, -3, -7, -7, -13 std::vector expected_out{ double{ 2.0}, double{ -3.0}, double{ -3.0}, double{ -7.0}, double{ -7.0}, double{-13.0} }; std::vector out(in.size(), flag); // // User specifies identity value explicitly. // #if defined(BINARY_OPERATION_STRUCT) p3732::exclusive_scan(in, out, initial_value, p3732::binary_operation{ std::ranges::min, double(std::numeric_limits<double>::infinity()) } ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); #endif // BINARY_OPERATION_STRUCT #if defined(OP_IDENTITY_STRUCT) // If op_identity has a default template argument, // then it needs a deduction guide to make this work. p3732::exclusive_scan(in, out, initial_value, std::ranges::min, p3732::op_identity{ std::numeric_limits<double>::infinity() } ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); p3732::exclusive_scan(in, out, initial_value, std::ranges::min, p3732::op_identity<double>{ std::numeric_limits<double>::infinity() } ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); #endif #if defined(POSITION_ONLY) p3732::exclusive_scan(in, out, initial_value, std::ranges::min, std::numeric_limits<double>::infinity() ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); #endif // POSITION_ONLY // // User specifies identity as a compile-time value. // // This doesn't work with std::ranges::min // if we deduce the template argument T, // even if the compile-time value type // has overloaded mixed operator<, // because std::ranges::min assumes that // the two values to compare have the same type. // Users also aren't allowed to say // std::ranges::min<double>, // so they have to use a lambda instead. // #if defined(BINARY_OPERATION_STRUCT) p3732::exclusive_scan(in, out, initial_value, p3732::binary_operation{ //std::ranges::min, [] (auto x, auto y) { return std::fmin(x, y); }, p3732::constant< double, std::numeric_limits<double>::infinity() >{} } ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); #endif // BINARY_OPERATION_STRUCT #if defined(OP_IDENTITY_STRUCT) p3732::exclusive_scan(in, out, initial_value, //std::ranges::min, [] (auto x, auto y) { return std::fmin(x, y); }, p3732::op_identity{ p3732::constant< double, std::numeric_limits<double>::infinity() >{} } ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); #endif // OP_IDENTITY_STRUCT #if defined(POSITION_ONLY) p3732::exclusive_scan(in, out, initial_value, //std::ranges::min, [] (auto x, auto y) { return std::fmin(x, y); }, p3732::constant< double, std::numeric_limits<double>::infinity() >{} ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); #endif // POSITION_ONLY // // Use cases that we might not want to permit // #if defined(OP_IDENTITY_STRUCT) // Identity argument is implicit but requires curly braces. // // Should this be permitted? p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return std::fmin(x, y); }, { std::numeric_limits<double>::infinity() } ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); #endif // OP_IDENTITY_STRUCT } // Custom number type with no identity value // (that cannot be value-initialized). // // The integer variant of the min-plus semiring // still defines binary addition as minimum // and binary multiplication as addition. // However, the additive identity (+Inf) // can't be represented as an integer. // A user-defined type like this could add extra state // (e.g., a bool is_finite_ = false), so that a // value-initialized object would represent +Inf. // However, that would make it hard for the type // to interact with existing libraries that expect // sizeof(integer_min_plus_semiring) == sizeof(int). // It would also complicate the implementation. // For example, the (int value_, bool is_finite_=false) // implementation would not have a unique bit representation // of +Inf. class integer_min_plus_semiring { public: explicit constexpr integer_min_plus_semiring(int value) : value_(value) {} constexpr int value() const { return value_; } friend constexpr integer_min_plus_semiring operator+(integer_min_plus_semiring x, integer_min_plus_semiring y) { return integer_min_plus_semiring{x.value_ <= y.value_ ? x.value_ : y.value_}; } friend constexpr integer_min_plus_semiring operator*(integer_min_plus_semiring x, integer_min_plus_semiring y) { // that's right, it's plus and not times return integer_min_plus_semiring{x.value_ + y.value_}; } friend constexpr bool operator==(integer_min_plus_semiring, integer_min_plus_semiring) = default; private: int value_; }; // integer_min_plus_semiring doesn't have an identity value. void exclusive_scan_integer_min_plus_semiring() { constexpr auto initial_value = integer_min_plus_semiring{2}; constexpr auto flag = integer_min_plus_semiring{-100000}; std::vector in{ integer_min_plus_semiring{ -3}, integer_min_plus_semiring{ 5}, integer_min_plus_semiring{ -7}, integer_min_plus_semiring{ 11}, integer_min_plus_semiring{-13}, integer_min_plus_semiring{ 17} }; // Input sequence: 2, -3, 5, -7, 11, -13, 17 // Exclusive plus scan: 2, -3, -3, -7, -7, -13 std::vector expected_out{ integer_min_plus_semiring{ 2}, integer_min_plus_semiring{ -3}, integer_min_plus_semiring{ -3}, integer_min_plus_semiring{ -7}, integer_min_plus_semiring{ -7}, integer_min_plus_semiring{-13} }; std::vector out(in.size(), flag); std::exclusive_scan(in.begin(), in.end(), out.begin(), initial_value, std::plus{}); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); #if defined(BINARY_OPERATION_STRUCT) p3732::exclusive_scan(in, out, initial_value, p3732::binary_operation{ [](auto x, auto y) { return x + y; }, p3732::no_identity } ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); #endif // BINARY_OPERATION_STRUCT #if defined(OP_IDENTITY_STRUCT) p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; }, p3732::no_op_identity ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); #endif // OP_IDENTITY_STRUCT #if defined(POSITION_ONLY) p3732::exclusive_scan(in, out, initial_value, [](auto x, auto y) { return x + y; }, p3732::no_identity ); assert(expected_out == out); std::fill(out.begin(), out.end(), flag); #endif // POSITION_ONLY } } // namespace test int main() { test::exclusive_scan_plus_and_zero(); test::exclusive_scan_min_plus_semiring(); test::exclusive_scan_min_plus_semiring_manual(); test::exclusive_scan_integer_min_plus_semiring(); return 0; }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree