Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion from vector<bool> to json fails to build #494

Closed
landler opened this issue Mar 10, 2017 · 8 comments
Closed

Conversion from vector<bool> to json fails to build #494

landler opened this issue Mar 10, 2017 · 8 comments

Comments

@landler
Copy link

landler commented Mar 10, 2017

The following code:

#include "json.hpp"
#include <vector>
using nlohmann::json;

int main() {
	std::vector<bool> boolVector;
	json j;
	j["bool_vector"] = boolVector;
};

Fails to compile on os x (clang-800.0.42.1) when built like this:
clang++ -std=c++11 -c main.cpp

Substituting vector with another container (like list) compiles ok.

This also compiles fine on a linux system (I don't have the version info available, will update if necessary).

Am I missing some compiler params / something else or is this a problem with the library?

The full compilation error is below:

In file included from main.cpp:1:
./json.hpp:835:9: error: static_assert failed "could not find to_json() method in T's namespace"
        static_assert(sizeof(BasicJsonType) == 0,
        ^             ~~~~~~~~~~~~~~~~~~~~~~~~~~
./json.hpp:844:16: note: in instantiation of function template specialization 'nlohmann::detail::to_json_fn::call<nlohmann::basic_json<std::map, std::vector, std::__1::basic_string<char>, bool, long long,
      unsigned long long, double, std::allocator, adl_serializer>, std::__1::__bit_const_reference<std::__1::vector<bool, std::__1::allocator<bool> > > >' requested here
        return call(j, std::forward<T>(val), priority_tag<1> {});
               ^
./json.hpp:934:9: note: in instantiation of function template specialization 'nlohmann::detail::to_json_fn::operator()<nlohmann::basic_json<std::map, std::vector, std::__1::basic_string<char>, bool, long long,
      unsigned long long, double, std::allocator, adl_serializer>, std::__1::__bit_const_reference<std::__1::vector<bool, std::__1::allocator<bool> > > >' requested here
        ::nlohmann::to_json(j, std::forward<ValueType>(val));
        ^
./json.hpp:1998:28: note: in instantiation of function template specialization 'nlohmann::adl_serializer<std::__1::__bit_const_reference<std::__1::vector<bool, std::__1::allocator<bool> > >,
      void>::to_json<nlohmann::basic_json<std::map, std::vector, std::__1::basic_string<char>, bool, long long, unsigned long long, double, std::allocator, adl_serializer>,
      std::__1::__bit_const_reference<std::__1::vector<bool, std::__1::allocator<bool> > > >' requested here
        JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
                           ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1740:31: note: in instantiation of function template specialization 'nlohmann::basic_json<std::map, std::vector, std::__1::basic_string<char>,
      bool, long long, unsigned long long, double, std::allocator, adl_serializer>::basic_json<std::__1::__bit_const_reference<std::__1::vector<bool, std::__1::allocator<bool> > >,
      std::__1::__bit_const_reference<std::__1::vector<bool, std::__1::allocator<bool> > >, 0>' requested here
            ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
                              ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1656:18: note: in instantiation of function template specialization 'std::__1::allocator<nlohmann::basic_json<std::map, std::vector,
      std::__1::basic_string<char>, bool, long long, unsigned long long, double, std::allocator, adl_serializer> >::construct<nlohmann::basic_json<std::map, std::vector, std::__1::basic_string<char>, bool, long
      long, unsigned long long, double, std::allocator, adl_serializer>, std::__1::__bit_const_reference<std::__1::vector<bool, std::__1::allocator<bool> > > >' requested here
            {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
                 ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1502:14: note: (skipping 8 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all)
            {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
             ^
./json.hpp:829:16: note: in instantiation of function template specialization 'nlohmann::detail::to_json<nlohmann::basic_json<std::map, std::vector, std::__1::basic_string<char>, bool, long long, unsigned long
      long, double, std::allocator, adl_serializer>, std::__1::vector<bool, std::__1::allocator<bool> >, 0>' requested here
        return to_json(j, std::forward<T>(val));
               ^
./json.hpp:844:16: note: in instantiation of function template specialization 'nlohmann::detail::to_json_fn::call<nlohmann::basic_json<std::map, std::vector, std::__1::basic_string<char>, bool, long long,
      unsigned long long, double, std::allocator, adl_serializer>, std::__1::vector<bool, std::__1::allocator<bool> > &>' requested here
        return call(j, std::forward<T>(val), priority_tag<1> {});
               ^
./json.hpp:934:9: note: in instantiation of function template specialization 'nlohmann::detail::to_json_fn::operator()<nlohmann::basic_json<std::map, std::vector, std::__1::basic_string<char>, bool, long long,
      unsigned long long, double, std::allocator, adl_serializer>, std::__1::vector<bool, std::__1::allocator<bool> > &>' requested here
        ::nlohmann::to_json(j, std::forward<ValueType>(val));
        ^
./json.hpp:1998:28: note: in instantiation of function template specialization 'nlohmann::adl_serializer<std::__1::vector<bool, std::__1::allocator<bool> >, void>::to_json<nlohmann::basic_json<std::map,
      std::vector, std::__1::basic_string<char>, bool, long long, unsigned long long, double, std::allocator, adl_serializer>, std::__1::vector<bool, std::__1::allocator<bool> > &>' requested here
        JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
                           ^
main.cpp:8:21: note: in instantiation of function template specialization 'nlohmann::basic_json<std::map, std::vector, std::__1::basic_string<char>, bool, long long, unsigned long long, double, std::allocator,
      adl_serializer>::basic_json<std::__1::vector<bool, std::__1::allocator<bool> > &, std::__1::vector<bool, std::__1::allocator<bool> >, 0>' requested here
        j["bool_vector"] = boolVector;
                           ^
1 error generated.
@nlohmann
Copy link
Owner

I can confirm the issue with clang version 5.0.0 (trunk 296991) and gcc 7.0.1 20170305 (experimental).

@nlohmann nlohmann changed the title Conversion from vector<bool> to json fails to build on clang-800.0.42.1 (OS X) Conversion from vector<bool> to json fails to build Mar 10, 2017
@theodelrieu
Copy link
Contributor

I forgot about that famous container ...
We need to add a from_json overload for vector<bool>

@nlohmann
Copy link
Owner

How was it excluded so far?

@gregmarr
Copy link
Contributor

The problem is that a version of to_json/from_json that works for vector<T> doesn't necessarily work for vector<bool>.

@nlohmann
Copy link
Owner

I understand. I was just curious what part of the bool-magic breaks the requirements of the current from_json...

@TurpentineDistillery
Copy link

Usually problems with vector<bool> arise from the impossibility of having a pointer/reference to an element thereof... Wonder if that's the case here too.

@theodelrieu
Copy link
Contributor

Exact, we iterate on container elements and call to_json on them.

Since vector<bool> iterators return proxy objects, it doesn't find the overload for those objects

@nlohmann nlohmann self-assigned this Mar 11, 2017
@nlohmann nlohmann added this to the Release 3.0.0 milestone Mar 11, 2017
nlohmann added a commit that referenced this issue Mar 11, 2017
Adds a to_json function for std::vector<bool> to allow implicit
conversion from bit vectors to basic_json.
@nlohmann
Copy link
Owner

Should be fixed with d9e2dd0. Waiting for Travis to complete.

@nlohmann nlohmann added solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Mar 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants