1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include <cstdio>
#include <iostream>
#include <stdexcept>
template<class... Param>
std::string formatStdString(char const* format, Param... params) {
constexpr size_t bufferSize = 1024;
char buffer[bufferSize];
int const snprintfResult = std::snprintf(buffer, bufferSize, format, params...);
if(snprintfResult < 0)
throw std::runtime_error("formatStdString : error code " + std::to_string(snprintfResult) + ".");
if(snprintfResult >= bufferSize)
throw std::runtime_error("formatStdString : the buffer is not big enough.");
return std::string{buffer};
}
int main() {
double const gx = 1234.56789;
double const gy = 0.0456789;
double const gz = 78.9;
std::cout << formatStdString("Gyr: %+8.3f %+8.3f %+8.3f\t", gx, gy, gz);
return 0;
} |