C++20 std::format — 타입 안전 문자열 포매팅
std::format은 C++20에서 도입된 타입 안전 문자열 포매팅 라이브러리입니다. Python의 str.format()에서 영감을 받았으며, printf의 간결함과 iostream의 타입 안전성을 결합합니다.
1. 기본 사용법
섹션 제목: “1. 기본 사용법”#include <format>#include <string>#include <iostream>
int main() { // 기본 포매팅 std::string s = std::format("Hello, {}!", "world"); std::cout << s << '\n'; // Hello, world!
// 인덱스 기반 std::string s2 = std::format("{0} + {1} = {2}", 1, 2, 3); std::cout << s2 << '\n'; // 1 + 2 = 3}2. 포맷 지정자
섹션 제목: “2. 포맷 지정자”#include <format>#include <numbers>
// 정수std::format("{:d}", 42); // "42"std::format("{:08d}", 42); // "00000042"std::format("{:#x}", 255); // "0xff"std::format("{:#b}", 10); // "0b1010"
// 부동소수점std::format("{:.3f}", 3.14159); // "3.142"std::format("{:e}", 1234.5); // "1.234500e+03"std::format("{:g}", 0.0001); // "0.0001"
// 문자열 정렬std::format("{:>10}", "right"); // " right"std::format("{:<10}", "left"); // "left "std::format("{:^10}", "center"); // " center "std::format("{:*^10}", "hi"); // "****hi****"3. std::print / std::println (C++23)
섹션 제목: “3. std::print / std::println (C++23)”#include <print>
std::print("값: {}\n", 42); // 출력 후 개행 없음std::println("값: {}", 42); // 출력 후 자동 개행std::println(stderr, "오류: {}", "실패"); // stderr에 출력4. 커스텀 포매터 구현
섹션 제목: “4. 커스텀 포매터 구현”#include <format>
struct Color { uint8_t r, g, b; };
template<>struct std::formatter<Color> { // 포맷 지정자 파싱 (없으면 기본) constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
// 실제 포매팅 auto format(const Color& c, std::format_context& ctx) const { return std::format_to(ctx.out(), "#{:02X}{:02X}{:02X}", c.r, c.g, c.b); }};
int main() { Color red{255, 0, 0}; std::string s = std::format("색상: {}", red); // "색상: #FF0000"}5. 동적 포맷 문자열 — std::vformat
섹션 제목: “5. 동적 포맷 문자열 — std::vformat”#include <format>
void log(std::string_view fmt, std::format_args args) { std::string msg = std::vformat(fmt, args); // 로그 처리...}
// 호출log("{}: {}", std::make_format_args("level", 42));6. 성능 비교
섹션 제목: “6. 성능 비교”| 방식 | 타입 안전 | 성능 | 가독성 |
|---|---|---|---|
printf | ✗ | 빠름 | 중간 |
stringstream | ✓ | 느림 | 낮음 |
std::format | ✓ | 빠름 | 높음 |
{fmt} 라이브러리 | ✓ | 매우 빠름 | 높음 |
std::format은 {fmt} 라이브러리를 표준화한 것으로, 컴파일러 최적화를 통해 printf에 근접한 성능을 냅니다.
std::format은 C++ 문자열 처리의 패러다임을 바꿉니다. 포맷 지정자가 컴파일 타임에 검증되고, 커스텀 타입도 std::formatter 특수화로 자연스럽게 지원됩니다. C++23의 std::print와 함께 사용하면 완전히 printf를 대체할 수 있습니다.