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

Cant compile with -C++17 and beyond compiler options #1362

Closed
KennethThompson opened this issue Nov 18, 2018 · 25 comments
Closed

Cant compile with -C++17 and beyond compiler options #1362

KennethThompson opened this issue Nov 18, 2018 · 25 comments

Comments

@KennethThompson
Copy link

See image below. I've added the #include suggested by a previous post, but that does not seem to resolve this.
image

@nlohmann
Copy link
Owner

The error message suggests that you try to convert a JSON value to a user-defined type, but there was not conversion function found. I do not know how this relates to the title of the issue. Could you provide more information?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Nov 18, 2018
@KennethThompson
Copy link
Author

Ok, so a bit more info. I was previously compiling with the -C++14 option and the project was building fine against your json library. I've since tried to switch to -C++17 (and beyond) and I keep running into the error message above. Here is the function that its complaining about:

image

@nlohmann
Copy link
Owner

It's hard to debug based on screen shots. Which version of the library are you using? Could you provide a small example demonstrating the issue? Which compiler are you using?

@KennethThompson
Copy link
Author

I'm running against HEAD, let me gen up some example code to simplify

@KennethThompson
Copy link
Author

KennethThompson commented Nov 18, 2018

#include <stdio.h>
#include "json.hpp"

    

int main(int argc, char **argv)
{
	printf("hello world\n");
    nlohmann::json _j;
    std::string tmp = _j["test"];  // OK
    
        //std::string tempstring;
        try
        {
            std::string tempstring = _j["setting"]; // OK
        }
        catch( std::exception &ex )
        {
            
        }
        
        std::string tempstring2;
        try
        {
            tempstring2 = _j["setting"];  // FAIL
        }
        catch( std::exception &ex )
        {
            
        }
}

@KennethThompson
Copy link
Author

Works just fine when compiling std==c++14, breaks when I attempt to go beyond C++14

@nlohmann
Copy link
Owner

Which compiler are you using?

@KennethThompson
Copy link
Author

gcc thats built into codelite

@nlohmann
Copy link
Owner

Which version is that?

@KennethThompson
Copy link
Author

uhh, let me see..

ken@ken-PowerEdge-R420:/git-repos/json$ g++ --version
g++ (Ubuntu 7.3.0-27ubuntu1
18.04) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

ken@ken-PowerEdge-R420:~/git-repos/json$

@nlohmann
Copy link
Owner

Thank you. (This just took 4 questions to get all the information that was originally asked in the issue template :-/)

@nlohmann nlohmann removed the state: needs more info the author of the issue needs to provide more details label Nov 18, 2018
@KennethThompson
Copy link
Author

my apologies. I've gotten rusty and lazy

@KennethThompson
Copy link
Author

In my defense, I did at least read previous posts on C++17 compiling:)

@nlohmann
Copy link
Owner

With GCC 7.3 on macOS

g++-7 (Homebrew GCC 7.3.0) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

your code compiles without problems with

  • -std=c++11
  • -std=c++14
  • -std=c++17

@KennethThompson
Copy link
Author

odd, could I trouble you to check -std=c++1z ?

@nlohmann
Copy link
Owner

Works as well.

@KennethThompson
Copy link
Author

Please give me your complete list of compile flags you are using

@nlohmann
Copy link
Owner

Same with

g++ (Ubuntu 8.2.0-7ubuntu1) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

@nlohmann
Copy link
Owner

No flags:

g++-7 bug.cpp -I single_include/nlohmann -std=c++11

@KennethThompson
Copy link
Author

KennethThompson commented Nov 18, 2018

