A simple formatted string to replace s[n]printf
usage.
More...
#include <formatted_string.h>
Public Member Functions | |
formatted_string (char const *format, Args...args) | |
template<typename... Ts> | |
formatted_string (formatted_string< Ts...> &&rhs) noexcept | |
Move constructor. | |
template<typename... Ts> | |
formatted_string & | operator= (formatted_string< Ts...> &&rhs) noexcept |
Move assignment operator. | |
formatted_string (formatted_string const &)=delete | |
formatted_string & | operator= (formatted_string const &)=delete |
char const * | c_str () const noexcept |
std::string | to_string () const |
~formatted_string () | |
Static Public Attributes | |
static constexpr auto | max_len = 32 |
Maximum length of the formatted string before heap allocation is used. | |
A simple formatted string to replace s[n]printf
usage.
This is a simple handle class that provides storage for a formatted string that would normally be allocated using a C-style array and populated with s[n]printf
. Storage is allocated either on the stack or the heap, depending on the runtime size of the input as determined by calling std::snprintf(nullptr, 0, format, args);
. The underlying string can be read using the class members c_str
to retrieve a char const*
or to_string
to retrieve a std::string
.
Example usage:
|
inlineexplicit |
format | The C-style printf format string |
args | A parameter pack of values to put into the string |
This uses the special form of snprintf
that was introduced in C++11. It determines the size of the resulting string that would be needed to hold the formatted arguments. See cppref for details.
|
delete |
Copy semantics make little sense, so they are explicitly deleted.
|
inline |
The destructor only does work when the underlying string is long enough to require heap storage (len > max_len).
|
inlinenoexcept |
Retrieve the underlying string as a char const*
. This is much like the c_str
member of std::string
.
|
inline |
Retrieve the underlying formatted string and place it in an owning std::string
object.