Need to use two classes header in together in C++ - c++

Hello I need to use two classes like blow
file for class c1
#include<iostream>
#include<variant>
class c1{
private:
...
std::variant<c2,int,double,std::string> a;
public:
...
c2 getA(){
return a;
}
};
and in file of class c2
#include<iostream>
#include<vector>
class c2{
private:
...
std::vector<c1> b;
public:
...
c1 getB(){
return b;
}
};
I think my code is not such good
any one can help to fix or do this
I have use like this in blow of these two headers
class c1;
class c2{...};
but I still can't create a variable with c1 class
I can only create pointers but I can't use them correctly in the std::variant
and I have tried to including them but compiler get error
what can I do for fix this?
please help
EDIT:
and I have tried to including them but compiler get error
this is that error
In file included from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/move.h:55,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/nested_exception.h:40,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/exception:144,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ios:39,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:38,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
from C:\Users\M\CLionProjects\MyJson\my_json\JSONObject.h:9,
from C:\Users\M\CLionProjects\MyJson\my_json\JSONObject.cpp:5:
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits: In instantiation of 'struct std::is_trivially_destructible<JSONObject>':
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits:2846:25: required from 'constexpr const bool std::is_trivially_destructible_v<JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:313:5: required from 'constexpr const bool std::__detail::__variant::_Traits<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>::_S_trivial_dtor'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:321:20: required from 'constexpr const bool std::__detail::__variant::_Traits<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>::_S_trivial_move_assign'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:646:16: required by substitution of 'template<class ... _Types> using _Move_assign_alias = std::__detail::__variant::_Move_assign_base<std::__detail::__variant::_Traits<_Types>::_S_trivial_move_assign, _Types ...> [with _Types = {int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject}]'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:649:12: required from 'struct std::__detail::__variant::_Variant_base<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:1038:11: required from 'class std::variant<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:\Users\M\CLionProjects\MyJson\my_json\JSONType.h:22:62: required from here
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits:1282:12: error: invalid use of incomplete type 'class JSONObject'
struct is_trivially_destructible
^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from C:\Users\M\CLionProjects\MyJson\my_json\JSONObject.h:14,
from C:\Users\M\CLionProjects\MyJson\my_json\JSONObject.cpp:5:
C:\Users\M\CLionProjects\MyJson\my_json\JSONType.h:14:7: note: forward declaration of 'class JSONObject'
class JSONObject;
^~~~~~~~~~
In file included from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/move.h:55,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/nested_exception.h:40,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/exception:144,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ios:39,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:38,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
from C:\Users\M\CLionProjects\MyJson\my_json\JSONObject.h:9,
from C:\Users\M\CLionProjects\MyJson\my_json\JSONObject.cpp:5:
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits: In instantiation of 'constexpr const bool std::is_trivially_destructible_v<JSONObject>':
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:313:5: required from 'constexpr const bool std::__detail::__variant::_Traits<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>::_S_trivial_dtor'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:321:20: required from 'constexpr const bool std::__detail::__variant::_Traits<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>::_S_trivial_move_assign'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:646:16: required by substitution of 'template<class ... _Types> using _Move_assign_alias = std::__detail::__variant::_Move_assign_base<std::__detail::__variant::_Traits<_Types>::_S_trivial_move_assign, _Types ...> [with _Types = {int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject}]'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:649:12: required from 'struct std::__detail::__variant::_Variant_base<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:1038:11: required from 'class std::variant<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:\Users\M\CLionProjects\MyJson\my_json\JSONType.h:22:62: required from here
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits:2846:25: error: 'value' is not a member of 'std::is_trivially_destructible<JSONObject>'
inline constexpr bool is_trivially_destructible_v =
^~~~~~~~~~~~~~~~~~~~~~~~~~~
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits: In instantiation of 'struct std::__is_trivially_move_constructible_impl<JSONObject, true>':
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits:1228:12: required from 'struct std::is_trivially_move_constructible<JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits:2834:25: required from 'constexpr const bool std::is_trivially_move_constructible_v<JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:317:5: required from 'constexpr const bool std::__detail::__variant::_Traits<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>::_S_trivial_move_ctor'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:523:70: required by substitution of 'template<class ... _Types> using _Move_ctor_alias = std::__detail::__variant::_Move_ctor_base<std::__detail::__variant::_Traits<_Types>::_S_trivial_move_ctor, _Types ...> [with _Types = {int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject}]'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:526:12: required from 'struct std::__detail::__variant::_Copy_assign_base<false, int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:580:12: required from 'struct std::__detail::__variant::_Move_assign_base<false, int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:649:12: required from 'struct std::__detail::__variant::_Variant_base<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:1038:11: required from 'class std::variant<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:\Users\M\CLionProjects\MyJson\my_json\JSONType.h:22:62: required from here
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits:1221:12: error: invalid use of incomplete type 'class JSONObject'
struct __is_trivially_move_constructible_impl<_Tp, true>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from C:\Users\M\CLionProjects\MyJson\my_json\JSONObject.h:14,
from C:\Users\M\CLionProjects\MyJson\my_json\JSONObject.cpp:5:
C:\Users\M\CLionProjects\MyJson\my_json\JSONType.h:14:7: note: forward declaration of 'class JSONObject'
class JSONObject;
^~~~~~~~~~
In file included from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/move.h:55,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/nested_exception.h:40,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/exception:144,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ios:39,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:38,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
from C:\Users\M\CLionProjects\MyJson\my_json\JSONObject.h:9,
from C:\Users\M\CLionProjects\MyJson\my_json\JSONObject.cpp:5:
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits: In instantiation of 'constexpr const bool std::is_trivially_move_constructible_v<JSONObject>':
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:317:5: required from 'constexpr const bool std::__detail::__variant::_Traits<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>::_S_trivial_move_ctor'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:523:70: required by substitution of 'template<class ... _Types> using _Move_ctor_alias = std::__detail::__variant::_Move_ctor_base<std::__detail::__variant::_Traits<_Types>::_S_trivial_move_ctor, _Types ...> [with _Types = {int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject}]'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:526:12: required from 'struct std::__detail::__variant::_Copy_assign_base<false, int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:580:12: required from 'struct std::__detail::__variant::_Move_assign_base<false, int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:649:12: required from 'struct std::__detail::__variant::_Variant_base<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:1038:11: required from 'class std::variant<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:\Users\M\CLionProjects\MyJson\my_json\JSONType.h:22:62: required from here
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits:2834:25: error: 'value' is not a member of 'std::is_trivially_move_constructible<JSONObject>'
inline constexpr bool is_trivially_move_constructible_v =
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits: In instantiation of 'struct std::__is_trivially_copy_constructible_impl<JSONObject, true>':
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits:1207:12: required from 'struct std::is_trivially_copy_constructible<JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits:2831:25: required from 'constexpr const bool std::is_trivially_copy_constructible_v<JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:315:5: required from 'constexpr const bool std::__detail::__variant::_Traits<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>::_S_trivial_copy_ctor'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:489:70: required by substitution of 'template<class ... _Types> using _Copy_ctor_alias = std::__detail::__variant::_Copy_ctor_base<std::__detail::__variant::_Traits<_Types>::_S_trivial_copy_ctor, _Types ...> [with _Types = {int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject}]'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:492:12: required from 'struct std::__detail::__variant::_Move_ctor_base<false, int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:526:12: required from 'struct std::__detail::__variant::_Copy_assign_base<false, int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:580:12: required from 'struct std::__detail::__variant::_Move_assign_base<false, int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:649:12: required from 'struct std::__detail::__variant::_Variant_base<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:1038:11: required from 'class std::variant<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:\Users\M\CLionProjects\MyJson\my_json\JSONType.h:22:62: required from here
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits:1200:12: error: invalid use of incomplete type 'class JSONObject'
struct __is_trivially_copy_constructible_impl<_Tp, true>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from C:\Users\M\CLionProjects\MyJson\my_json\JSONObject.h:14,
from C:\Users\M\CLionProjects\MyJson\my_json\JSONObject.cpp:5:
C:\Users\M\CLionProjects\MyJson\my_json\JSONType.h:14:7: note: forward declaration of 'class JSONObject'
class JSONObject;
^~~~~~~~~~
In file included from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/move.h:55,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/nested_exception.h:40,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/exception:144,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ios:39,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:38,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
from C:\Users\M\CLionProjects\MyJson\my_json\JSONObject.h:9,
from C:\Users\M\CLionProjects\MyJson\my_json\JSONObject.cpp:5:
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits: In instantiation of 'constexpr const bool std::is_trivially_copy_constructible_v<JSONObject>':
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:315:5: required from 'constexpr const bool std::__detail::__variant::_Traits<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>::_S_trivial_copy_ctor'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:489:70: required by substitution of 'template<class ... _Types> using _Copy_ctor_alias = std::__detail::__variant::_Copy_ctor_base<std::__detail::__variant::_Traits<_Types>::_S_trivial_copy_ctor, _Types ...> [with _Types = {int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject}]'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:492:12: required from 'struct std::__detail::__variant::_Move_ctor_base<false, int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:526:12: required from 'struct std::__detail::__variant::_Copy_assign_base<false, int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:580:12: required from 'struct std::__detail::__variant::_Move_assign_base<false, int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:649:12: required from 'struct std::__detail::__variant::_Variant_base<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:1038:11: required from 'class std::variant<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:\Users\M\CLionProjects\MyJson\my_json\JSONType.h:22:62: required from here
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits:2831:25: error: 'value' is not a member of 'std::is_trivially_copy_constructible<JSONObject>'
inline constexpr bool is_trivially_copy_constructible_v =
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits: In instantiation of 'struct std::is_literal_type<JSONObject>':
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits:2791:25: required from 'constexpr const bool std::is_literal_type_v<JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:356:33: recursively required from 'union std::__detail::__variant::_Variadic_union<double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:356:33: required from 'union std::__detail::__variant::_Variadic_union<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:413:34: required from 'struct std::__detail::__variant::_Variant_storage<false, int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:458:12: required from 'struct std::__detail::__variant::_Copy_ctor_base<false, int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:492:12: required from 'struct std::__detail::__variant::_Move_ctor_base<false, int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:526:12: required from 'struct std::__detail::__variant::_Copy_assign_base<false, int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:580:12: required from 'struct std::__detail::__variant::_Move_assign_base<false, int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:649:12: required from 'struct std::__detail::__variant::_Variant_base<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:1038:11: required from 'class std::variant<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
C:\Users\M\CLionProjects\MyJson\my_json\JSONType.h:22:62: required from here
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits:698:12: error: invalid use of incomplete type 'class JSONObject'
struct is_literal_type
^~~~~~~~~~~~~~~
In file included from C:\Users\M\CLionProjects\MyJson\my_json\JSONObject.h:14,
from C:\Users\M\CLionProjects\MyJson\my_json\JSONObject.cpp:5:
C:\Users\M\CLionProjects\MyJson\my_json\JSONType.h:14:7: note: forward declaration of 'class JSONObject'
class JSONObject;
^~~~~~~~~~
In file included from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/move.h:55,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/nested_exception.h:40,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/exception:144,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ios:39,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:38,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
from C:\Users\M\CLionProjects\MyJson\my_json\JSONObject.h:9,
from C:\Users\M\CLionProjects\MyJson\my_json\JSONObject.cpp:5:
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/type_traits: In instantiation of 'constexpr const bool std::is_literal_type_v<JSONObject>':
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/variant:356:33: recursively required from 'union std::__detail::__variant::_Variadic_union<double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, JSONObject>'
...
mingw32-make.exe[3]: *** [CMakeFiles\MyJson.dir\build.make:75: CMakeFiles/MyJson.dir/my_json/JSONObject.cpp.obj] Error 1
mingw32-make.exe[3]: *** Waiting for unfinished jobs....
mingw32-make.exe[3]: *** [CMakeFiles\MyJson.dir\build.make:88: CMakeFiles/MyJson.dir/my_json/JSONType.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/MyJson.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/MyJson.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: MyJson] Error 2

