nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
42.4k stars 6.67k forks source link

std::function error #4050

Closed hevandpatel closed 1 year ago

hevandpatel commented 1 year ago

Description

g++ 4.9.2 and nlohmann Json version 3.11.2, error:

Reproduction steps

include

include

include

include "nlohmann/json.hpp"

void Foo(const nlohmann::json &j) { std::cout<<j.dump()<<std::endl; }

using Handler = std::function<void(const nlohmann::json&)>;

int main() { std::map<int, Handler> handlers; handlers.emplace(1, std::bind(Foo, std::placeholders::_1));

for(auto &i : handlers) {
    i.second(nlohmann::json{{"data", "123"}});
}

}

Expected vs. actual results

Need to compile successfully

Minimal code example

No response

Error messages

/bin/sh -c 'make -e -f  Makefile'
----------Building project:[ HelloWorld - QuecOpen ]----------
make[1]: Entering directory '/home/hevan/QuecTelEC25/HelloWorld/HelloWorld'
/home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/x86_64-oesdk-linux/usr/bin/arm-oe-linux-gnueabi/arm-oe-linux-gnueabi-g++  -c  "/home/hevan/QuecTelEC25/HelloWorld/HelloWorld/main.cpp" -O2 -std=c++11 -Wall -DNDEBUG  -o ./Release/main.cpp.o -I. -I. -I./libs -I/home/hevan/QuecTelEC25/Libs/Modbus/modbus/ -I/home/hevan/QuecTelEC25/HelloWorld/libs/muParser/include/ -I/home/hevan/QuecTelEC25/HelloWorld/libs/
In file included from /home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/include/c++/4.9.2/functional:55:0,
                 from /home/hevan/QuecTelEC25/HelloWorld/HelloWorld/main.cpp:4:
