- To use
std::printneedg++-14and compile with-std=c++23.sudo apt install g++-14 sudo rm /usr/bin/g++ sudo ln -s /usr/bin/g++-14 /usr/bin/g++
g++ -std=c++23 print_me.cpp -o print_me
Last active
August 30, 2025 22:05
-
-
Save brccabral/2e16d8b867204f387e5c928d21157ab0 to your computer and use it in GitHub Desktop.
C++ Print formatter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <print> | |
| struct SDL_FRect{ | |
| float x,y,w,h; | |
| }; | |
| template <> | |
| struct std::formatter<SDL_FRect> | |
| { | |
| constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); } | |
| auto format(const SDL_FRect& r, std::format_context& ctx) const | |
| { | |
| return std::format_to(ctx.out(), "[x: {} y: {} w: {} h: {}]", r.x, r.y, r.w, r.h); | |
| } | |
| }; | |
| int main() { | |
| auto rect = SDL_FRect{}; | |
| std::print("{}\n", rect); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment