Skip to content

Instantly share code, notes, and snippets.

@brccabral
Last active August 30, 2025 22:05
Show Gist options
  • Save brccabral/2e16d8b867204f387e5c928d21157ab0 to your computer and use it in GitHub Desktop.
Save brccabral/2e16d8b867204f387e5c928d21157ab0 to your computer and use it in GitHub Desktop.
C++ Print formatter

std::print - C++ Print formatter

  • To use std::print need g++-14 and 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
#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