Code Monkey home page Code Monkey logo

cxx-prettyprint's People

Contributors

louisdx avatar toroidal-code avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cxx-prettyprint's Issues

GCC 4.7: error: 'valarray' is not a member of 'std'

With GCC 4.7.2 (flags: -Wall -Wextra -pedantic -std=c++11 -O2) and the latest prettyprint.hpp I get

prettyprint.hpp:89:48: error: 'valarray' is not a member of 'std'
prettyprint.hpp:89:48: error: 'valarray' is not a member of 'std'
prettyprint.hpp:89:64: error: template argument 1 is invalid
prettyprint.hpp:89:65: error: expected unqualified-id before '>' token
prettyprint.hpp:89:97: warning: extra ';' [-pedantic]

The header needs to #include <valarray>.

Custom delimiters with tuples

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.

Internal compiler error with MSVC (VS Community 2017, ver. 15.9.14)

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

prettyprint98.hpp: foward declaration of std names leads to undefined behaviour

The foward declaration of set and multiset in prettyprint98.hpp leads to undefined behaviour. See the following discussion:
https://groups.google.com/forum/?fromgroups=#!topic/comp.lang.c++.moderated/XBKFjGNquDM

Please remove the forward declarations and just include . I think the impact on compilation speed is negligible, too.

(This is a real world issue since I can't compile prettyprint together with boost/archive due to that.)

Does not build on VC++ 2013

I am running Visual Studio 2013 with update 4 installed. This code has multiple build issues.

  1. In the pair and tuple specializations for print_container_helper::printer, it chokes on this line:
using ostream_type = print_container_helper<T, TChar, TCharTraits, TDelimiters>::ostream_type;

I've fixed it locally with this:

#ifdef _MSC_VER
    using ostream_type = std::basic_ostream<TChar, TCharTraits>;
#else
    using ostream_type = print_container_helper<T, TChar, TCharTraits, TDelimiters>::ostream_type;
#endif

This isn't the greatest fix because it hardcodes it to basic_ostream, rather than referencing print_container_helper::ostream_type, but at least it works.

  1. Once that builds, the library is unusable since it now chokes on the main interface that a caller would use: the << operator.
template<typename T, typename TChar, typename TCharTraits>
inline typename enable_if<::pretty_print::is_container<T>::value,
       basic_ostream<TChar, TCharTraits> &>::type
       operator<<(basic_ostream<TChar, TCharTraits>& stream, const T& container)
{
    return stream << ::pretty_print::print_container_helper<T, TChar, TCharTraits>(container);
}

Cannot resolve to the proper type in your most basic example:

  std::vector<int> foo;
  foo.push_back(1);
  foo.push_back(2);
  foo.push_back(3);

  std::cout << "My vector: " << foo << std::endl;

VS says:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::vector<int,std::allocator<_Ty>>' (or there is no acceptable conversion)

The user must specifically add each type they want the overloaded << operator to work with, such as:

template <typename T>
struct is_container<std::vector<T>> : std::true_type { };

template <typename T1, typename T2>
struct is_container<std::unordered_map<T1, T2>> : std::true_type { };

IMHO, if the main example shows it working with a vector, then it should work like that out of the box without the user having to specifically enter each type they want to work with.

With those changes, I have it working locally. Thanks.

Build conflict with Eigen

>prettyprint.hpp(117): error C3313: 'it': variable cannot have the type 'void'

        template <typename U>
        struct printer
        {
            static void print_body(const U & c, ostream_type & stream)
            {
                using std::begin;
                using std::end;

                auto it = begin(c); //<<< The error is here
                const auto the_end = end(c);

                if (it != the_end)
                {
                    for ( ; ; )
                    {
                        stream << *it;

                    if (++it == the_end) break;

                    if (delimiters_type::values.delimiter != NULL)
                        stream << delimiters_type::values.delimiter;
                    }
                }
            }
        };

It seems that some Eigen containers are detected as iterable: has_begin_end && has_const_iterator are true, but when using it begin and end function returns a void iterator.

As someone faced the same issue?
Is there a way to check that begin/end should not be void?

prettyprint98.hpp is not compatible with C++11.

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;

Use in online compilers

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?

Compilation issue

I get the following issue while trying to use the pretty print without modifications (I just renamed the file)

1>c:\main\dlmu\albfd\include\util\utlprettyprint.hpp(160): error C2061: syntax error: identifier 'ostream_type'
1> c:\main\dlmu\albfd\include\util\utlprettyprint.hpp(169): note: see reference to class template instantiation 'pretty_print::print_container_helper<T,TChar,TCharTraits,TDelimiters>::printerstd::pair<_Other1,_Other2>' being compiled
1>c:\main\dlmu\albfd\include\util\utlprettyprint.hpp(160): error C2238: unexpected token(s) preceding ';'
1>c:\main\dlmu\albfd\include\util\utlprettyprint.hpp(177): error C2061: syntax error: identifier 'ostream_type'
1> c:\main\dlmu\albfd\include\util\utlprettyprint.hpp(208): note: see reference to class template instantiation 'pretty_print::print_container_helper<T,TChar,TCharTraits,TDelimiters>::printerstd::tuple<_Types1...>' being compiled
1>c:\main\dlmu\albfd\include\util\utlprettyprint.hpp(177): error C2238: unexpected token(s) preceding ';'

Intel Compiler Error: invalid partial specialization

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)

Standard C++ violation?

Having a look to your code I see you add to namaspace std the following:
inline basic_ostream<TChar, TCharTraits> & operator<<(basic_ostream<TChar, TCharTraits> & stream, const tuple<TUPLE_ARGS> & value)

Is this legal c++? because of:
[C++11: 17.6.4.2.1/1]: The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.

this is not legal.
I'm missing something?
Anyway, thank a lot for this nice piece of code.

Is there an option to quote std::string (e.g., [" ", ""] instead of [ , ])?

First, thank you, the cxx-prettyprint library is really great. However, I wanted the strings in my output to be quoted, especially non-trimmed strings are otherwise hard to understand.

Example: [" ", ""] instead of [ , ]

I'm not sure if it is possible to customize it with the existing template specializations, so I made some minor modification:
https://github.com/philipp-classen/cxx-prettyprint

The patch is good enough to solve my specific use case, however, if the option to quote string proves to be useful for others, I would expect there is a better way to implement it. So, no pull request but a prove of concept if others are interested.

VS November 2012 CTP cannot find operator<<

I use Visual Studio November 2012 CTP. It has the best support for C++11, including variadic templates. However, for some reason default importing scheme does not work for me: when I write:

#include "prettyprint.hpp"
// ...
vector<int> v;
std::cout << v;

I get "error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::vector<int,std::allocator<_Ty>>' (or there is no acceptable conversion)".

GCC with -Wshadow generates warnings

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 :-)

Qualify begin() and end() in the C++98 code

If prettyprint98.hpp is being built with a C++11 compiler, the calls to begin() and end() are ambiguous with the std:: versions of them, and produce an error. Qualify them like so around line 197:

if (pretty_print::begin(_container) != pretty_print::end(_container))
            for (TIter it = pretty_print::begin(_container), it_end = pretty_print::end(_container);;)

I'm sure the intent with the C++98 version is that it is *not being built with a C++11 compiler, but just to be safe.

prints std::wstring to std::cout as an array of short

This code:

#include <iostream>
#include <prettyprint.hpp>
int main() {
    std::wstring ws = L"Hi";
    std::cout << "ws = " << ws << std::endl;
}

prints:

ws = [72, 105]

instead of producing compiler error (as it do without second include)

License

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.