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#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
cpp2_cppfront 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
cppfront trunk
Options
Source code
print_container: (name: std::string_view, cont) = { std::cout << "(name)$: \t"; for cont do (n) { std::cout << "(n)$ "; } std::cout << '\n'; } // C++ function template that calls Cpp2 `print_container` function template <typename Adaptor> requires (std::ranges::input_range<typename Adaptor::container_type>) void print(std::string_view name, const Adaptor& adaptor) { struct Printer : Adaptor // to access protected Adaptor::Container c; { void print(std::string_view name) const { ::print_container(name, this->c); } }; static_cast<Printer const&>(adaptor).print(name); } main: () = { data: std::vector = (1, 8, 5, 6, 3, 4, 0, 9, 7, 2); print_container("data", data); q1: std::priority_queue<int> = (); // Max priority queue for data do (n: int) { q1.push(n); } print("q1", q1); // Min priority queue // std::greater<int> makes the max priority queue act as a min priority queue minq1: std::priority_queue<int, std::vector<int>, std::greater<int>> = (data.begin(), data.end()); print("minq1", minq1); // Second way to define a min priority queue minq2: std::priority_queue = (data.begin(), data.end(), std::greater<int>()); print("minq2", minq2); // Using a terse, unnamed function expression (lambda) to compare elements. minq3: std::priority_queue = (data.begin(), data.end(), :(l, r) l > r ); print("minq3", minq3); // Using a named function expression (lambda) to compare elements. cmp:= :(left: int, right: int) -> bool = { return (left ^ 1) < (right ^ 1); }; q5: std::priority_queue<int, std::vector<int>, decltype(cmp)> = (cmp); for data do (n: int) { q5.push(n); } print("q5", q5); }
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
CE on Bluesky
About the author
Statistics
Changelog
Version tree