Each class should know about internal structures (at least size of data) for correct compilation. So, you can't create cycle definition.

Related

Gtest compiler error when invoking function with rvalue argument

I cant for the love of god understand what this compiler error is trying to tell me:
Reproduced here on godbolt: https://godbolt.org/z/v3M5hEbdv
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include <string>
#include <functional>
#include <vector>
class MyService {
public:
virtual ~MyService() = default;
virtual void bar(
std::function<void (std::vector<std::string>&&)> onSuccess
) = 0;
};
class MyServiceMock : public MyService
{
public:
MOCK_METHOD(
void,
bar,
(std::function<void(std::vector<std::string>&&)> onSuccess),
(override)
);
};
[[maybe_unused]]
void foo() {
testing::NiceMock<MyServiceMock> myService;
EXPECT_CALL(myService, bar(testing::_))
.Times(testing::Exactly(1))
.WillOnce(testing::DoAll(
testing::Return(),
testing::InvokeArgument<0>(
std::vector<std::string>({"hello", "world"})
)
));
}
int main() {
return 0;
}
The compiler error :
Output of x86-64 gcc 11.2 (Compiler #1)
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/unique_ptr.h:37,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/memory:76,
from /opt/compiler-explorer/libs/googletest/trunk/googletest/include/gtest/gtest.h:55,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/tuple: In instantiation of 'struct std::tuple_element<0, std::tuple<> >':
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/utility:118:11: required by substitution of 'template<long unsigned int __i, class _Tp> using __tuple_element_t = typename std::tuple_element::type [with long unsigned int __i = 0; _Tp = std::tuple<>]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/tuple:1403:5: required by substitution of 'template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_UTypes ...> >&& std::get(const std::tuple<_UTypes ...>&&) [with long unsigned int __i = 0; _Elements = {}]'
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-more-actions.h:515:22: required by substitution of 'template<class ... Args> decltype (testing::internal::InvokeArgument(get<0>(std::forward_as_tuple((forward<Args>)(testing::internal::InvokeArgumentAction<index, Params ...>::operator()::args)...)), declval<const std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&>())) testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >::operator()<Args ...>(Args&& ...) const [with Args = {}]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:2466:26: required by substitution of 'template<class _Fn, class ... _Args> static std::__result_of_success<decltype (declval<_Fn>()((declval<_Args>)()...)), std::__invoke_other> std::__result_of_other_impl::_S_test(int) [with _Fn = testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&; _Args = {}]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:2477:55: required from 'struct std::__result_of_impl<false, false, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&>'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:2482:12: [ skipping 3 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/std_function.h:344:8: required by substitution of 'template<class _Res, class ... _ArgTypes> template<class _Cond, class _Tp> using _Requires = typename std::enable_if<_Cond::value, _Tp>::type [with _Cond = std::function<void()>::_Callable<testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, std::__invoke_result<testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&> >; _Tp = void; _Res = void; _ArgTypes = {}]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/std_function.h:413:9: required by substitution of 'template<class _Functor, class, class> std::function<void()>::function(_Functor) [with _Functor = testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >; <template-parameter-1-2> = void; <template-parameter-1-3> = <missing>]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:906:30: required from 'struct std::__is_constructible_impl<std::function<void()>, const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&>'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:911:12: required from 'struct std::is_constructible<std::function<void()>, const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&>'
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-actions.h:466:7: required by substitution of 'template<class G, class> testing::Action<void(std::function<void(std::vector<std::__cxx11::basic_string<char> >&&)>)>::Action(G&&) [with G = const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&; <template-parameter-1-2> = <missing>]'
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-actions.h:1061:56: required from 'testing::internal::DoAllAction<Actions>::operator testing::Action<R(Args ...)>() const [with R = void; Args = {std::function<void(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&&)>}; Actions = {testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >}]'
<source>:33:18: required from here
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/tuple:1360:25: error: static assertion failed: tuple index must be in range
1360 | static_assert(__i < tuple_size<tuple<>>::value,
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/tuple:1360:25: note: '(0 < ((long unsigned int)std::integral_constant<long unsigned int, 0>::value))' evaluates to false
In file included from /opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock.h:57,
from <source>:2:
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-actions.h: In instantiation of 'testing::internal::DoAllAction<Actions>::operator testing::Action<R(Args ...)>() const [with R = void; Args = {std::function<void(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&&)>}; Actions = {testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >}]':
<source>:33:18: required from here
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-actions.h:1061:56: error: could not convert 'std::get<1, testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >(((const testing::internal::DoAllAction<testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >*)this)->testing::internal::DoAllAction<testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >::actions)' from 'std::__tuple_element_t<1, std::tuple<testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > >' {aka 'const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >'} to 'testing::Action<void(std::function<void(std::vector<std::__cxx11::basic_string<char> >&&)>)>'
1061 | std::get<sizeof...(Actions) - 1>(actions)};
| ^
| |
| std::__tuple_element_t<1, std::tuple<testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > > {aka const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >}
ASM generation compiler returned: 1
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/unique_ptr.h:37,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/memory:76,
from /opt/compiler-explorer/libs/googletest/trunk/googletest/include/gtest/gtest.h:55,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/tuple: In instantiation of 'struct std::tuple_element<0, std::tuple<> >':
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/utility:118:11: required by substitution of 'template<long unsigned int __i, class _Tp> using __tuple_element_t = typename std::tuple_element::type [with long unsigned int __i = 0; _Tp = std::tuple<>]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/tuple:1403:5: required by substitution of 'template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_UTypes ...> >&& std::get(const std::tuple<_UTypes ...>&&) [with long unsigned int __i = 0; _Elements = {}]'
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-more-actions.h:515:22: required by substitution of 'template<class ... Args> decltype (testing::internal::InvokeArgument(get<0>(std::forward_as_tuple((forward<Args>)(testing::internal::InvokeArgumentAction<index, Params ...>::operator()::args)...)), declval<const std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&>())) testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >::operator()<Args ...>(Args&& ...) const [with Args = {}]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:2466:26: required by substitution of 'template<class _Fn, class ... _Args> static std::__result_of_success<decltype (declval<_Fn>()((declval<_Args>)()...)), std::__invoke_other> std::__result_of_other_impl::_S_test(int) [with _Fn = testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&; _Args = {}]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:2477:55: required from 'struct std::__result_of_impl<false, false, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&>'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:2482:12: [ skipping 3 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/std_function.h:344:8: required by substitution of 'template<class _Res, class ... _ArgTypes> template<class _Cond, class _Tp> using _Requires = typename std::enable_if<_Cond::value, _Tp>::type [with _Cond = std::function<void()>::_Callable<testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, std::__invoke_result<testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&> >; _Tp = void; _Res = void; _ArgTypes = {}]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/std_function.h:413:9: required by substitution of 'template<class _Functor, class, class> std::function<void()>::function(_Functor) [with _Functor = testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >; <template-parameter-1-2> = void; <template-parameter-1-3> = <missing>]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:906:30: required from 'struct std::__is_constructible_impl<std::function<void()>, const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&>'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:911:12: required from 'struct std::is_constructible<std::function<void()>, const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&>'
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-actions.h:466:7: required by substitution of 'template<class G, class> testing::Action<void(std::function<void(std::vector<std::__cxx11::basic_string<char> >&&)>)>::Action(G&&) [with G = const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&; <template-parameter-1-2> = <missing>]'
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-actions.h:1061:56: required from 'testing::internal::DoAllAction<Actions>::operator testing::Action<R(Args ...)>() const [with R = void; Args = {std::function<void(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&&)>}; Actions = {testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >}]'
<source>:33:18: required from here
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/tuple:1360:25: error: static assertion failed: tuple index must be in range
1360 | static_assert(__i < tuple_size<tuple<>>::value,
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/tuple:1360:25: note: '(0 < ((long unsigned int)std::integral_constant<long unsigned int, 0>::value))' evaluates to false
In file included from /opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock.h:57,
from <source>:2:
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-actions.h: In instantiation of 'testing::internal::DoAllAction<Actions>::operator testing::Action<R(Args ...)>() const [with R = void; Args = {std::function<void(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&&)>}; Actions = {testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >}]':
<source>:33:18: required from here
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-actions.h:1061:56: error: could not convert 'std::get<1, testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >(((const testing::internal::DoAllAction<testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >*)this)->testing::internal::DoAllAction<testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >::actions)' from 'std::__tuple_element_t<1, std::tuple<testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > >' {aka 'const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >'} to 'testing::Action<void(std::function<void(std::vector<std::__cxx11::basic_string<char> >&&)>)>'
1061 | std::get<sizeof...(Actions) - 1>(actions)};
| ^
| |
| std::__tuple_element_t<1, std::tuple<testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > > {aka const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >}
Execution build compiler returned: 1
If I remove the &&and change the argument to the function from a rvalue reference into a copy, it compiles fine. What could be happening here?
It seems it is unable to work with r-value references. The workaround:
EXPECT_CALL(myService, bar(testing::_)).WillOnce(
testing::WithArg<0>(
[] (std::function<void (std::vector<std::string>&&)> f) {
f({"hello", "world"});
}
)
Related issue has been closed for some reasons Add support for move only parameters in mocked methods.

g++ compiler error: invalid use of incomplete type... struct is_trivially_destructible

I am using std::variant which is a C++17 feature. But as far as I am aware g++ should support this. I am using g++ version 9.2.1.
The first error summarised is:
json20.hpp:18:48: required from here
/usr/include/c++/9/type_traits:1235:12: error: invalid use of incomplete type ‘struct json_object’
1235 | struct is_trivially_destructible
| ^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from json20.cpp:1:
json20.hpp:17:8: note: forward declaration of ‘struct json_object’
17 | struct json_object {
| ^~~~~~~~~~~
In file included from /usr/include/c++/9/variant:36,
from json20.hpp:5,
from json20.cpp:1:
I am compiling just json.cpp like this:
g++ -c -std=c++17 json20.cpp
This compiles ok on Visual Studio 2017. Although there I had to set the compiler to use c++17 standard. So I assume it should work on g++. Or is there an error in my code?
Here are the source files:
json20.hpp:
#ifndef JSON20_HPP_
#define JSON20_HPP_
#include <variant> // type-safe union
#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>
#include "json_value.hpp"
struct json_array {
std::vector<json_value> array;
};
struct json_object {
std::unordered_map < std::string, json_value> object;
json_value& operator [](std::string key) {
return object[key];
}
};
struct json_null {
};
std::ostream& operator<< (std::ostream& os, json_value const& v);
class json20 {
public:
/* default constructor */
json20() {}
/* construct json object from input stream */
json20(std::istream& istrm, std::string& parse_status);
/* parent node of json object */
json_value root;
bool insert(const json_object& object);
private:
bool parse(std::istream& istrm, std::string& parse_status);
json_value parse_json(std::istream& istrm, json_value& value, std::string& parse_status);
};
#endif // JSON20_HPP_
json20.cpp:
#include "json20.hpp"
std::ostream& operator<< (std::ostream& os, json_value const& v) {
return os;
}
json20::json20(std::istream& istrm, std::string& parse_status) {
}
json_value json20::parse_json(std::istream& istrm, json_value& value, std::string& parse_status) {
return value;
}
bool json20::parse(std::istream& istrm, std::string& parse_status) {
return true;
}
bool json20::insert(const json_object& object) {
return true;
}
json_value.hpp:
#ifndef JSON_VALUE_HPP_
#define JSON_VALUE_HPP_
#include "json20.hpp"
#include <variant>
#include <string>
#include <unordered_map>
// fwd declare
struct json_array;
struct json_object;
struct json_null;
typedef std::variant<double, bool, std::string, json_array, json_object, json_null> json_value;
#endif // JSON_VALUE_HPP_
test.cpp:
#include "json20.hpp"
#include <string>
#include <iostream>
#include <sstream>
#include <unordered_map>
int main() {
const std::string s = "my list";
double arrayofdoubles[] = {1.0, 2.0, 3.0};
json_array jarray;
for (auto i : arrayofdoubles) {
jarray.array.push_back(i);
}
json_object obj;
obj.object[s] = jarray;
json20 json;
json.insert(obj);
//// output to stdout
std::ostringstream ostrm;
ostrm << json.root;
std::string expected("{\"my list\":[1,2,3]}");
std::cout << json.root;
}
The complete error message is huge and I am only able to provide a part of it due to stackoverflow size limits:
acomber#mail:~/Documents/projects/json20$ g++ -c -std=c++17 json20.cpp
In file included from /usr/include/c++/9/variant:36,
from json20.hpp:5,
from json20.cpp:1:
/usr/include/c++/9/type_traits: In instantiation of ‘struct std::is_trivially_destructible<json_object>’:
/usr/include/c++/9/type_traits:2959:25: required from ‘constexpr const bool std::is_trivially_destructible_v<json_object>’
/usr/include/c++/9/variant:286:5: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_dtor’
/usr/include/c++/9/variant:295:4: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_move_assign’
/usr/include/c++/9/variant:677:11: required by substitution of ‘template<class ... _Types> using _Move_assign_alias = std::__detail::__variant::_Move_assign_base<std::__detail::__variant::_Traits<_Types>::_S_trivial_move_assign, _Types ...> [with _Types = {double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null}]’
/usr/include/c++/9/variant:681:12: required from ‘struct std::__detail::__variant::_Variant_base<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>’
/usr/include/c++/9/variant:1219:11: [ skipping 3 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/c++/9/bits/hashtable_policy.h:233:43: required from ‘struct std::__detail::_Hash_node_value_base<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> > >’
/usr/include/c++/9/bits/hashtable_policy.h:264:12: required from ‘struct std::__detail::_Hash_node<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, true>’
/usr/include/c++/9/bits/hashtable_policy.h:2027:13: required from ‘struct std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, true> > >’
/usr/include/c++/9/bits/hashtable.h:173:11: required from ‘class std::_Hashtable<std::basic_string<char>, std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, std::allocator<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> > >, std::__detail::_Select1st, std::equal_to<std::basic_string<char> >, std::hash<std::basic_string<char> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >’
/usr/include/c++/9/bits/unordered_map.h:105:18: required from ‘class std::unordered_map<std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >’
json20.hpp:18:48: required from here
/usr/include/c++/9/type_traits:1235:12: error: invalid use of incomplete type ‘struct json_object’
1235 | struct is_trivially_destructible
| ^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from json20.cpp:1:
json20.hpp:17:8: note: forward declaration of ‘struct json_object’
17 | struct json_object {
| ^~~~~~~~~~~
In file included from /usr/include/c++/9/variant:36,
from json20.hpp:5,
from json20.cpp:1:
/usr/include/c++/9/type_traits: In instantiation of ‘constexpr const bool std::is_trivially_destructible_v<json_object>’:
/usr/include/c++/9/variant:286:5: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_dtor’
/usr/include/c++/9/variant:295:4: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_move_assign’
/usr/include/c++/9/variant:677:11: required by substitution of ‘template<class ... _Types> using _Move_assign_alias = std::__detail::__variant::_Move_assign_base<std::__detail::__variant::_Traits<_Types>::_S_trivial_move_assign, _Types ...> [with _Types = {double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null}]’
/usr/include/c++/9/variant:681:12: required from ‘struct std::__detail::__variant::_Variant_base<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>’
/usr/include/c++/9/variant:1219:11: required from ‘class std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>’
/usr/include/c++/9/bits/stl_pair.h:215:11: [ skipping 2 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/c++/9/bits/hashtable_policy.h:233:43: required from ‘struct std::__detail::_Hash_node_value_base<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> > >’
/usr/include/c++/9/bits/hashtable_policy.h:264:12: required from ‘struct std::__detail::_Hash_node<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, true>’
/usr/include/c++/9/bits/hashtable_policy.h:2027:13: required from ‘struct std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, true> > >’
/usr/include/c++/9/bits/hashtable.h:173:11: required from ‘class std::_Hashtable<std::basic_string<char>, std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, std::allocator<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> > >, std::__detail::_Select1st, std::equal_to<std::basic_string<char> >, std::hash<std::basic_string<char> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >’
/usr/include/c++/9/bits/unordered_map.h:105:18: required from ‘class std::unordered_map<std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >’
json20.hpp:18:48: required from here
/usr/include/c++/9/type_traits:2959:25: error: ‘value’ is not a member of ‘std::is_trivially_destructible<json_object>’
2959 | inline constexpr bool is_trivially_destructible_v =
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/9/type_traits: In instantiation of ‘struct std::is_trivially_destructible<json_null>’:
/usr/include/c++/9/type_traits:2959:25: required from ‘constexpr const bool std::is_trivially_destructible_v<json_null>’
/usr/include/c++/9/variant:286:5: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_dtor’
/usr/include/c++/9/variant:295:4: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_move_assign’
/usr/include/c++/9/variant:677:11: required by substitution of ‘template<class ... _Types> using _Move_assign_alias = std::__detail::__variant::_Move_assign_base<std::__detail::__variant::_Traits<_Types>::_S_trivial_move_assign, _Types ...> [with _Types = {double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null}]’
/usr/include/c++/9/variant:681:12: required from ‘struct std::__detail::__variant::_Variant_base<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>’
/usr/include/c++/9/variant:1219:11: [ skipping 3 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/c++/9/bits/hashtable_policy.h:233:43: required from ‘struct std::__detail::_Hash_node_value_base<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> > >’
/usr/include/c++/9/bits/hashtable_policy.h:264:12: required from ‘struct std::__detail::_Hash_node<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, true>’
/usr/include/c++/9/bits/hashtable_policy.h:2027:13: required from ‘struct std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, true> > >’
/usr/include/c++/9/bits/hashtable.h:173:11: required from ‘class std::_Hashtable<std::basic_string<char>, std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, std::allocator<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> > >, std::__detail::_Select1st, std::equal_to<std::basic_string<char> >, std::hash<std::basic_string<char> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >’
/usr/include/c++/9/bits/unordered_map.h:105:18: required from ‘class std::unordered_map<std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >’
json20.hpp:18:48: required from here
/usr/include/c++/9/type_traits:1235:12: error: invalid use of incomplete type ‘struct json_null’
1235 | struct is_trivially_destructible
| ^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from json20.hpp:11,
from json20.cpp:1:
json_value.hpp:13:8: note: forward declaration of ‘struct json_null’
13 | struct json_null;
| ^~~~~~~~~
In file included from /usr/include/c++/9/variant:36,
from json20.hpp:5,
from json20.cpp:1:
/usr/include/c++/9/type_traits: In instantiation of ‘constexpr const bool std::is_trivially_destructible_v<json_null>’:
/usr/include/c++/9/variant:286:5: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_dtor’
/usr/include/c++/9/variant:295:4: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_move_assign’
/usr/include/c++/9/variant:677:11: required by substitution of ‘template<class ... _Types> using _Move_assign_alias = std::__detail::__variant::_Move_assign_base<std::__detail::__variant::_Traits<_Types>::_S_trivial_move_assign, _Types ...> [with _Types = {double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null}]’
/usr/include/c++/9/variant:681:12: required from ‘struct std::__detail::__variant::_Variant_base<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>’
/usr/include/c++/9/variant:1219:11: required from ‘class std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>’
/usr/include/c++/9/bits/stl_pair.h:215:11: [ skipping 2 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/c++/9/bits/hashtable_policy.h:233:43: required from ‘struct std::__detail::_Hash_node_value_base<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> > >’
/usr/include/c++/9/bits/hashtable_policy.h:264:12: required from ‘struct std::__detail::_Hash_node<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, true>’
/usr/include/c++/9/bits/hashtable_policy.h:2027:13: required from ‘struct std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, true> > >’
/usr/include/c++/9/bits/hashtable.h:173:11: required from ‘class std::_Hashtable<std::basic_string<char>, std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, std::allocator<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> > >, std::__detail::_Select1st, std::equal_to<std::basic_string<char> >, std::hash<std::basic_string<char> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >’
/usr/include/c++/9/bits/unordered_map.h:105:18: required from ‘class std::unordered_map<std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >’
json20.hpp:18:48: required from here
/usr/include/c++/9/type_traits:2959:25: error: ‘value’ is not a member of ‘std::is_trivially_destructible<json_null>’
2959 | inline constexpr bool is_trivially_destructible_v =
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/9/type_traits: In instantiation of ‘struct std::__is_trivially_move_constructible_impl<json_object, true>’:
/usr/include/c++/9/type_traits:1185:12: required from ‘struct std::is_trivially_move_constructible<json_object>’
/usr/include/c++/9/type_traits:2947:25: required from ‘constexpr const bool std::is_trivially_move_constructible_v<json_object>’
/usr/include/c++/9/variant:290:5: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_move_ctor’
/usr/include/c++/9/variant:295:23: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_move_assign’
/usr/include/c++/9/variant:677:11: required by substitution of ‘template<class ... _Types>
Something interesting is that if I replace std::unordered_map with std::map then it compiles ok. Why?

Is there any way to insert a unique_ptr into a map in C++0x/gcc 4.4.7)?

I can't figure out how to do it; it's driving me nuts.
#include <iostream>
#include <memory>
#include <map>
int main()
{
std::map<std::string, std::unique_ptr<std::string>> map;
std::unique_ptr<std::string> bar(new std::string("bar"));
map["foo"] = std::move(bar);
std::cout << "foo: " << *(map["foo"]) << std::endl;
}
This compiles just fine on gcc v4.9.2:
$ g++ -std=c++0x test.cc -o test
Unfortunately, all I have available to me is gcc v4.4.7, which produces a hideous error message that I'll stick at the bottom.
I can push_back unique_ptrs into vectors just fine; I don't understand why there's an issue with map.
I've also tried using insert:
int main()
{
std::map<std::string, std::unique_ptr<std::string>> map;
std::unique_ptr<std::string> bar(new std::string("bar"));
auto pair = std::make_pair("foo", std::move(bar));
map.insert(std::move(pair));
std::cout << "foo: " << *(map["foo"]) << std::endl;
}
I can make the pair just fine, but when I try to move it into insert I get the error. It appears that it's trying to call the copy constructor for some reason, but I don't understand why, since I'm moveing it.
emplace has the same result:
int main()
{
std::map<std::string, std::unique_ptr<std::string>> map;
std::unique_ptr<std::string> bar(new std::string("bar"));
auto pair = std::make_pair("foo", std::move(bar));
map.emplace(std::move(pair));
std::cout << "foo: " << *(map["foo"]) << std::endl;
}
Both of these compile fine in g++ v4.9.2, but not in v4.4.7.
Anyone have any ideas on an alternative way to do this (in v4.4.7)?
Full error message output (from the first example):
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:66,
from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/char_traits.h:41,
from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ios:41,
from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:40,
from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iostream:40,
from test.cc:1:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/unique_ptr.h: In copy constructor 'std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >::pair(const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&)':
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_pair.h:68: instantiated from 'std::_Rb_tree_node<_Val>::_Rb_tree_node(_Args&& ...) [with _Args = const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >]'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ext/new_allocator.h:111: instantiated from 'void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, _Args&& ...) [with _Args = const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&, _Tp = std::_Rb_tree_node<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >]'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_tree.h:394: instantiated from 'std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&, _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >]'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_tree.h:881: instantiated from 'std::_Rb_tree_iterator<_Val> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_(const std::_Rb_tree_node_base*, const std::_Rb_tree_node_base*, const _Val&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >]'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_tree.h:1215: instantiated from 'std::_Rb_tree_iterator<_Val> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique_(std::_Rb_tree_const_iterator<_Val>, const _Val&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >]'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_map.h:540: instantiated from 'typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::insert(typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator, const std::pair<const _Key, _Tp>&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >]'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_map.h:450: instantiated from '_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >]'
test.cc:9: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/unique_ptr.h:214: error: deleted function 'std::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp_Deleter = std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_pair.h:68: error: used here
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/map:60,
from test.cc:3:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_tree.h: In constructor 'std::_Rb_tree_node<_Val>::_Rb_tree_node(_Args&& ...) [with _Args = const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >]':
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_tree.h:136: note: synthesized method 'std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >::pair(const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&)' first required here
GCC 4.4.7 does not provide full support for C++ 11 features, so it is unrealistic to expect all C++ 11 features to work properly. In this particular case, the compiler is not generating move assignment and move copy methods for you. So, when std::map attempts assignment or copy, it uses the regular default ones, which are deleted for unique_ptr.
If you just need a smart pointer, you can use shared_ptr instead.
std::map<std::string, std::shared_ptr<std::string>> map;
std::shared_ptr<std::string> bar = std::make_shared<std::string>("bar");
map["foo"] = bar;
I have looked into the issue. STL implementation for map correctly has a move constructor defined in it:
#ifdef __GXX_EXPERIMENTAL_CXX0X__
template<typename... _Args>
_Rb_tree_node(_Args&&... __args)
: _Rb_tree_node_base(),
_M_value_field(std::forward<_Args>(__args)...) { }
#endif
};
Moreover, this is called, as stated in the error output. std::pair has move constructor defined as well, but this one is not called.
I believe, std::forward and deduced type of Args is at fault:
usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_tree.h:
In constructor ‘std::_Rb_tree_node<_Val>::_Rb_tree_node(_Args&& ...)
[with _Args = const std::pair<..., std::unique_ptr<...> >&,
As you see, type of _Args is a const reference, which is forwarded by forward, and regular pair constructor is called.
I assume, one might fiddle with stl_tree.h to fix it - for example, by removing std::forward and just casting the arg to rvalue reference. This would not be correct, of course, but might work for specific case.

error: '__pred' cannot be used as a function

I can't find what I'm doing wrong here. The function eta does what I ask but when I use it in the loop I get the attached error.
bool eta(map<string, TLorentzVector> map_jets, string jet){
return( fabs(map_jets[jet].PseudoRapidity()) > 2.5 );
}
and then
vector<pair<string,double> > jets_pt( vec_jets.size() );
for( vector<pair<string,double> >::iterator it = jets_pt.begin(); it != jets_pt.end(); ++it)
jets_pt.erase(remove_if(jets_pt.begin(),jets_pt.end(),eta(map_jets,it1->first)),jets_pt.end);
I get the error
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h: In function '_OutputIterator std::remove_copy_if(_InputIterator, _InputIterator, _OutputIterator, _Predicate) [with _InputIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _OutputIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]':
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:1291: instantiated from '_ForwardIterator std::remove_if(_ForwardIterator, _ForwardIterator, _Predicate) [with _ForwardIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]'
/misc/cdf/gbertoli/hww/Diboson_v20_taus/Ana/src/Functions.cc:25: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:1216: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h: In function '_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]':
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:338: instantiated from '_InputIterator std::find_if(_InputIterator, _InputIterator, _Predicate) [with _InputIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:1287: instantiated from '_ForwardIterator std::remove_if(_ForwardIterator, _ForwardIterator, _Predicate) [with _ForwardIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]'
/misc/cdf/gbertoli/hww/Diboson_v20_taus/Ana/src/Functions.cc:25: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:260: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:264: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:268: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:272: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:280: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:284: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:288: error: '__pred' cannot be used as a function
mv: cannot stat `/misc/cdf/gbertoli/hww/Diboson_v20_taus/tmp/Linux2.6-GCC_4_1/Ana/srt_dep_tmp.27294': No such file or directory
gmake[2]: *** [/misc/cdf/gbertoli/hww/Diboson_v20_taus/tmp/Linux2.6-GCC_4_1/Ana/libAna-shared/Functions.o] Error 1
gmake[1]: *** [src.lib] Error 2
gmake: *** [Ana.all] Error 2
First off, the only valid signature for remove_if takes a single function as the predicate argument:
jets_pt.erase(std::remove_if(jets_pt.begin(), jets_pt.end(), eta),
jets_pt.end());
This means that eta must be a function returning bool and taking precisely one argument whose type is the value type of the container:
bool eta(const std::pair<string, double> & p)
{
// do something useful with p
}
If this doesn't fit your bill because you need additional state information in the predicate, then you need to make it a function object:
struct Eta
{
const std::map<string, TLorentzVector> & map_jets;
Eta(const std::map<string, TLorentzVector> & m) : map_jets(m) { }
bool operator()(const std::pair<string, double> & p) const
{
std::map<string, TLorentzVector>::const_iterator it = map_jets.find(p->second);
return it == map_jets.end() ?
false :
std::fabs(it->second.PseudoRapidity()) > 2.5;
}
};
Now you have to use remove_if with an instance of Eta:
jets_pt.erase(std::remove_if(jets_pt.begin(), jets_pt.end(), Eta(map_jets)),
jets_pt.end()); // ^^^^^^^^^^^^^
Note that your use of the for loop is extremely suspicious; you should double-check that.
std::remove_if takes a value of the contained type (bool pred (container::value_type), or a functor which overloads operator() appropriately).
For std::map, you have to do it like explained here remove_if equivalent for std::map .
You're passing a bool (the results of calling eta) to remove_if as
the third argument. You need to pass it a predicate: a pointer to a
function taking a single argument and returning something which can be
converted to a bool, or a functional object. In the error messages,
__pred is your third argument; remove_if calls it here with a
pair<string, double>, and expects a bool in return. You have to
provide something which can be called like this. (You might want to
look into boost::bind.)
And BTW: passing a map by value to eta might not be a good idea.
It's going to slow things down considerably if the map is big.

C++ Deep Copying a pointer to a map with pointer values

I am having a bit of trouble deep copying a pointer to a map of pointer values.
std::map<string, TH1D*>* m_hist_split;
My current method:
I am using a class template that copies the map pair and initializes the value of the pair on the heap. the template then returns a map::type_value
#ifndef DEEPCOPY_H
#define DEEPCOPY_H
#include <map>
#include <algorithm>
namespace DeepCopy{
template<class A, class B>
struct MapPointerValue{
typedef typename std::map< A, B* >::value_type map_t;
map_t operator() (map_t p){
return std::make_pair(p.first, new B(*p.second) );
}
};
...
...
}
#endif
Inside my copy constructor I then attempt to deep copy my pointer to a map of pointer values.
Cutflow::Cutflow(const Cutflow& cpy){
m_hist_split = new map<string, TH1D*>;
transform(cpy.m_hist_split->begin(),cpy.m_hist_split->end(), inserter(m_hist_split, m_hist_split->begin()), DeepCopy::MapPointerValue<string, TH1D>() );
...
...
}
When compiling I then get an ugly error :-( :
Compiling src/Cutflow.cxx to obj/Cutflow.o
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_iterator.h: In instantiation of 'std::insert_iterator<std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*> > >*>':
src/Cutflow.cxx:42: instantiated from here
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_iterator.h:562: error: 'std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*> > >*' is not a class, struct, or union type
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_iterator.h:572: error: 'std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*> > >*' is not a class, struct, or union type
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_iterator.h:599: error: 'std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*> > >*' is not a class, struct, or union type
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_iterator.h:608: error: 'std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*> > >*' is not a class, struct, or union type
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_iterator.h: In function 'std::insert_iterator<_Container> std::inserter(_Container&, _Iterator) [with _Container = std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*> > >*, _Iterator = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*> >]':
src/Cutflow.cxx:42: instantiated from here
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_iterator.h:648: error: 'std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*> > >*' is not a class, struct, or union type
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_algo.h: In function '_OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation) [with _IIter = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*> >, _OIter = std::insert_iterator<std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*> > >*>, _UnaryOperation = DeepCopy::MapPointerValue<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D>]':
src/Cutflow.cxx:42: instantiated from here
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_algo.h:4281: error: no match for 'operator=' in '__result.std::insert_iterator<_Container>::operator* [with _Container = std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*> > >*]() = DeepCopy::MapPointerValue<A, B>::operator()(typename std::map<A, B*, std::less<_Key>, std::allocator<std::pair<const A, B*> > >::value_type) [with A = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, B = TH1D](std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*>(((const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*>&)((const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*>*)__first.std::_Rb_tree_iterator<_Tp>::operator* [with _Tp = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*>]()))))'
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_iterator.h:559: note: candidates are: std::insert_iterator<std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*> > >*>& std::insert_iterator<std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*> > >*>::operator=(const std::insert_iterator<std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*> > >*>&)
Any ideas what I am doing wrong.
Also,
is what I am doing correct?
:-D
In the following line
transform(cpy.m_hist_split->begin(),cpy.m_hist_split->end(), inserter(m_hist_split, m_hist_split->begin()), DeepCopy::MapPointerValue<string, TH1D>() );
you have mistakenly called the inserter function with a pointer to a map as the first parameter instead of a reference.
For completeness, inserter is defined in §24.5.2.6.5 like this:
template <class Container>
insert_iterator<Container> inserter(Container& x, typename Container::iterator i);
Returns: insert_iterator(x, i)
In this case, a simple typo caused a big headache.