changa  3.5
 All Classes Files Functions Variables Typedefs Enumerations Friends Macros Groups Pages
Classes | Public Member Functions | Static Public Attributes | List of all members
formatted_string< Args > Class Template Reference

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_stringoperator= (formatted_string< Ts...> &&rhs) noexcept
 Move assignment operator.
 
 formatted_string (formatted_string const &)=delete
 
formatted_stringoperator= (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.
 

Detailed Description

template<typename... Args>
class formatted_string< Args >

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.

Warning
Modifying the underlying data store is undefined behavior!

Example usage:

// Old way
char str[100];
sprintf(str, "%s %d", "Hello", 3);
// New way
auto str = make_formatted_string("%s %d", "Hello", 3);

Constructor & Destructor Documentation

template<typename... Args>
formatted_string< Args >::formatted_string ( char const *  format,
Args...  args 
)
inlineexplicit
Parameters
formatThe C-style printf format string
argsA 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.

template<typename... Args>
formatted_string< Args >::formatted_string ( formatted_string< Args > const &  )
delete

Copy semantics make little sense, so they are explicitly deleted.

template<typename... Args>
formatted_string< Args >::~formatted_string ( )
inline

The destructor only does work when the underlying string is long enough to require heap storage (len > max_len).

Member Function Documentation

template<typename... Args>
char const* formatted_string< Args >::c_str ( ) const
inlinenoexcept

Retrieve the underlying string as a char const*. This is much like the c_str member of std::string.

template<typename... Args>
std::string formatted_string< Args >::to_string ( ) const
inline

Retrieve the underlying formatted string and place it in an owning std::string object.


The documentation for this class was generated from the following file: