Compile-time Checked, Type-Safe Formatting in C++14.
MPark.Format is an experimental compile-time checked, type-safe formatting library for C++14.
Currently, it supports Python-like format strings with positional parameters.
#include <iostream>
#include <ostream>
#include <mpark/format.hpp>
struct Date {
int year, month, day;
};
std::ostream &operator<<(std::ostream &strm, const Date &that) {
return strm << mpark::format(
FS("{0}-{1}-{2}"), that.year, that.month, that.day);
}
int main() {
std::cout << mpark::format(FS("{0}{1}{0}\n"), "abra", "cad");
// prints: "abracadabra"
// std::cout << mpark::format(FS("{0}, {1}\n"), 'x');
// error: Index out of range.
std::cout << mpark::format(FS("The date is {0}.\n"), Date{2016, 4, 19});
// prints: "The date is 2016-4-19."
}