/home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/include/c++/4.9.2/tuple: In instantiation of 'constexpr std::_Head_base<_Idx, _Head, false>::_Head_base() [with unsigned int _Idx = 0u; _Head = const nlohmann::json_abi_v3_11_2::basic_json<>&]':
/home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/include/c++/4.9.2/tuple:251:29:   required from 'constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl() [with unsigned int _Idx = 0u; _Head = const nlohmann::json_abi_v3_11_2::basic_json<>&; _Tail = {}]'
/home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/include/c++/4.9.2/type_traits:806:28:   required by substitution of 'template<class _Tp, class> static std::true_type std::__do_is_default_constructible_impl::__test(int) [with _Tp = std::_Tuple_impl<0u, const nlohmann::json_abi_v3_11_2::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long long int, long long unsigned int, double, std::allocator, nlohmann::json_abi_v3_11_2::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >&>; <template-parameter-1-2> = <missing>]'
/home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/include/c++/4.9.2/type_traits:817:37:   required from 'struct std::__is_default_constructible_impl<std::_Tuple_impl<0u, const nlohmann::json_abi_v3_11_2::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long long int, long long unsigned int, double, std::allocator, nlohmann::json_abi_v3_11_2::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >&> >'
/home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/include/c++/4.9.2/type_traits:134:12:   required from 'struct std::__and_<std::__not_<std::is_void<std::_Tuple_impl<0u, const nlohmann::json_abi_v3_11_2::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long long int, long long unsigned int, double, std::allocator, nlohmann::json_abi_v3_11_2::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >&> > >, std::__is_default_constructible_impl<std::_Tuple_impl<0u, const nlohmann::json_abi_v3_11_2::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long long int, long long unsigned int, double, std::allocator, nlohmann::json_abi_v3_11_2::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >&> > >'
/home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/include/c++/4.9.2/type_traits:821:12:   required from 'struct std::__is_default_constructible_atom<std::_Tuple_impl<0u, const nlohmann::json_abi_v3_11_2::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long long int, long long unsigned int, double, std::allocator, nlohmann::json_abi_v3_11_2::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >&> >'
/home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/include/c++/4.9.2/type_traits:842:12:   [ skipping 25 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/include/c++/4.9.2/functional:1322:60:   required from '_Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) [with _Args = {const nlohmann::json_abi_v3_11_2::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long long int, long long unsigned int, double, std::allocator, nlohmann::json_abi_v3_11_2::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >&}; _Result = void; _Functor = void (*)(const nlohmann::json_abi_v3_11_2::basic_json<>&); _Bound_args = {std::_Placeholder<1>}]'
/home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/include/c++/4.9.2/functional:2039:3:   required from 'static void std::_Function_handler<void(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes ...) [with _Functor = std::_Bind<void (*(std::_Placeholder<1>))(const nlohmann::json_abi_v3_11_2::basic_json<>&)>; _ArgTypes = {const nlohmann::json_abi_v3_11_2::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long long int, long long unsigned int, double, std::allocator, nlohmann::json_abi_v3_11_2::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >&}]'
/home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/include/c++/4.9.2/functional:2427:19:   required from 'std::function<_Res(_ArgTypes ...)>::function(_Functor) [with _Functor = std::_Bind<void (*(std::_Placeholder<1>))(const nlohmann::json_abi_v3_11_2::basic_json<>&)>; <template-parameter-2-2> = void; _Res = void; _ArgTypes = {const nlohmann::json_abi_v3_11_2::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long long int, long long unsigned int, double, std::allocator, nlohmann::json_abi_v3_11_2::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >&}]'
/home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/include/c++/4.9.2/bits/stl_pair.h:145:64:   required from 'constexpr std::pair<_T1, _T2>::pair(_U1&&, _U2&&) [with _U1 = int; _U2 = std::_Bind<void (*(std::_Placeholder<1>))(const nlohmann::json_abi_v3_11_2::basic_json<>&)>; <template-parameter-2-3> = void; _T1 = const int; _T2 = std::function<void(const nlohmann::json_abi_v3_11_2::basic_json<>&)>]'
/home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/include/c++/4.9.2/ext/new_allocator.h:120:4:   required from 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const int, std::function<void(const nlohmann::json_abi_v3_11_2::basic_json<>&)> >; _Args = {int, std::_Bind<void (*(std::_Placeholder<1>))(const nlohmann::json_abi_v3_11_2::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long long int, long long unsigned int, double, std::allocator, nlohmann::json_abi_v3_11_2::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >&)>}; _Tp = std::_Rb_tree_node<std::pair<const int, std::function<void(const nlohmann::json_abi_v3_11_2::basic_json<>&)> > >]'
/home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/include/c++/4.9.2/bits/alloc_traits.h:253:4:   required from 'static std::_Require<typename std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::type> std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::pair<const int, std::function<void(const nlohmann::json_abi_v3_11_2::basic_json<>&)> >; _Args = {int, std::_Bind<void (*(std::_Placeholder<1>))(const nlohmann::json_abi_v3_11_2::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long long int, long long unsigned int, double, std::allocator, nlohmann::json_abi_v3_11_2::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >&)>}; _Alloc = std::allocator<std::_Rb_tree_node<std::pair<const int, std::function<void(const nlohmann::json_abi_v3_11_2::basic_json<>&)> > > >; std::_Require<typename std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::type> = void]'
/home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/include/c++/4.9.2/bits/alloc_traits.h:399:57:   required from 'static decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::pair<const int, std::function<void(const nlohmann::json_abi_v3_11_2::basic_json<>&)> >; _Args = {int, std::_Bind<void (*(std::_Placeholder<1>))(const nlohmann::json_abi_v3_11_2::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long long int, long long unsigned int, double, std::allocator, nlohmann::json_abi_v3_11_2::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >&)>}; _Alloc = std::allocator<std::_Rb_tree_node<std::pair<const int, std::function<void(const nlohmann::json_abi_v3_11_2::basic_json<>&)> > > >; decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) = <type error>]'
/home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/include/c++/4.9.2/bits/stl_tree.h:423:42:   required from 'std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {int, std::_Bind<void (*(std::_Placeholder<1>))(const nlohmann::json_abi_v3_11_2::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long long int, long long unsigned int, double, std::allocator, nlohmann::json_abi_v3_11_2::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >&)>}; _Key = int; _Val = std::pair<const int, std::function<void(const nlohmann::json_abi_v3_11_2::basic_json<>&)> >; _KeyOfValue = std::_Select1st<std::pair<const int, std::function<void(const nlohmann::json_abi_v3_11_2::basic_json<>&)> > >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, std::function<void(const nlohmann::json_abi_v3_11_2::basic_json<>&)> > >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const int, std::function<void(const nlohmann::json_abi_v3_11_2::basic_json<>&)> > >*]'
/home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/include/c++/4.9.2/bits/stl_tree.h:1743:64:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_emplace_unique(_Args&& ...) [with _Args = {int, std::_Bind<void (*(std::_Placeholder<1>))(const nlohmann::json_abi_v3_11_2::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long long int, long long unsigned int, double, std::allocator, nlohmann::json_abi_v3_11_2::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >&)>}; _Key = int; _Val = std::pair<const int, std::function<void(const nlohmann::json_abi_v3_11_2::basic_json<>&)> >; _KeyOfValue = std::_Select1st<std::pair<const int, std::function<void(const nlohmann::json_abi_v3_11_2::basic_json<>&)> > >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, std::function<void(const nlohmann::json_abi_v3_11_2::basic_json<>&)> > >]'
/home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/include/c++/4.9.2/bits/stl_map.h:576:64:   required from 'std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename __gnu_cxx::__alloc_traits<_Allocator>::rebind<std::pair<const _Key, _Tp> >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::emplace(_Args&& ...) [with _Args = {int, std::_Bind<void (*(std::_Placeholder<1>))(const nlohmann::json_abi_v3_11_2::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long long int, long long unsigned int, double, std::allocator, nlohmann::json_abi_v3_11_2::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >&)>}; _Key = int; _Tp = std::function<void(const nlohmann::json_abi_v3_11_2::basic_json<>&)>; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, std::function<void(const nlohmann::json_abi_v3_11_2::basic_json<>&)> > >; typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename __gnu_cxx::__alloc_traits<_Allocator>::rebind<std::pair<const _Key, _Tp> >::other>::iterator = std::_Rb_tree_iterator<std::pair<const int, std::function<void(const nlohmann::json_abi_v3_11_2::basic_json<>&)> > >]'
/home/hevan/QuecTelEC25/HelloWorld/HelloWorld/main.cpp:25:62:   required from here
/home/hevan/QuecTel_BSP/EC25EFAR06A12M4G_OCPU_20.002.20.002_SDK/ql-ol-sdk/ql-ol-crosstool/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/include/c++/4.9.2/tuple:131:22: error: value-initialization of reference type 'const nlohmann::json_abi_v3_11_2::basic_json<>&'
       : _M_head_impl() { }
                      ^
HelloWorld.mk:95: recipe for target 'Release/main.cpp.o' failed
make[1]: *** [Release/main.cpp.o] Error 1
make[1]: Leaving directory '/home/hevan/QuecTelEC25/HelloWorld/HelloWorld'
Makefile:4: recipe for target 'All' failed
make: *** [All] Error 2
====19 errors, 1 warnings====

Compiler and operating system

g++ 4.9.2 and cross compiling on Ubutu 20 for arm-a7

Library version

latest release, old release as well

Validation

gregmarr commented 1 year ago

This works in g++ 5.2 and later. #2888

nlohmann commented 1 year ago

Closing as it is fixed with a later GCC version.