cxx-prettyprint =============== A pretty printing library for C++ containers. Synopsis: Simply by including this header-only library in your source file, you can say "std::cout << x" for any container object x. Sensible defaults are provided, but the behaviour (i.e. the delimiters) are compile-time customizable to a great extent via partial specializiation. Usage: Just add "#include "prettyprint.hpp" to your source file and make sure that prettyprint.hpp is findable. Language requirements: C++0x for prettyprint.hpp, C++98/03 for prettyprint98.hpp Example: Some usage examples are provided by ppdemo.cpp. Using GCC, compile with g++ -W -Wall -pedantic -O2 -s ppdemo.cpp -o ppdemo -std=c++0x g++ -W -Wall -pedantic -O2 -s ppdemo98.cpp -o ppdemo98 For the C++98/03-version, define "NO_TR1" to prevent any inclusion of TR1 headers and to disable std::tr1::tuple support. For details, please see the website (http://louisdx.github.com/cxx-prettyprint/). License: Boost Software License, Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
A header-only library for C++(0x) that allows automagic pretty-printing of any container.
cxx-prettyprint =============== A pretty printing library for C++ containers. Synopsis: Simply by including this header-only library in your source file, you can say "std::cout << x" for any container object x. Sensible defaulCategory: C/C++ / Miscellaneous |
Watchers: 32 |
Star: 531 |
Fork: 72 |
Last update: Aug 2, 2022 |
... include
I'm looking to use this library in a GPLv3+ project, but I can't find a license on this code. Would it be possible that you make explicit the license it is under (or point me to where this is done)?
It would be better to select an existing license, like CC0 for the closest one can legally get to releasing into public domain in many jurisdictions, zlib license for a permissive Free/Open Source license, or GNU GPLv3 for a strong copyleft Free/Open Source license. This is preferable to a statement such as "You can use this however you want" -- that statement is ambiguous in to whom the license refers, so it might actually not be a Free/Open Source license!
Use clang++ or g++ with -std=c++11
get an error::
/tmp/prettyprint.hpp:207:17: error: call to 'begin' is ambiguous
if (begin(_container) != end(_container))
^~~~~
Here is a simple patch to fix this:
--- prettyprint_old.hpp 2012-07-13 10:37:02.521452515 +0800
+++ prettyprint.hpp 2012-07-13 10:39:05.604789149 +0800
@@ -204,8 +204,9 @@
if (delimiters_type::values.prefix != NULL)
stream << delimiters_type::values.prefix;
- if (begin(_container) != end(_container))
- for (TIter it = begin(_container), it_end = end(_container); ; )
+ if (::pretty_print::begin(_container) != ::pretty_print::end(_container))
+ for (TIter it = ::pretty_print::begin(_container),
+ it_end = ::pretty_print::end(_container); ; )
{
stream << *it;
prettyprint98.hpp
generates this with GCC 4.7 and -Wshadow
:
prettyprint98.hpp: In constructor 'pretty_print::custom_delims_wrapper<T, Delims>::custom_delims_wrapper(const T&)':
prettyprint98.hpp:241:44: warning: declaration of 't' shadows a member of 'this' [-Wshadow]
prettyprint98.hpp: In member function 'std::ostream& pretty_print::custom_delims_wrapper<T, Delims>::stream(std::ostream&)':
prettyprint98.hpp:244:9: warning: declaration of 'stream' shadows a member of 'this' [-Wshadow]
prettyprint98.hpp: In member function 'std::wostream& pretty_print::custom_delims_wrapper<T, Delims>::stream(std::wostream&)':
prettyprint98.hpp:248:9: warning: declaration of 'stream' shadows a member of 'this' [-Wshadow]
As -Wshadow
is a very useful warning and I would like my programs to be warning-free, I kindly ask to fix this :-)
Apparently, it is not possible to define custom delimiters for tuples.
#include <iostream>
#include <tuple>
#include <prettyprint/prettyprint.hpp>
struct delims { static const pretty_print::delimiters_values<char> values;};
const pretty_print::delimiters_values<char> delims::values = {"<", "," ">"};
int main() {
auto t = std::make_tuple(12.5, 4, "hello");
std::cout << pretty_print::custom_delims<delims>(t) << std::endl;
}
Compiled with:
clang++ -std=c++11 -stdlib=libc++ pretty.cpp -o pretty
compiler:
$ clang --version
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix
Error:
/usr/local/include/prettyprint/prettyprint.hpp:205:27: error: no matching
function for call to 'begin'
Thanks, Ilio.
The following code fails to compile with the Intel Compiler icc (ICC) 2021.1 Beta 20200827
:
gcc and clang compile this code without problems.
// ex.cpp
#include<iostream>
#include<vector>
#include<utility>
#include "prettyprint.hpp"
int main() {
std::vector<std::pair<int, std::vector<int>>> v;
std::cout << v << std::endl;
return 1;
}
I ran the code using a docker container based on the official intel image intel/oneapi-hpckit.
The following is the slightly cleaned-up output of icc
including the setup of the docker container.
> ls
ex.cpp
prettyprint.hpp
> docker run --rm -it -v$(pwd):/src:ro intel/oneapi-hpckit
# icc -I /src/ /src/ex.cpp
/src/prettyprint.hpp(152): error: invalid partial specialization -- class "pretty_print::print_container_helper<T, TChar, TCharTraits, TDelimiters>::printer<std::pair<int, std::vector<int, std::allocator<int>>>> [with T=std::vector<int, std::allocator<int>>, TChar=char, TCharTraits=std::char_traits<char>, TDelimiters=pretty_print::delimiters<std::vector<int, std::allocator<int>>, char>]" is already fully specialized
struct print_container_helper<T, TChar, TCharTraits, TDelimiters>::printer<std::pair<T1, T2>> {
^
detected during:
instantiation of class "pretty_print::print_container_helper<T, TChar, TCharTraits, TDelimiters> [with T=std::vector<int, std::allocator<int>>, TChar=char, TCharTraits=std::char_traits<char>, TDelimiters=pretty_print::delimiters<std::vector<int, std::allocator<int>>, char>]" at line 475
instantiation of "std::enable_if<pretty_print::is_container<T>::value, std::basic_ostream<TChar, TCharTraits> &>::type std::operator<<(std::basic_ostream<TChar, TCharTraits> &, const T &) [with T=std::vector<int, std::allocator<int>>, TChar=char, TCharTraits=std::char_traits<char>]" at line 160
instantiation of "void pretty_print::print_container_helper<T, TChar, TCharTraits, TDelimiters>::printer<std::pair<T1, T2>>::print_body(const std::pair<T1, T2> &, pretty_print::print_container_helper<T, TChar, TCharTraits, TDelimiters>::printer<std::pair<T1, T2>>::ostream_type &) [with T=std::pair<int, std::vector<int, std::allocator<int>>>, TChar=char, TCharTraits=std::char_traits<char>, TDelimiters=pretty_print::delimiters<std::pair<int, std::vector<int, std::allocator<int>>>, char>, T1=int, T2=std::vector<int, std::allocator<int>>]" at line 138
instantiation of "void pretty_print::print_container_helper<T, TChar, TCharTraits, TDelimiters>::operator()(pretty_print::print_container_helper<T, TChar, TCharTraits, TDelimiters>::ostream_type &) const [with T=std::pair<int, std::vector<int, std::allocator<int>>>, TChar=char, TCharTraits=std::char_traits<char>, TDelimiters=pretty_print::delimiters<std::pair<int, std::vector<int, std::allocator<int>>>, char>]" at line 211
instantiation of "std::basic_ostream<TChar, TCharTraits> &pretty_print::operator<<(std::basic_ostream<TChar, TCharTraits> &, const pretty_print::print_container_helper<T, TChar, TCharTraits, TDelimiters> &) [with T=std::pair<int, std::vector<int, std::allocator<int>>>, TChar=char, TCharTraits=std::char_traits<char>, TDelimiters=pretty_print::delimiters<std::pair<int, std::vector<int, std::allocator<int>>>, char>]" at line 475
instantiation of "std::enable_if<pretty_print::is_container<T>::value, std::basic_ostream<TChar, TCharTraits> &>::type std::operator<<(std::basic_ostream<TChar, TCharTraits> &, const T &) [with T=std::pair<int, std::vector<int, std::allocator<int>>>, TChar=char, TCharTraits=std::char_traits<char>]" at line 116
instantiation of "void pretty_print::print_container_helper<T, TChar, TCharTraits, TDelimiters>::printer<U>::print_body(const U &, pretty_print::print_container_helper<T, TChar, TCharTraits, TDelimiters>::ostream_type &) [with T=std::vector<std::pair<int, std::vector<int, std::allocator<int>>>, std::allocator<std::pair<int, std::vector<int, std::allocator<int>>>>>, TChar=char, TCharTraits=std::char_traits<char>, TDelimiters=pretty_print::delimiters<std::vector<std::pair<int, std::vector<int, std::allocator<int>>>, std::allocator<std::pair<int, std::vector<int, std::allocator<int>>>>>, char>, U=std::vector<std::pair<int, std::vector<int, std::allocator<int>>>, std::allocator<std::pair<int, std::vector<int, std::allocator<int>>>>>]" at line 138
instantiation of "void pretty_print::print_container_helper<T, TChar, TCharTraits, TDelimiters>::operator()(pretty_print::print_container_helper<T, TChar, TCharTraits, TDelimiters>::ostream_type &) const [with T=std::vector<std::pair<int, std::vector<int, std::allocator<int>>>, std::allocator<std::pair<int, std::vector<int, std::allocator<int>>>>>, TChar=char, TCharTraits=std::char_traits<char>, TDelimiters=pretty_print::delimiters<std::vector<std::pair<int, std::vector<int, std::allocator<int>>>, std::allocator<std::pair<int, std::vector<int, std::allocator<int>>>>>, char>]" at line 211
instantiation of "std::basic_ostream<TChar, TCharTraits> &pretty_print::operator<<(std::basic_ostream<TChar, TCharTraits> &, const pretty_print::print_container_helper<T, TChar, TCharTraits, TDelimiters> &) [with T=std::vector<std::pair<int, std::vector<int, std::allocator<int>>>, std::allocator<std::pair<int, std::vector<int, std::allocator<int>>>>>, TChar=char, TCharTraits=std::char_traits<char>, TDelimiters=pretty_print::delimiters<std::vector<std::pair<int, std::vector<int, std::allocator<int>>>, std::allocator<std::pair<int, std::vector<int, std::allocator<int>>>>>, char>]" at line 475
instantiation of "std::enable_if<pretty_print::is_container<T>::value, std::basic_ostream<TChar, TCharTraits> &>::type std::operator<<(std::basic_ostream<TChar, TCharTraits> &, const T &) [with T=std::vector<std::pair<int, std::vector<int, std::allocator<int>>>, std::allocator<std::pair<int, std::vector<int, std::allocator<int>>>>>, TChar=char, TCharTraits=std::char_traits<char>]" at line 9 of "/src/ex.cpp"
compilation aborted for /src/ex.cpp (code 2)
I was trying to compile ppdemo.cpp
with the latest version of VS 2017 (ver. 15.9.14), and got the following internal compiler error:
D:\Work_OSS\cxx-prettyprint>cl /EHsc ppdemo.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27032.1 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
ppdemo.cpp
d:\work_oss\cxx-prettyprint\prettyprint.hpp(62): fatal error C1001: An internal error has occurred in the compiler.
(compiler file 'd:\agent\_work\2\s\src\vctools\compiler\cxxfe\sl\p1\c\types.c', line 4563)
To work around this problem, try simplifying or changing the program near the locations listed above.
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
d:\work_oss\cxx-prettyprint\prettyprint.hpp(69): note: see reference to class template instantiation 'pretty_print::detail::has_begin_end<T>' being compiled
This PR changes has_const_iterator
to additionally check
- for
std::iterator_traits
to exist and - for its
return_type
to be non-void
.
This catches additional cases and is compatible with the iterator rules up to C++20.
Motivation for this change is the incompatibility with the new iterator interface of Eigen3, which sets const_iterator = void
for non-vector types. This breaks prettyprint
.
How can we use this in online compilers that don't have the option to add multiple files? Is this library can be accessed with another method?
This adds support for std::optional. It uses a feature-test macro to determine whether the compiler supports that. I added a fourth delimiter type in order to print "nullopt" when the optional is empty.