콘텐츠로 이동

C++20 std::format — 타입 안전 문자열 포매팅

std::format은 C++20에서 도입된 타입 안전 문자열 포매팅 라이브러리입니다. Python의 str.format()에서 영감을 받았으며, printf의 간결함과 iostream의 타입 안전성을 결합합니다.


#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
}

#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****"

#include <print>
std::print("값: {}\n", 42); // 출력 후 개행 없음
std::println("값: {}", 42); // 출력 후 자동 개행
std::println(stderr, "오류: {}", "실패"); // stderr에 출력

#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));

방식타입 안전성능가독성
printf빠름중간
stringstream느림낮음
std::format빠름높음
{fmt} 라이브러리매우 빠름높음

std::format{fmt} 라이브러리를 표준화한 것으로, 컴파일러 최적화를 통해 printf에 근접한 성능을 냅니다.


std::format은 C++ 문자열 처리의 패러다임을 바꿉니다. 포맷 지정자가 컴파일 타임에 검증되고, 커스텀 타입도 std::formatter 특수화로 자연스럽게 지원됩니다. C++23의 std::print와 함께 사용하면 완전히 printf를 대체할 수 있습니다.