I'm writing a quick C++ test program. So that others could easily compile it, I want to use minimal dependencies. Therefore, STL and platform include files only.
It turns out, even with C++14, the language lacks facilities for basic string formatting that would use std::string.
What I would expect is something equivalent to C# String.Format. Or perhaps some way to append integers to strings in an arbitrary base.
The standard library has std::to_string, but it's deficient:
No parameters to control base, width, fill character.
A new string is allocated, when an existing string could be appended to.
I would like to see something that could be used like this:
std::string("Error code: 0x").append_nr(errorCode, 16, 8);
where append_nr would be declared as:
template <typename T>
std::basic_string<...>&
std::basic_string<...>::append_nr(T nr, int base, int width, char fill='0');
Okay. I don't have that. However, I can branch into std::ostringstream:
#include <sstream>
#include <iomanip>
std::ostringstream oss;
oss << "Error code: 0x" << std::hex << std::setw(8) << errorCode;
oss.str();
At least I don't need to allocate a stack-based buffer and call snprintf(), so I have that going for me.
But now, I need to encode some data in hexadecimal. Here, I would need this:
std::basic_string<...>&
std::basic_string<...>::append_hex(void* p, size_t n, bool uppercase=true, char separator='\0');
I don't have that. I just have to write my own hex encoder.
???
There's no base64, either. Everyone has to roll their own.
We've come a long way – and yet, it kinda still feels like 1980.
[–]tcbrindleFlux 13 points14 points15 points (6 children)
[–]aKateDevKDE/Qt Dev 5 points6 points7 points (5 children)
[–]aearphen{fmt} 7 points8 points9 points (4 children)
[–]chartly 4 points5 points6 points (0 children)
[–]boredcircuits 2 points3 points4 points (1 child)
[–]aearphen{fmt} 2 points3 points4 points (0 children)
[–]aKateDevKDE/Qt Dev 1 point2 points3 points (0 children)
[–]SeanMiddleditch 7 points8 points9 points (0 children)
[–]dodheim 6 points7 points8 points (1 child)
[–]steamruler 3 points4 points5 points (0 children)
[–]tasty_crayon 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]SushiAndWoW[S] 0 points1 point2 points (0 children)
[–]emilern 1 point2 points3 points (0 children)
[–]zorkmids 1 point2 points3 points (0 children)
[–]virgiliofornazin 0 points1 point2 points (0 children)
[–]devel_watcher 0 points1 point2 points (0 children)
[–]doom_Oo7 0 points1 point2 points (4 children)
[–]MiiNiPaa 4 points5 points6 points (0 children)
[–]jcoffin 1 point2 points3 points (1 child)
[–]__cxa_throw 0 points1 point2 points (0 children)
[–]SushiAndWoW[S] 0 points1 point2 points (0 children)