Overview
cpp-dump is an all-round dump function library for C++ that supports even user-defined classes.
This library has the following features:
- Outputs to the standard error output (std::clog) string representations of a wide variety of types: multidimensional arrays, (multi)maps, (multi)sets, complex numbers, even error objects, and etc.
- Automatically indents so that the output fits into the maximum line width.
- Header-only library, no build or dependencies required.
- The macro version can dump variables along with the names.
- User-defined types can also be dumped by using macros.
- The string representation of variables is similar to JavaScript, Python and C syntax.
Introduction
cpp-dump has a macro version and a function version of dump functions.
CPP_DUMP(expressions…) macro
This macro dumps variable(s) along with expression(s). See Full Example Code

cpp_dump::dump(args…) function
This function simply dumps variable(s). See Full Example Code

Features
Both dump functions have the following features.
A wide variety of supported types
See All Supported Types
See Full Example Code
// See the full example code for the definitions of the variables. std::clog << "n// Basic Type" << std::endl; CPP_DUMP(false, 0, 0.0, '0'); CPP_DUMP(true, 3.14, my_int, 9265); CPP_DUMP("This is a string."); CPP_DUMP(ptr, void_ptr, nullptr); std::clog << "n// Container" << std::endl; CPP_DUMP(my_vector); std::clog << "n// Set/Map" << std::endl; CPP_DUMP(my_set); CPP_DUMP(my_map); std::clog << "n// Multiset/Multimap" << std::endl; CPP_DUMP(my_multiset); CPP_DUMP(my_multimap); std::clog << "n// Tuple" << std::endl; CPP_DUMP(my_tuple); CPP_DUMP(pair); std::clog << "n// FIFO/LIFO" << std::endl; CPP_DUMP(my_queue); CPP_DUMP(pq); CPP_DUMP(my_stack); std::clog << "n// Other" << std::endl; CPP_DUMP(my_bitset); CPP_DUMP(my_complex); CPP_DUMP(my_optional, std::nullopt); CPP_DUMP(my_variant); std::clog << "n// Combination" << std::endl; CPP_DUMP(vector_of_pairs);
Auto indent
Automatically indent so that the output does not exceed the maximum width. See Full Example Code
CPP_DUMP(my_vector); my_vector.push_back("This is a test string."); CPP_DUMP(my_vector);
User-defined types can also be supported by using macros
User-defined class
// At top level struct class_A { int i; std::string str() const { return std::to_string(i); } }; CPP_DUMP_DEFINE_EXPORT_OBJECT(class_A, i, str()); // In main() or some function class_A my_class_A{10}; CPP_DUMP(my_class_A);
Enum
// At top level enum class enum_A { a, b, c }; CPP_DUMP_DEFINE_EXPORT_ENUM(enum_A, enum_A::a, enum_A::b, enum_A::c); // In main() or some function enum_A my_enum_A = enum_A::c; CPP_DUMP(my_enum_A);
Requirement
- C++17 or higher.
- No build or dependencies are required since cpp-dump is a header-only library.
Installation
git clone https://github.com/philip82148/cpp-dump
or
git submodule add https://github.com/philip82148/cpp-dump
Usage
#include "path/to/cpp-dump/dump.hpp"
Macros
/** * Output string representations of expression(s) and result(s) to std::clog. */ #define CPP_DUMP(expressions...) /** * Make export_var() support type T. * Member functions to be displayed must be const. */ #define CPP_DUMP_DEFINE_EXPORT_OBJECT(T, members...) /** * Make export_var() support enum T. */ #define CPP_DUMP_DEFINE_EXPORT_ENUM(T, members...) /** * Set a value to a variable in cpp_dump namespace. */ #define CPP_DUMP_SET_OPTION(variable, value)
Variables
/** * Maximum line width of output strings of cpp_dump::export_var(). */ inline size_t cpp_dump::max_line_width = 160; /** * Maximum number of times cpp_dump::export_var() is applied recursively. */ inline size_t cpp_dump::max_depth = 5; /** * Maximum number of times cpp_dump::export_var() iterates over an iterator. * Note that in a single call, export_var() calls itself at most * (max_iteration_count^max_depth-1)/(max_iteration_count-1)-1 times. */ inline size_t cpp_dump::max_iteration_count = 16;
Functions
Show comments (8)