ken@ken-PowerEdge-R420:~/git-repos/septemmud/testjson$ /usr/bin/g++-7  -c  "/home/ken/git-repos/septemmud/testjson/main.cpp" -std=c++11  -o ./Debug/main.cpp.o -I. -I. -I../../json/src
ken@ken-PowerEdge-R420:~/git-repos/septemmud/testjson$ /usr/bin/g++-7  -c  "/home/ken/git-repos/septemmud/testjson/main.cpp" -std=c++17  -o ./Debug/main.cpp.o -I. -I. -I../../json/src
In file included from /home/ken/git-repos/septemmud/testjson/main.cpp:2:0:
../../json/src/json.hpp: In instantiation of ‘void nlohmann::detail::from_json_fn::call(const BasicJsonType&, T&, nlohmann::detail::priority_tag<0>) const [with BasicJsonType = nlohmann::basic_json<>; T = std::basic_string_view<char>]’:
../../json/src/json.hpp:1128:47:   required from ‘void nlohmann::detail::from_json_fn::operator()(const BasicJsonType&, T&) const [with BasicJsonType = nlohmann::basic_json<>; T = std::basic_string_view<char>]’
../../json/src/json.hpp:1175:30:   required from ‘static void nlohmann::adl_serializer< <template-parameter-1-1>, <template-parameter-1-2> >::from_json(BasicJsonType&&, ValueType&) [with BasicJsonType = const nlohmann::basic_json<>&; ValueType = std::basic_string_view<char>; <template-parameter-1-1> = std::basic_string_view<char>; <template-parameter-1-2> = void]’
../../json/src/json.hpp:3478:45:   required from ‘ValueType nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer>::get() const [with ValueTypeCV = std::basic_string_view<char>; ValueType = std::basic_string_view<char>; typename std::enable_if<(((! std::is_same<nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer>, ValueType>::value) && nlohmann::detail::has_from_json<nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer>, ValueType>::value) && (! nlohmann::detail::has_non_default_from_json<nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer>, ValueType>::value)), int>::type <anonymous> = 0; ObjectType = std::map; ArrayType = std::vector; StringType = std::__cxx11::basic_string<char>; BooleanType = bool; NumberIntegerType = long int; NumberUnsignedType = long unsigned int; NumberFloatType = double; AllocatorType = std::allocator; JSONSerializer = nlohmann::adl_serializer]’
../../json/src/json.hpp:3737:30:   required from ‘nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer>::operator ValueType() const [with ValueType = std::basic_string_view<char>; typename std::enable_if<(((! std::is_pointer<_Ptr>::value) && (! std::is_same<ValueType, typename StringType::value_type>::value)) && (! std::is_same<ValueType, std::initializer_list<typename StringType::value_type> >::value)), int>::type <anonymous> = 0; ObjectType = std::map; ArrayType = std::vector; StringType = std::__cxx11::basic_string<char>; BooleanType = bool; NumberIntegerType = long int; NumberUnsignedType = long unsigned int; NumberFloatType = double; AllocatorType = std::allocator; JSONSerializer = nlohmann::adl_serializer]’
/usr/include/c++/7/bits/basic_string.h:1475:14:   required from ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_If_sv<_Tp, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::assign(const _Tp&) [with _Tp = nlohmann::basic_json<>; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_If_sv<_Tp, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&> = std::__cxx11::basic_string<char>&]’
/usr/include/c++/7/bits/basic_string.h:791:35:   required from ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_If_sv<_Tp, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(const _Tp&) [with _Tp = nlohmann::basic_json<>; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_If_sv<_Tp, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&> = std::__cxx11::basic_string<char>&]’
/home/ken/git-repos/septemmud/testjson/main.cpp:25:39:   required from here
../../json/src/json.hpp:1119:9: error: static assertion failed: could not find from_json() method in T's namespace
         static_assert(sizeof(BasicJsonType) == 0,
         ^~~~~~~~~~~~~
ken@ken-PowerEdge-R420:~/git-repos/septemmud/testjson$

@KennethThompson
Copy link
Author

C++11 works fine, C++17 does not

@theodelrieu
Copy link
Contributor

This is due to #958.

There are multiple basic_string::operator= overloads, only one of them is templated, thus the compiler picks this one up instead of the std::basic_string one, which is chosen in previous C++ standards.
Looking at constructor (6) on cppreference tells that the compiler tries to convert the templated type to a std::string_view.
Note that allowing this conversion would create a dangling std::string_view, so the library will not add a from_json(json, std::string_view&) in any case.

I suggest using j.get<std::string>() or j.get_to(s) instead.

@KennethThompson
Copy link
Author

Thanks! I'll use your suggested alternative

@nlohmann
Copy link
Owner

@CalielOfSeptem Can I close the issue?

@KennethThompson
Copy link
Author

KennethThompson commented Dec 13, 2018 via email

kebbbnnn added a commit to kebbbnnn/NFHTTP that referenced this issue Mar 25, 2019
The error relates to this nlohmann/json#1362. Applying the suggested way to retrieve string fixes the issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants