How to precompile CGAL headers to speed up (Ubuntu) - c++

I was launching one of the examples offered by CGAL on their website, and I realized that the compilation time is very high, so I saw that one possibility was to generate a .h file including all the headers that are being used and precompile them.
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Point_set_3.h>
#include <CGAL/Point_set_3/IO.h>
#include <CGAL/remove_outliers.h>
#include <CGAL/grid_simplify_point_set.h>
#include <CGAL/jet_smooth_point_set.h>
#include <CGAL/jet_estimate_normals.h>
#include <CGAL/mst_orient_normals.h>
#include <CGAL/poisson_surface_reconstruction.h>
#include <CGAL/Advancing_front_surface_reconstruction.h>
#include <CGAL/Scale_space_surface_reconstruction_3.h>
#include <CGAL/Scale_space_reconstruction_3/Jet_smoother.h>
#include <CGAL/Scale_space_reconstruction_3/Advancing_front_mesher.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Polygon_mesh_processing/polygon_soup_to_polygon_mesh.h>
And after creating the MyHeaders.h I try to compile it using:
g++ -std=c++14 main.cpp -lCGAL -lboost_system -lboost_date_time -lboost_thread -lgmp -lmpfr -L/usr/local/include
But errors of many kinds are coming out. This example, as far as I know, uses CGAL, BOOST, GMP and MPFR.
Does someone know how can I solve this problem? I'm using Ubuntu 20.04 with CGAL 5.4
The errors:
/usr/local/include/CGAL/poisson_surface_reconstruction.h: In instantiation of ‘bool CGAL::poisson_surface_reconstruction_delaunay(PointInputIterator, PointInputIterator, PointMap, NormalMap, PolygonMesh&, double, double, double, double, Tag) [with PointInputIterator = __gnu_cxx::__normal_iterator<CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >::Index*, std::vector<CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >::Index, std::allocator<CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >::Index> > >; PointMap = CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >::Property_map<CGAL::Point_3<CGAL::Epick> >; NormalMap = CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >::Property_map<CGAL::Vector_3<CGAL::Epick> >; PolygonMesh = CGAL::Surface_mesh<CGAL::Point_3<CGAL::Epick> >; Tag = CGAL::Manifold_with_boundary_tag]’:
main.cpp:99:38: required from here
/usr/local/include/CGAL/poisson_surface_reconstruction.h:105:10: error: no matching function for call to ‘CGAL::Poisson_reconstruction_function<CGAL::Epick>::compute_implicit_function()’
105 | if ( ! function.compute_implicit_function() )
In file included from /usr/local/include/CGAL/poisson_surface_reconstruction.h:21,
from main.cpp:9:
/usr/local/include/CGAL/Poisson_reconstruction_function.h:417:8: note: candidate: ‘template<class SparseLinearAlgebraTraits_d, class Visitor> bool CGAL::Poisson_reconstruction_function<Gt>::compute_implicit_function(SparseLinearAlgebraTraits_d, Visitor, double, double) [with SparseLinearAlgebraTraits_d = SparseLinearAlgebraTraits_d; Visitor = Visitor; Gt = CGAL::Epick]’
417 | bool compute_implicit_function(
| ^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/CGAL/Poisson_reconstruction_function.h:417:8: note: template argument deduction/substitution failed:
In file included from main.cpp:9:
/usr/local/include/CGAL/poisson_surface_reconstruction.h:105:10: note: candidate expects 4 arguments, 0 provided
105 | if ( ! function.compute_implicit_function() )
In file included from /usr/local/include/CGAL/poisson_surface_reconstruction.h:21,
from main.cpp:9:
/usr/local/include/CGAL/Poisson_reconstruction_function.h:571:8: note: candidate: ‘template<class SparseLinearAlgebraTraits_d> bool CGAL::Poisson_reconstruction_function<Gt>::compute_implicit_function(SparseLinearAlgebraTraits_d, bool) [with SparseLinearAlgebraTraits_d = SparseLinearAlgebraTraits_d; Gt = CGAL::Epick]’
571 | bool compute_implicit_function(SparseLinearAlgebraTraits_d solver, bool smoother_hole_filling = false)
| ^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/CGAL/Poisson_reconstruction_function.h:571:8: note: template argument deduction/substitution failed:
In file included from main.cpp:9:
/usr/local/include/CGAL/poisson_surface_reconstruction.h:105:10: note: candidate expects 2 arguments, 0 provided
105 | if ( ! function.compute_implicit_function() )
In file included from /usr/local/include/CGAL/basic.h:26,
from /usr/local/include/CGAL/Cartesian/Cartesian_base.h:20,
from /usr/local/include/CGAL/Simple_cartesian.h:20,
from /usr/local/include/CGAL/Exact_predicates_inexact_constructions_kernel.h:20,
from main.cpp:1:
/usr/local/include/CGAL/jet_smooth_point_set.h: In instantiation of ‘void CGAL::jet_smooth_point_set(PointRange&, unsigned int, const NamedParameters&) [with ConcurrencyTag = CGAL::Sequential_tag; PointRange = CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >; NamedParameters = CGAL::Named_function_parameters<CGAL::Epick, CGAL::internal_np::geom_traits_t, CGAL::Named_function_parameters<CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >::Property_map<CGAL::Vector_3<CGAL::Epick> >, CGAL::internal_np::normal_t, CGAL::Named_function_parameters<CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >::Property_map<CGAL::Point_3<CGAL::Epick> >, CGAL::internal_np::point_t, CGAL::internal_np::No_property> > >]’:
/usr/local/include/CGAL/jet_smooth_point_set.h:285:5: required from ‘void CGAL::jet_smooth_point_set(PointRange&, unsigned int) [with ConcurrencyTag = CGAL::Sequential_tag; PointRange = CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >]’
main.cpp:86:65: required from here
/usr/local/include/CGAL/jet_smooth_point_set.h:202:3: error: static assertion failed: Error: no SVD traits
202 | CGAL_static_assertion_msg(!(boost::is_same<SvdTraits,
| ^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/CGAL/jet_smooth_point_set.h: In instantiation of ‘void CGAL::jet_smooth_point_set(PointRange&, unsigned int, const NamedParameters&) [with ConcurrencyTag = CGAL::Sequential_tag; PointRange = CGAL::Iterator_range<__gnu_cxx::__normal_iterator<CGAL::Point_3<CGAL::Epick>*, std::vector<CGAL::Point_3<CGAL::Epick>, std::allocator<CGAL::Point_3<CGAL::Epick> > > > >; NamedParameters = CGAL::Named_function_parameters<unsigned int, CGAL::internal_np::degree_monge_t, CGAL::Named_function_parameters<unsigned int, CGAL::internal_np::degree_fitting_t, CGAL::internal_np::No_property> >]’:
/usr/local/include/CGAL/Scale_space_reconstruction_3/Jet_smoother.h:83:5: required from ‘void CGAL::Scale_space_reconstruction_3::Jet_smoother<Geom_traits, ConcurrencyTag>::operator()(InputIterator, InputIterator) [with InputIterator = __gnu_cxx::__normal_iterator<CGAL::Point_3<CGAL::Epick>*, std::vector<CGAL::Point_3<CGAL::Epick>, std::allocator<CGAL::Point_3<CGAL::Epick> > > >; Geom_traits = CGAL::Epick; ConcurrencyTag = CGAL::Sequential_tag]’
/usr/local/include/CGAL/Scale_space_surface_reconstruction_3.h:180:39: required from ‘void CGAL::Scale_space_surface_reconstruction_3<Geom_traits>::increase_scale(std::size_t, const Smoother&) [with Smoother = CGAL::Scale_space_reconstruction_3::Jet_smoother<CGAL::Epick>; Geom_traits = CGAL::Epick; std::size_t = long unsigned int]’
main.cpp:136:98: required from here
/usr/local/include/CGAL/jet_smooth_point_set.h:202:3: error: static assertion failed: Error: no SVD traits
In file included from /usr/include/c++/9/functional:59,
from /usr/local/include/CGAL/config.h:431,
from /usr/local/include/CGAL/basic.h:21,
from /usr/local/include/CGAL/Cartesian/Cartesian_base.h:20,
from /usr/local/include/CGAL/Simple_cartesian.h:20,
from /usr/local/include/CGAL/Exact_predicates_inexact_constructions_kernel.h:20,
from main.cpp:1:
/usr/include/c++/9/bits/std_function.h:667:7: error: ‘std::function<_Res(_ArgTypes ...)>::function(_Functor) [with _Functor = CGAL::jet_smooth_point_set(PointRange&, unsigned int, const NamedParameters&) [with ConcurrencyTag = CGAL::Sequential_tag; PointRange = CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >; NamedParameters = CGAL::Named_function_parameters<CGAL::Epick, CGAL::internal_np::geom_traits_t, CGAL::Named_function_parameters<CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >::Property_map<CGAL::Vector_3<CGAL::Epick> >, CGAL::internal_np::normal_t, CGAL::Named_function_parameters<CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >::Property_map<CGAL::Point_3<CGAL::Epick> >, CGAL::internal_np::point_t, CGAL::internal_np::No_property> > >]::<lambda(const reference&)>; <template-parameter-2-2> = void; <template-parameter-2-3> = void; _Res = bool; _ArgTypes = {boost::tuples::cons<CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >::Index&, boost::tuples::cons<CGAL::Point_3<CGAL::Epick>&, boost::tuples::null_type> >}]’, declared using local type ‘CGAL::jet_smooth_point_set(PointRange&, unsigned int, const NamedParameters&) [with ConcurrencyTag = CGAL::Sequential_tag; PointRange = CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >; NamedParameters = CGAL::Named_function_parameters<CGAL::Epick, CGAL::internal_np::geom_traits_t, CGAL::Named_function_parameters<CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >::Property_map<CGAL::Vector_3<CGAL::Epick> >, CGAL::internal_np::normal_t, CGAL::Named_function_parameters<CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >::Property_map<CGAL::Point_3<CGAL::Epick> >, CGAL::internal_np::point_t, CGAL::internal_np::No_property> > >]::<lambda(const reference&)>’, is used but never defined [-fpermissive]
667 | function<_Res(_ArgTypes...)>::
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/9/bits/std_function.h:667:7: error: ‘std::function<_Res(_ArgTypes ...)>::function(_Functor) [with _Functor = CGAL::jet_smooth_point_set(PointRange&, unsigned int, const NamedParameters&) [with ConcurrencyTag = CGAL::Sequential_tag; PointRange = CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >; NamedParameters = CGAL::Named_function_parameters<CGAL::Epick, CGAL::internal_np::geom_traits_t, CGAL::Named_function_parameters<CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >::Property_map<CGAL::Vector_3<CGAL::Epick> >, CGAL::internal_np::normal_t, CGAL::Named_function_parameters<CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >::Property_map<CGAL::Point_3<CGAL::Epick> >, CGAL::internal_np::point_t, CGAL::internal_np::No_property> > >]::<lambda(const reference&)>; <template-parameter-2-2> = void; <template-parameter-2-3> = void; _Res = bool; _ArgTypes = {boost::tuples::cons<CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >::Index&, boost::tuples::cons<CGAL::Point_3<CGAL::Epick>&, boost::tuples::null_type> >}]’, declared using local type ‘CGAL::jet_smooth_point_set(PointRange&, unsigned int, const NamedParameters&) [with ConcurrencyTag = CGAL::Sequential_tag; PointRange = CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >; NamedParameters = CGAL::Named_function_parameters<CGAL::Epick, CGAL::internal_np::geom_traits_t, CGAL::Named_function_parameters<CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >::Property_map<CGAL::Vector_3<CGAL::Epick> >, CGAL::internal_np::normal_t, CGAL::Named_function_parameters<CGAL::Point_set_3<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> >::Property_map<CGAL::Point_3<CGAL::Epick> >, CGAL::internal_np::point_t, CGAL::internal_np::No_property> > >]::<lambda(const reference&)>’, is used but never defined [-fpermissive]
/usr/include/c++/9/bits/std_function.h:667:7: error: ‘std::function<_Res(_ArgTypes ...)>::function(_Functor) [with _Functor = CGAL::jet_smooth_point_set(PointRange&, unsigned int, const NamedParameters&) [with ConcurrencyTag = CGAL::Sequential_tag; PointRange = CGAL::Iterator_range<__gnu_cxx::__normal_iterator<CGAL::Point_3<CGAL::Epick>*, std::vector<CGAL::Point_3<CGAL::Epick>, std::allocator<CGAL::Point_3<CGAL::Epick> > > > >; NamedParameters = CGAL::Named_function_parameters<unsigned int, CGAL::internal_np::degree_monge_t, CGAL::Named_function_parameters<unsigned int, CGAL::internal_np::degree_fitting_t, CGAL::internal_np::No_property> >]::<lambda(const reference&)>; <template-parameter-2-2> = void; <template-parameter-2-3> = void; _Res = bool; _ArgTypes = {boost::tuples::cons<CGAL::Point_3<CGAL::Epick>&, boost::tuples::cons<CGAL::Point_3<CGAL::Epick>&, boost::tuples::null_type> >}]’, declared using local type ‘CGAL::jet_smooth_point_set(PointRange&, unsigned int, const NamedParameters&) [with ConcurrencyTag = CGAL::Sequential_tag; PointRange = CGAL::Iterator_range<__gnu_cxx::__normal_iterator<CGAL::Point_3<CGAL::Epick>*, std::vector<CGAL::Point_3<CGAL::Epick>, std::allocator<CGAL::Point_3<CGAL::Epick> > > > >; NamedParameters = CGAL::Named_function_parameters<unsigned int, CGAL::internal_np::degree_monge_t, CGAL::Named_function_parameters<unsigned int, CGAL::internal_np::degree_fitting_t, CGAL::internal_np::No_property> >]::<lambda(const reference&)>’, is used but never defined [-fpermissive]
/usr/include/c++/9/bits/std_function.h:667:7: error: ‘std::function<_Res(_ArgTypes ...)>::function(_Functor) [with _Functor = CGAL::jet_smooth_point_set(PointRange&, unsigned int, const NamedParameters&) [with ConcurrencyTag = CGAL::Sequential_tag; PointRange = CGAL::Iterator_range<__gnu_cxx::__normal_iterator<CGAL::Point_3<CGAL::Epick>*, std::vector<CGAL::Point_3<CGAL::Epick>, std::allocator<CGAL::Point_3<CGAL::Epick> > > > >; NamedParameters = CGAL::Named_function_parameters<unsigned int, CGAL::internal_np::degree_monge_t, CGAL::Named_function_parameters<unsigned int, CGAL::internal_np::degree_fitting_t, CGAL::internal_np::No_property> >]::<lambda(const reference&)>; <template-parameter-2-2> = void; <template-parameter-2-3> = void; _Res = bool; _ArgTypes = {boost::tuples::cons<CGAL::Point_3<CGAL::Epick>&, boost::tuples::cons<CGAL::Point_3<CGAL::Epick>&, boost::tuples::null_type> >}]’, declared using local type ‘CGAL::jet_smooth_point_set(PointRange&, unsigned int, const NamedParameters&) [with ConcurrencyTag = CGAL::Sequential_tag; PointRange = CGAL::Iterator_range<__gnu_cxx::__normal_iterator<CGAL::Point_3<CGAL::Epick>*, std::vector<CGAL::Point_3<CGAL::Epick>, std::allocator<CGAL::Point_3<CGAL::Epick> > > > >; NamedParameters = CGAL::Named_function_parameters<unsigned int, CGAL::internal_np::degree_monge_t, CGAL::Named_function_parameters<unsigned int, CGAL::internal_np::degree_fitting_t, CGAL::internal_np::No_property> >]::<lambda(const reference&)>’, is used but never defined [-fpermissive]
Edit: Add CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(CGAL_V0)
set(Qt5_DIR "/opt/Qt/5.15.2/gcc_64/lib/cmake/Qt5/")
set(CGAL_DATA_DIR "/usr/include/CGAL")
set(CGAL_DO_NOT_WARN_ABOUT_CMAKE_BUILD_TYPE TRUE)
find_package(CGAL REQUIRED COMPONENTS Core)
find_package(GMP REQUIRED)
message(STATUS "GMP Found: ${GMP_FOUND}")
message(STATUS "GMP Libs: ${GMP_LIBRARIES}")
find_package(MPFR REQUIRED)
message(STATUS "MPFR Found: ${MPFR_FOUND}")
message(STATUS "MPFR Libs: ${MPFR_LIBRARIES}")
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIR})
message(STATUS "Eigen3 Found: ${EIGEN3_FOUND}")
message(STATUS "Eigen3 DIR: ${EIGEN3_INCLUDE_DIR}")
set(CMAKE_CXX_STANDARD 14)
add_executable(CGAL_V0 main.cpp)
target_link_libraries(CGAL_V0 CGAL::CGAL CGAL::CGAL_Core ${MPFR_LIBRARIES} ${GMP_LIBRARIES})

Related

warning: operator delete called on unallocated object

I'm trying to compile the following code (this is a minimal example), but I get a warning I can't figure out:
#include <string>
#include <variant>
#include <vector>
struct Bar {
std::wstring x = L"";
};
Bar Foo() {
std::variant<std::vector<int>, Bar> tmp = Bar();
if (std::holds_alternative<Bar>(tmp)) return std::move(std::get<Bar>(tmp));
return Bar();
}
I'm trying to build this with g++ -std=c++20 -Wall -Wextra -O2 /tmp/test.cc -c
I get the following warning:
In file included from /usr/include/x86_64-linux-gnu/c++/12/bits/c++allocator.h:33,
from /usr/include/c++/12/bits/allocator.h:46,
from /usr/include/c++/12/string:41,
from /tmp/test.cc:1:
In member function ‘void std::__new_allocator<_Tp>::deallocate(_Tp*, size_type) [with _Tp = int]’,
inlined from ‘constexpr void std::allocator< <template-parameter-1-1> >::deallocate(_Tp*, std::size_t) [with _Tp = int]’ at /usr/include/c++/12/bits/allocator.h:200:35,
inlined from ‘static constexpr void std::allocator_traits<std::allocator<_CharT> >::deallocate(allocator_type&, pointer, size_type) [with _Tp = int]’ at /usr/include/c++/12/bits/alloc_traits.h:496:23,
inlined from ‘constexpr void std::_Vector_base<_Tp, _Alloc>::_M_deallocate(pointer, std::size_t) [with _Tp = int; _Alloc = std::allocator<int>]’ at /usr/include/c++/12/bits/stl_vector.h:387:19,
inlined from ‘constexpr std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = int; _Alloc = std::allocator<int>]’ at /usr/include/c++/12/bits/stl_vector.h:366:15,
inlined from ‘constexpr std::vector<_Tp, _Alloc>::~vector() [with _Tp = int; _Alloc = std::allocator<int>]’ at /usr/include/c++/12/bits/stl_vector.h:733:7,
inlined from ‘constexpr void std::destroy_at(_Tp*) [with _Tp = vector<int>]’ at /usr/include/c++/12/bits/stl_construct.h:88:18,
inlined from ‘constexpr void std::_Destroy(_Tp*) [with _Tp = vector<int>]’ at /usr/include/c++/12/bits/stl_construct.h:149:22,
inlined from ‘std::__detail::__variant::_Variant_storage<false, std::vector<int, std::allocator<int> >, Bar>::_M_reset()::<lambda(auto:11&&)> mutable [with auto:11 = std::vector<int>&]’ at /usr/include/c++/12/variant:472:19,
inlined from ‘constexpr _Res std::__invoke_impl(__invoke_other, _Fn&&, _Args&& ...) [with _Res = void; _Fn = __detail::__variant::_Variant_storage<false, vector<int, allocator<int> >, Bar>::_M_reset()::<lambda(auto:11&&)>; _Args = {vector<int, allocator<int> >&}]’ at /usr/include/c++/12/bits/invoke.h:61:36,
inlined from ‘constexpr std::enable_if_t<is_invocable_r_v<_Res, _Callable, _Args ...>, _Res> std::__invoke_r(_Callable&&, _Args&& ...) [with _Res = void; _Callable = __detail::__variant::_Variant_storage<false, vector<int, allocator<int> >, Bar>::_M_reset()::<lambda(auto:11&&)>; _Args = {vector<int, allocator<int> >&}]’ at /usr/include/c++/12/bits/invoke.h:111:28,
inlined from ‘static constexpr decltype(auto) std::__detail::__variant::__gen_vtable_impl<std::__detail::__variant::_Multi_array<_Result_type (*)(_Visitor, _Variants ...)>, std::integer_sequence<long unsigned int, __indices ...> >::__visit_invoke(_Visitor&&, _Variants ...) [with _Result_type = void; _Visitor = std::__detail::__variant::_Variant_storage<false, std::vector<int, std::allocator<int> >, Bar>::_M_reset()::<lambda(auto:11&&)>&&; _Variants = {std::variant<std::vector<int, std::allocator<int> >, Bar>&}; long unsigned int ...__indices = {0}]’ at /usr/include/c++/12/variant:1035:40,
inlined from ‘constexpr decltype(auto) std::__do_visit(_Visitor&&, _Variants&& ...) [with _Result_type = void; _Visitor = __detail::__variant::_Variant_storage<false, vector<int, allocator<int> >, Bar>::_M_reset()::<lambda(auto:11&&)>; _Variants = {variant<vector<int, allocator<int> >, Bar>&}]’ at /usr/include/c++/12/variant:1783:5,
inlined from ‘constexpr void std::__detail::__variant::_Variant_storage<false, _Types ...>::_M_reset() [with _Types = {std::vector<int, std::allocator<int> >, Bar}]’ at /usr/include/c++/12/variant:470:23,
inlined from ‘constexpr std::__detail::__variant::_Variant_storage<false, _Types ...>::~_Variant_storage() [with _Types = {std::vector<int, std::allocator<int> >, Bar}]’ at /usr/include/c++/12/variant:480:17,
inlined from ‘constexpr std::__detail::__variant::_Copy_ctor_base<false, std::vector<int, std::allocator<int> >, Bar>::~_Copy_ctor_base()’ at /usr/include/c++/12/variant:554:12,
inlined from ‘constexpr std::__detail::__variant::_Move_ctor_base<false, std::vector<int, std::allocator<int> >, Bar>::~_Move_ctor_base()’ at /usr/include/c++/12/variant:591:12,
inlined from ‘constexpr std::__detail::__variant::_Copy_assign_base<false, std::vector<int, std::allocator<int> >, Bar>::~_Copy_assign_base()’ at /usr/include/c++/12/variant:629:12,
inlined from ‘constexpr std::__detail::__variant::_Move_assign_base<false, std::vector<int, std::allocator<int> >, Bar>::~_Move_assign_base()’ at /usr/include/c++/12/variant:681:12,
inlined from ‘constexpr std::__detail::__variant::_Variant_base<std::vector<int, std::allocator<int> >, Bar>::~_Variant_base()’ at /usr/include/c++/12/variant:735:12,
inlined from ‘constexpr std::variant<_Types>::~variant() [with _Types = {std::vector<int, std::allocator<int> >, Bar}]’ at /usr/include/c++/12/variant:1407:28,
inlined from ‘Bar Foo()’ at /tmp/test.cc:13:1:
/usr/include/c++/12/bits/new_allocator.h:158:33: warning: ‘void operator delete(void*, std::size_t)’ called on unallocated object ‘tmp’ [-Wfree-nonheap-object]
158 | _GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p, __n));
| ^
/tmp/test.cc: In function ‘Bar Foo()’:
/tmp/test.cc:10:39: note: declared here
10 | std::variant<std::vector<int>, Bar> tmp = Bar();
| ^~~
g++ --version says g++ (Debian 12.2.0-10) 12.2.0
I know this code, as is, doesn't make that much sense; but it also shouldn't have this problem: this is a minimal example that exhibits this warning, of a much more complex function.
Strangely, the warning disappears if I do any of the following:
Change Bar::x to be a std::string.
Change tmp to be std::variant<int, Bar> (rather than having a std::vector<int> as the first type).
Remove g++ command-line flag -O2 (i.e., compile with g++ -std=c++20 -Wall -Wextra -O2 /tmp/test.cc -c).
Why would any of these changes make the warning go away!?
If I build this with --std=c++17, the problem remains.
The -Wfree-nonheap-object flag of gcc has a series of false positives bugs, among which bug 99098 is used to record these meta-bugs.
The bug in your example is similar to bug 108088 and has been listed, which issues a false positive warning message since gcc-12.

How to make unique_ptr work with Qt's QHash and QString?

I am learning the intrincacies of unique_ptr.
I have a working example of a std::unordered_map that holds std:string and std::unique_ptr<std::string>.
But I can't make unique_ptr examples with Qt's QString and QHash work. They won't compile mentioning errors like use of deleted function.
How can I make the two last examples work?
Edit 1
In case this info is relevant, I am using gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0.
Example 1 (working): unordered_map and string
unordered_map_string.h
#ifndef UNORDERED_MAP_STRING_H
#define UNORDERED_MAP_STRING_H
#include <memory>
#include <string>
#include <unordered_map>
class UnorderedMapString
{
public:
UnorderedMapString() {};
std::unordered_map<std::string, std::unique_ptr<std::string>> container;
void addItem(std::string aKey, std::unique_ptr<std::string> aValue);
void showItem(std::string aKey);
};
#endif // UNORDERED_MAP_STRING_H
unordered_map_string.cpp
#include "unordered_map_string.h"
#include <iostream>
void UnorderedMapString::addItem(std::string aKey, std::unique_ptr<std::string> aValue)
{
container[aKey] = std::move(aValue);
}
void UnorderedMapString::showItem(std::string aKey)
{
std::string *aValue = container[aKey].get();
std::cout << "Item '" << aKey << "': '" << *aValue << "'\n";
}
main.cpp
#include "unordered_map_string.h"
#include <memory>
int main(int argc, char *argv[])
{
UnorderedMapString testContainer;
testContainer.addItem("key 1", std::make_unique<std::string>("one value"));
testContainer.addItem("key 2", std::make_unique<std::string>("two value"));
testContainer.addItem("key 3", std::make_unique<std::string>("three value"));
testContainer.showItem("key 2");
testContainer.showItem("key 3");
testContainer.showItem("key 2");
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(unique_ptr_string LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(unique_ptr_string
main.cpp
unordered_map_string.h unordered_map_string.cpp
)
Example 2 (not working): unordered_map and QString
unordered_map_qstring.h
#ifndef UNORDERED_MAP_QSTRING_H
#define UNORDERED_MAP_QSTRING_H
#include <QString>
#include <memory>
#include <unordered_map>
class UnorderedMapQString
{
public:
UnorderedMapQString() {};
std::unordered_map<QString, std::unique_ptr<QString>> container;
void addItem(QString aKey, std::unique_ptr<QString> aValue);
void showItem(QString aKey);
};
#endif // UNORDERED_MAP_QSTRING_H
unordered_map_qstring.cpp
#include "unordered_map_qstring.h"
#include <iostream>
void UnorderedMapQString::addItem(QString aKey, std::unique_ptr<QString> aValue)
{
container[aKey] = std::move(aValue);
}
void UnorderedMapQString::showItem(QString aKey)
{
QString *aValue = container[aKey].get();
std::cout << "Item '" << aKey.toStdString() << "': '" << (*aValue).toStdString() << "'\n";
}
main.cpp
#include "unordered_map_qstring.h"
#include <QString>
#include <memory>
int main(int argc, char *argv[])
{
UnorderedMapQString testContainer;
testContainer.addItem("key 1", std::make_unique<QString>("one value"));
testContainer.addItem("key 2", std::make_unique<QString>("two value"));
testContainer.addItem("key 3", std::make_unique<QString>("three value"));
testContainer.showItem("key 2");
testContainer.showItem("key 3");
testContainer.showItem("key 2");
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(unique_ptr_qstring LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED)
add_executable(unique_ptr_qstring
main.cpp
unordered_map_qstring.h unordered_map_qstring.cpp
)
target_link_libraries(unique_ptr_qstring Qt${QT_VERSION_MAJOR}::Core)
EDIT 2: compile errors for example 2
$ make
[ 33%] Building CXX object CMakeFiles/unique_ptr_qstring.dir/main.cpp.o
In file included from /home/rodrigo/devel/unordered_map-qstring/main.cpp:1:
/home/rodrigo/devel/unordered_map-qstring/unordered_map_qstring.h: In constructor ‘UnorderedMapQString::UnorderedMapQString()’:
/home/rodrigo/devel/unordered_map-qstring/unordered_map_qstring.h:12:27: error: use of deleted function ‘std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unordered_map() [with _Key = QString; _Tp = std::unique_ptr<QString>; _Hash = std::hash<QString>; _Pred = std::equal_to<QString>; _Alloc = std::allocator<std::pair<const QString, std::unique_ptr<QString> > >]’
12 | UnorderedMapQString() {};
| ^
In file included from /usr/include/c++/9/unordered_map:47,
from /usr/include/c++/9/functional:61,
from /usr/include/c++/9/pstl/glue_algorithm_defs.h:13,
from /usr/include/c++/9/algorithm:71,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:142,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qchar.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qstring.h:48,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/QString:1,
from /home/rodrigo/devel/unordered_map-qstring/unordered_map_qstring.h:4,
from /home/rodrigo/devel/unordered_map-qstring/main.cpp:1:
/usr/include/c++/9/bits/unordered_map.h:141:7: note: ‘std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unordered_map() [with _Key = QString; _Tp = std::unique_ptr<QString>; _Hash = std::hash<QString>; _Pred = std::equal_to<QString>; _Alloc = std::allocator<std::pair<const QString, std::unique_ptr<QString> > >]’ is implicitly deleted because the default definition would be ill-formed:
141 | unordered_map() = default;
| ^~~~~~~~~~~~~
/usr/include/c++/9/bits/unordered_map.h:141:7: error: use of deleted function ‘std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::_Hashtable() [with _Key = QString; _Value = std::pair<const QString, std::unique_ptr<QString> >; _Alloc = std::allocator<std::pair<const QString, std::unique_ptr<QString> > >; _ExtractKey = std::__detail::_Select1st; _Equal = std::equal_to<QString>; _H1 = std::hash<QString>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true>]’
In file included from /usr/include/c++/9/unordered_map:46,
from /usr/include/c++/9/functional:61,
from /usr/include/c++/9/pstl/glue_algorithm_defs.h:13,
from /usr/include/c++/9/algorithm:71,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:142,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qchar.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qstring.h:48,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/QString:1,
from /home/rodrigo/devel/unordered_map-qstring/unordered_map_qstring.h:4,
from /home/rodrigo/devel/unordered_map-qstring/main.cpp:1:
/usr/include/c++/9/bits/hashtable.h:414:7: note: ‘std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::_Hashtable() [with _Key = QString; _Value = std::pair<const QString, std::unique_ptr<QString> >; _Alloc = std::allocator<std::pair<const QString, std::unique_ptr<QString> > >; _ExtractKey = std::__detail::_Select1st; _Equal = std::equal_to<QString>; _H1 = std::hash<QString>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true> ’ is implicitly deleted because the default definition would be ill-formed:
414 | _Hashtable() = default;
| ^~~~~~~~~~
/usr/include/c++/9/bits/hashtable.h:414:7: error: use of deleted function ‘std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, _Hash, _Traits>::_Hashtable_base() [with _Key = QString; _Value = std::pair<const QString, std::unique_ptr<QString> >; _ExtractKey = std::__detail::_Select1st; _Equal = std::equal_to<QString>; _H1 = std::hash<QString>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _Traits = std::__detail::_Hashtable_traits<true, false, true>]’
In file included from /usr/include/c++/9/bits/hashtable.h:35,
from /usr/include/c++/9/unordered_map:46,
from /usr/include/c++/9/functional:61,
from /usr/include/c++/9/pstl/glue_algorithm_defs.h:13,
from /usr/include/c++/9/algorithm:71,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:142,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qchar.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qstring.h:48,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/QString:1,
from /home/rodrigo/devel/unordered_map-qstring/unordered_map_qstring.h:4,
from /home/rodrigo/devel/unordered_map-qstring/main.cpp:1:
/usr/include/c++/9/bits/hashtable_policy.h:1822:5: note: ‘std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, _Hash, _Traits>::_Hashtable_base() [with _Key = QString; _Value = std::pair<const QString, std::unique_ptr<QString> >; _ExtractKey = std::__detail::_Select1st; _Equal = std::equal_to<QString>; _H1 = std::hash<QString>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _Traits = std::__detail::_Hashtable_traits<true, false, true>]’ is implicitly deleted because the default definition would be ill-formed:
1822 | _Hashtable_base() = default;
| ^~~~~~~~~~~~~~~
/usr/include/c++/9/bits/hashtable_policy.h:1822:5: error: use of deleted function ‘std::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, std::__detail::_Default_ranged_hash, true>::_Hash_code_base() [with _Key = QString; _Value = std::pair<const QString, std::unique_ptr<QString> >; _ExtractKey = std::__detail::_Select1st; _H1 = std::hash<QString>; _H2 = std::__detail::_Mod_range_hashing]’
/usr/include/c++/9/bits/hashtable_policy.h:1373:7: note: ‘std::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, std::__detail::_Default_ranged_hash, true>::_Hash_code_base() [with _Key = QString; _Value = std::pair<const QString, std::unique_ptr<QString> >; _ExtractKey = std::__detail::_Select1st; _H1 = std::hash<QString>; _H2 = std::__detail::_Mod_range_hashing]’ is implicitly deleted because the default definition would be ill-formed:
1373 | _Hash_code_base() = default;
| ^~~~~~~~~~~~~~~
/usr/include/c++/9/bits/hashtable_policy.h:1373:7: error: use of deleted function ‘std::__detail::_Hashtable_ebo_helper<_Nm, _Tp, true>::_Hashtable_ebo_helper() [with int _Nm = 1; _Tp = std::hash<QString>]’
/usr/include/c++/9/bits/hashtable_policy.h:1096:7: note: ‘std::__detail::_Hashtable_ebo_helper<_Nm, _Tp, true>::_Hashtable_ebo_helper() [with int _Nm = 1; _Tp = std::hash<QString>]’ is implicitly deleted because the default definition would be ill-formed:
1096 | _Hashtable_ebo_helper() = default;
| ^~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/9/bits/hashtable_policy.h:1096:7: error: use of deleted function ‘std::hash<QString>::hash()’
In file included from /usr/include/c++/9/string_view:43,
from /usr/include/c++/9/bits/basic_string.h:48,
from /usr/include/c++/9/string:55,
from /usr/include/c++/9/stdexcept:39,
from /usr/include/c++/9/array:39,
from /usr/include/c++/9/tuple:39,
from /usr/include/c++/9/functional:54,
from /usr/include/c++/9/pstl/glue_algorithm_defs.h:13,
from /usr/include/c++/9/algorithm:71,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:142,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qchar.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qstring.h:48,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/QString:1,
from /home/rodrigo/devel/unordered_map-qstring/unordered_map_qstring.h:4,
from /home/rodrigo/devel/unordered_map-qstring/main.cpp:1:
/usr/include/c++/9/bits/functional_hash.h:101:12: note: ‘std::hash<QString>::hash()’ is implicitly deleted because the default definition would be ill-formed:
101 | struct hash : __hash_enum<_Tp>
| ^~~~
/usr/include/c++/9/bits/functional_hash.h:101:12: error: no matching function for call to ‘std::__hash_enum<QString, false>::__hash_enum()’
/usr/include/c++/9/bits/functional_hash.h:82:7: note: candidate: ‘std::__hash_enum<_Tp, <anonymous> >::__hash_enum(std::__hash_enum<_Tp, <anonymous> >&&) [with _Tp = QString; bool <anonymous> = false]’
82 | __hash_enum(__hash_enum&&);
| ^~~~~~~~~~~
/usr/include/c++/9/bits/functional_hash.h:82:7: note: candidate expects 1 argument, 0 provided
/usr/include/c++/9/bits/functional_hash.h:101:12: error: ‘std::__hash_enum<_Tp, <anonymous> >::~__hash_enum() [with _Tp = QString; bool <anonymous> = false]’ is private within this context
101 | struct hash : __hash_enum<_Tp>
| ^~~~
/usr/include/c++/9/bits/functional_hash.h:83:7: note: declared private here
83 | ~__hash_enum();
| ^
In file included from /usr/include/c++/9/bits/hashtable.h:35,
from /usr/include/c++/9/unordered_map:46,
from /usr/include/c++/9/functional:61,
from /usr/include/c++/9/pstl/glue_algorithm_defs.h:13,
from /usr/include/c++/9/algorithm:71,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:142,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qchar.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qstring.h:48,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/QString:1,
from /home/rodrigo/devel/unordered_map-qstring/unordered_map_qstring.h:4,
from /home/rodrigo/devel/unordered_map-qstring/main.cpp:1:
/usr/include/c++/9/bits/hashtable_policy.h:1096:7: error: use of deleted function ‘std::hash<QString>::~hash()’
1096 | _Hashtable_ebo_helper() = default;
| ^~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/string_view:43,
from /usr/include/c++/9/bits/basic_string.h:48,
from /usr/include/c++/9/string:55,
from /usr/include/c++/9/stdexcept:39,
from /usr/include/c++/9/array:39,
from /usr/include/c++/9/tuple:39,
from /usr/include/c++/9/functional:54,
from /usr/include/c++/9/pstl/glue_algorithm_defs.h:13,
from /usr/include/c++/9/algorithm:71,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:142,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qchar.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qstring.h:48,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/QString:1,
from /home/rodrigo/devel/unordered_map-qstring/unordered_map_qstring.h:4,
from /home/rodrigo/devel/unordered_map-qstring/main.cpp:1:
/usr/include/c++/9/bits/functional_hash.h:101:12: note: ‘std::hash<QString>::~hash()’ is implicitly deleted because the default definition would be ill-formed:
101 | struct hash : __hash_enum<_Tp>
| ^~~~
/usr/include/c++/9/bits/functional_hash.h:101:12: error: ‘std::__hash_enum<_Tp, <anonymous> >::~__hash_enum() [with _Tp = QString; bool <anonymous> = false]’ is private within this context
/usr/include/c++/9/bits/functional_hash.h:83:7: note: declared private here
83 | ~__hash_enum();
| ^
In file included from /usr/include/c++/9/bits/hashtable.h:35,
from /usr/include/c++/9/unordered_map:46,
from /usr/include/c++/9/functional:61,
from /usr/include/c++/9/pstl/glue_algorithm_defs.h:13,
from /usr/include/c++/9/algorithm:71,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:142,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qchar.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qstring.h:48,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/QString:1,
from /home/rodrigo/devel/unordered_map-qstring/unordered_map_qstring.h:4,
from /home/rodrigo/devel/unordered_map-qstring/main.cpp:1:
/usr/include/c++/9/bits/hashtable_policy.h:1373:7: error: use of deleted function ‘std::__detail::_Hashtable_ebo_helper<1, std::hash<QString>, true>::~_Hashtable_ebo_helper()’
1373 | _Hash_code_base() = default;
| ^~~~~~~~~~~~~~~
/usr/include/c++/9/bits/hashtable_policy.h:1093:12: note: ‘std::__detail::_Hashtable_ebo_helper<1, std::hash<QString>, true>::~_Hashtable_ebo_helper()’ is implicitly deleted because the default definition would be ill-formed:
1093 | struct _Hashtable_ebo_helper<_Nm, _Tp, true>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/9/bits/hashtable_policy.h:1093:12: error: use of deleted function ‘std::hash<QString>::~hash()’
/usr/include/c++/9/bits/hashtable_policy.h:1822:5: error: use of deleted function ‘std::__detail::_Hash_code_base<QString, std::pair<const QString, std::unique_ptr<QString> >, std::__detail::_Select1st, std::hash<QString>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>::~_Hash_code_base()’
1822 | _Hashtable_base() = default;
| ^~~~~~~~~~~~~~~
/usr/include/c++/9/bits/hashtable_policy.h:1346:12: note: ‘std::__detail::_Hash_code_base<QString, std::pair<const QString, std::unique_ptr<QString> >, std::__detail::_Select1st, std::hash<QString>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>::~_Hash_code_base()’ is implicitly deleted because the default definition would be ill-formed:
1346 | struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1347 | _Default_ranged_hash, true>
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/9/bits/hashtable_policy.h:1346:12: error: use of deleted function ‘std::__detail::_Hashtable_ebo_helper<1, std::hash<QString>, true>::~_Hashtable_ebo_helper()’
In file included from /usr/include/c++/9/unordered_map:46,
from /usr/include/c++/9/functional:61,
from /usr/include/c++/9/pstl/glue_algorithm_defs.h:13,
from /usr/include/c++/9/algorithm:71,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:142,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qchar.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qstring.h:48,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/QString:1,
from /home/rodrigo/devel/unordered_map-qstring/unordered_map_qstring.h:4,
from /home/rodrigo/devel/unordered_map-qstring/main.cpp:1:
/usr/include/c++/9/bits/hashtable.h:414:7: error: use of deleted function ‘std::__detail::_Hashtable_base<QString, std::pair<const QString, std::unique_ptr<QString> >, std::__detail::_Select1st, std::equal_to<QString>, std::hash<QString>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, false, true> >::~_Hashtable_base()’
414 | _Hashtable() = default;
| ^~~~~~~~~~
In file included from /usr/include/c++/9/bits/hashtable.h:35,
from /usr/include/c++/9/unordered_map:46,
from /usr/include/c++/9/functional:61,
from /usr/include/c++/9/pstl/glue_algorithm_defs.h:13,
from /usr/include/c++/9/algorithm:71,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:142,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qchar.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qstring.h:48,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/QString:1,
from /home/rodrigo/devel/unordered_map-qstring/unordered_map_qstring.h:4,
from /home/rodrigo/devel/unordered_map-qstring/main.cpp:1:
/usr/include/c++/9/bits/hashtable_policy.h:1770:10: note: ‘std::__detail::_Hashtable_base<QString, std::pair<const QString, std::unique_ptr<QString> >, std::__detail::_Select1st, std::equal_to<QString>, std::hash<QString>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, false, true> >::~_Hashtable_base()’ is implicitly deleted because the default definition would be ill-formed:
1770 | struct _Hashtable_base
| ^~~~~~~~~~~~~~~
/usr/include/c++/9/bits/hashtable_policy.h:1770:10: error: use of deleted function ‘std::__detail::_Hash_code_base<QString, std::pair<const QString, std::unique_ptr<QString> >, std::__detail::_Select1st, std::hash<QString>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>::~_Hash_code_base()’
In file included from /usr/include/c++/9/unordered_map:46,
from /usr/include/c++/9/functional:61,
from /usr/include/c++/9/pstl/glue_algorithm_defs.h:13,
from /usr/include/c++/9/algorithm:71,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:142,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qchar.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qstring.h:48,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/QString:1,
from /home/rodrigo/devel/unordered_map-qstring/unordered_map_qstring.h:4,
from /home/rodrigo/devel/unordered_map-qstring/main.cpp:1:
/usr/include/c++/9/bits/hashtable.h: In instantiation of ‘std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::~_Hashtable() [with _Key = QString; _Value = std::pair<const QString, std::unique_ptr<QString> >; _Alloc = std::allocator<std::pair<const QString, std::unique_ptr<QString> > >; _ExtractKey = std::__detail::_Select1st; _Equal = std::equal_to<QString>; _H1 = std::hash<QString>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true>]’:
/usr/include/c++/9/bits/unordered_map.h:102:11: required from here
/usr/include/c++/9/bits/hashtable.h:1354:5: error: use of deleted function ‘std::__detail::_Hashtable_base<QString, std::pair<const QString, std::unique_ptr<QString> >, std::__detail::_Select1st, std::equal_to<QString>, std::hash<QString>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, false, true> >::~_Hashtable_base()’
1354 | }
| ^
make[2]: *** [CMakeFiles/unique_ptr_qstring.dir/build.make:63: CMakeFiles/unique_ptr_qstring.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/unique_ptr_qstring.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
Example 3 (not working): QHash and QString
qhash_qstring.h
#ifndef QHASH_QSTRING_H
#define QHASH_QSTRING_H
#include <QHash>
#include <QString>
#include <memory>
class QHashQString
{
public:
QHashQString() {};
QHash<QString, std::unique_ptr<QString>> container;
void addItem(QString aKey, std::unique_ptr<QString> aValue);
void showItem(QString aKey);
};
#endif // QHASH_QSTRING_H
qhash_qstring.cpp
#include "qhash_qstring.h"
#include <iostream>
void QHashQString::addItem(QString aKey, std::unique_ptr<QString> aValue)
{
container[aKey] = std::move(aValue);
}
void QHashQString::showItem(QString aKey)
{
QString *aValue = container[aKey].get();
std::cout << "Item '" << aKey.toStdString() << "': '" << (*aValue).toStdString() << "'\n";
}
main.cpp
#include "qhash_qstring.h"
#include <memory>
int main(int argc, char *argv[])
{
QHashQString testContainer;
testContainer.addItem("key 1", std::make_unique<QString>("one value"));
testContainer.addItem("key 2", std::make_unique<QString>("two value"));
testContainer.addItem("key 3", std::make_unique<QString>("three value"));
testContainer.showItem("key 2");
testContainer.showItem("key 3");
testContainer.showItem("key 2");
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(qhash_qstring LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED)
add_executable(qhash_qstring
main.cpp
qhash_qstring.h qhash_qstring.cpp
)
target_link_libraries(qhash_qstring Qt${QT_VERSION_MAJOR}::Core)
Edit 3: compile errors for example 3
$ make
[ 33%] Building CXX object CMakeFiles/qhash_qstring.dir/qhash_qstring.cpp.o
In file included from /usr/include/x86_64-linux-gnu/qt5/QtCore/QHash:1,
from /home/rodrigo/devel/qhash-qstring/qhash_qstring.h:4,
from /home/rodrigo/devel/qhash-qstring/qhash_qstring.cpp:1:
/usr/include/x86_64-linux-gnu/qt5/QtCore/qhash.h: In instantiation of ‘QHashNode<Key, T>::QHashNode(const Key&, const T&, uint, QHashNode<Key, T>*) [with Key = QString; T = std::unique_ptr<QString>; uint = unsigned int]’:
/usr/include/x86_64-linux-gnu/qt5/QtCore/qhash.h:562:18: required from ‘QHash<K, V>::Node* QHash<K, V>::createNode(uint, const Key&, const T&, QHash<K, V>::Node**) [with Key = QString; T = std::unique_ptr<QString>; QHash<K, V>::Node = QHashNode<QString, std::unique_ptr<QString> >; uint = unsigned int]’
/usr/include/x86_64-linux-gnu/qt5/QtCore/qhash.h:762:16: required from ‘T& QHash<K, V>::operator[](const Key&) [with Key = QString; T = std::unique_ptr<QString>]’
/home/rodrigo/devel/qhash-qstring/qhash_qstring.cpp:7:19: required from here
/usr/include/x86_64-linux-gnu/qt5/QtCore/qhash.h:157:52: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = QString; _Dp = std::default_delete<QString>]’
157 | : next(n), h(hash), key(key0), value(value0) {}
| ^
In file included from /usr/include/c++/9/memory:80,
from /home/rodrigo/devel/qhash-qstring/qhash_qstring.h:7,
from /home/rodrigo/devel/qhash-qstring/qhash_qstring.cpp:1:
/usr/include/c++/9/bits/unique_ptr.h:414:7: note: declared here
414 | unique_ptr(const unique_ptr&) = delete;
| ^~~~~~~~~~
make[2]: *** [CMakeFiles/qhash_qstring.dir/build.make:76: CMakeFiles/qhash_qstring.dir/qhash_qstring.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/qhash_qstring.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
Example 2 does not compile because std::unordered_map requires its key type to be "hashable": by default, it will use the std::hash template. You can make it work by writing a specialisation for QString:
#include <functional> // std::hash
#include <QHashFunctions> // qHash
#include <QString>
template<>
struct std::hash<QString>
{
std::size_t operator()(QString const& s) const noexcept
{
return qHash(s);
}
};
Example 3 does not compile because Qt containers use implicit sharing and only support copy semantics, even when inserting new elements. Therefore the compiler is looking for the copy constructor of std::unique_ptr, which is deleted:
unique_ptr(const unique_ptr&) = delete;
This example cannot be made to work, it's a fundamental design difference between Qt and standard library containers.

copying between instances of different variant template classes

Why does one variant assignment compile while the other does not? The template instances do not share any types and char could be converted to int, say. What is boost::variant trying to do, that it cannot do in the case of the first assignment and that it can do in the case of the second assignment? Error is below.
#include <string>
#include "boost/variant.hpp"
int main()
{
boost::variant<char> v1;
boost::variant<std::string, int, double> v2;
v1 = v2; // compile error
v2 = v1; // compiles fine
return 0;
}
In file included from /usr/include/boost/variant.hpp:17:0,
from v.cpp:3:
/usr/include/boost/variant/variant.hpp: In instantiation of 'int boost::variant<T0, TN>::convert_copy_into::internal_visit(T&, int) const [with T = const std::basic_string<char>; T0_ = char; TN = {}]':
/usr/include/boost/variant/detail/visitation_impl.hpp:113:9: required from 'typename Visitor::result_type boost::detail::variant::visitation_impl_invoke_impl(int, Visitor&, VoidPtrCV, T*, mpl_::true_) [with Visitor = boost::variant<char>::convert_copy_into; VoidPtrCV = const void*; T = std::basic_string<char>; typename Visitor::result_type = int; mpl_::true_ = mpl_::bool_<true>]'
/usr/include/boost/variant/detail/visitation_impl.hpp:156:9: required from 'typename Visitor::result_type boost::detail::variant::visitation_impl_invoke(int, Visitor&, VoidPtrCV, T*, NoBackupFlag, int) [with Visitor = boost::variant<char>::convert_copy_into; VoidPtrCV = const void*; T = std::basic_string<char>; NoBackupFlag = boost::variant<std::basic_string<char>, int, double>::has_fallback_type_; typename Visitor::result_type = int]'
/usr/include/boost/variant/detail/visitation_impl.hpp:237:5: required from 'typename Visitor::result_type boost::detail::variant::visitation_impl(int, int, Visitor&, VoidPtrCV, mpl_::false_, NoBackupFlag, Which*, step0*) [with Which = mpl_::int_<0>; step0 = boost::detail::variant::visitation_impl_step<boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<3l>, std::basic_string<char>, boost::mpl::l_item<mpl_::long_<2l>, int, boost::mpl::l_item<mpl_::long_<1l>, double, boost::mpl::l_end> > > >, boost::mpl::l_iter<boost::mpl::l_end> >; Visitor = boost::variant<char>::convert_copy_into; VoidPtrCV = const void*; NoBackupFlag = boost::variant<std::basic_string<char>, int, double>::has_fallback_type_; typename Visitor::result_type = int; mpl_::false_ = mpl_::bool_<false>]'
/usr/include/boost/variant/variant.hpp:2245:13: required from 'static typename Visitor::result_type boost::variant<T0, TN>::internal_apply_visitor_impl(int, int, Visitor&, VoidPtrCV) [with Visitor = boost::variant<char>::convert_copy_into; VoidPtrCV = const void*; T0_ = std::basic_string<char>; TN = {int, double}; typename Visitor::result_type = int]'
/usr/include/boost/variant/variant.hpp:2267:13: [ skipping 2 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/boost/variant/variant.hpp:1581:13: required from 'void boost::variant<T0, TN>::convert_construct_variant(Variant&) [with Variant = const boost::variant<std::basic_string<char>, int, double>; T0_ = char; TN = {}]'
/usr/include/boost/variant/variant.hpp:1628:42: required from 'void boost::variant<T0, TN>::convert_construct(const boost::variant<U0, UN ...>&, long int) [with U0 = std::basic_string<char>; UN = {int, double}; T0_ = char; TN = {}]'
/usr/include/boost/variant/variant.hpp:1649:38: required from 'boost::variant<T0, TN>::variant(const T&) [with T = boost::variant<std::basic_string<char>, int, double>; T0_ = char; TN = {}]'
/usr/include/boost/variant/variant.hpp:2059:29: required from 'void boost::variant<T0, TN>::assign(const T&) [with T = boost::variant<std::basic_string<char>, int, double>; T0_ = char; TN = {}]'
/usr/include/boost/variant/variant.hpp:2099:19: required from 'boost::variant<T0, TN>& boost::variant<T0, TN>::operator=(const T&) [with T = boost::variant<std::basic_string<char>, int, double>; T0_ = char; TN = {}]'
v.cpp:10:6: required from here
/usr/include/boost/variant/variant.hpp:1366:61: error: no matching function for call to 'boost::variant<char>::initializer::initialize(void* const&, const std::basic_string<char>&)'
return initializer::initialize(storage_, operand);
^
/usr/include/boost/variant/variant.hpp:1366:61: note: candidates are:
In file included from /usr/include/boost/variant/variant.hpp:32:0,
from /usr/include/boost/variant.hpp:17,
from v.cpp:3:
/usr/include/boost/variant/detail/initializer.hpp:104:24: note: static int boost::detail::variant::make_initializer_node::apply<BaseIndexPair, Iterator>::initializer_node::initialize(void*, boost::detail::variant::make_initializer_node::apply<BaseIndexPair, Iterator>::initializer_node::param_T) [with BaseIndexPair = boost::mpl::pair<boost::detail::variant::initializer_root, mpl_::int_<0> >; Iterator = boost::mpl::l_iter<boost::mpl::list1<char> >; boost::detail::variant::make_initializer_node::apply<BaseIndexPair, Iterator>::initializer_node::param_T = const char&]
static int initialize(void* dest, param_T operand)
^
/usr/include/boost/variant/detail/initializer.hpp:104:24: note: no known conversion for argument 2 from 'const std::basic_string<char>' to 'boost::detail::variant::make_initializer_node::apply<boost::mpl::pair<boost::detail::variant::initializer_root, mpl_::int_<0> >, boost::mpl::l_iter<boost::mpl::list1<char> > >::initializer_node::param_T {aka const char&}'
/usr/include/boost/variant/detail/initializer.hpp:115:24: note: static int boost::detail::variant::make_initializer_node::apply<BaseIndexPair, Iterator>::initializer_node::initialize(void*, boost::detail::variant::make_initializer_node::apply<BaseIndexPair, Iterator>::initializer_node::param2_T) [with BaseIndexPair = boost::mpl::pair<boost::detail::variant::initializer_root, mpl_::int_<0> >; Iterator = boost::mpl::l_iter<boost::mpl::list1<char> >; boost::detail::variant::make_initializer_node::apply<BaseIndexPair, Iterator>::initializer_node::param2_T = char&&]
static int initialize(void* dest, param2_T operand)
^
/usr/include/boost/variant/detail/initializer.hpp:115:24: note: no known conversion for argument 2 from 'const std::basic_string<char>' to 'boost::detail::variant::make_initializer_node::apply<boost::mpl::pair<boost::detail::variant::initializer_root, mpl_::int_<0> >, boost::mpl::l_iter<boost::mpl::list1<char> > >::initializer_node::param2_T {aka char&&}'
/usr/include/boost/variant/detail/initializer.hpp:149:17: note: static void boost::detail::variant::initializer_root::initialize()
static void initialize();
^
/usr/include/boost/variant/detail/initializer.hpp:149:17: note: candidate expects 0 arguments, 2 provided
A char definitely can be stored in an int but the converse is not always true. Therefore the one that fails to compile would be unsafe at runtime, so you're probably better off with it failing. Cast the int to a char if you must.

Mpl for_each with mpl list

I'm trying to use mpl_list in for_each
#include <boost/mpl/list.hpp>
#include <algorithm>
#include <boost/mpl/for_each.hpp>
#include <string>
#include <istream>
#include <ostream>
#include <sstream>
#include <iostream>
#include <boost/mpl/range_c.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/bind.hpp>
using namespace std;
namespace mpl = boost::mpl;
typedef mpl::range_c<char,1,5> range5;
typedef mpl::list<
mpl::int_<1>
, mpl::int_<5>
, mpl::int_<31>
, mpl::int_<14>
, mpl::int_<51>
> inp_type;
typedef mpl::list<
mpl::int_<1>
, mpl::int_<5>
, mpl::int_<31>
, mpl::int_<14>
, mpl::int_<51>
> out_type;
template <class T> struct id {};
struct do_this_wrapper {
static char stat_c ;
typedef void result_type;
template<typename U> inline void operator()(int i, U )
{
if (i == U::value)
{
do_this_wrapper::stat_c = mpl::at_c<out_type,U::value>::type::value;
}
}
};
char do_this_wrapper::stat_c ;
int main()
{
int x;
std::cin>>x;
boost::mpl::for_each<inp_type>(boost::bind(do_this_wrapper(), x, _1));
return do_this_wrapper::stat_c;
};
I've got these errors
*... /usr/include/boost/mpl/list/aux_/iterator.hpp: In instantiation of ‘struct boost::mpl::deref >’: /usr/include/boost/mpl/aux_/at_impl.hpp:37:45: required from ‘struct boost::mpl::at_impl::apply, mpl_::int_<5>, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> >, mpl_::long_<5l> >’ /usr/include/boost/mpl/at.hpp:42:8: required from ‘struct boost::mpl::at_c, mpl_::int_<5>, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> >, 5l>’ ../src/TestProj3.cpp:2664:41: required from ‘void do_this_wrapper::operator()(int, U) [with U = mpl_::int_<5>]’ /usr/include/boost/bind/bind.hpp:313:34: required from ‘void boost::_bi::list2::operator()(boost::_bi::type, F&, A&, int) [with F = do_this_wrapper; A = boost::_bi::list1&>; A1 = boost::_bi::value; A2 = boost::arg<1>]’ /usr/include/boost/bind/bind_template.hpp:32:59: required from ‘boost::_bi::bind_t::result_type boost::_bi::bind_t::operator()(A1&) [with A1 = mpl_::int_<5>; R = boost::_bi::unspecified; F = do_this_wrapper; L = boost::_bi::list2, boost::arg<1> >; boost::_bi::bind_t::result_type = void]’ /usr/include/boost/mpl/for_each.hpp:75:25: required from ‘static void boost::mpl::aux::for_each_impl::execute(Iterator*, LastIterator*, TransformFunc*, F) [with Iterator = boost::mpl::l_iter, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> > >; LastIterator = boost::mpl::l_iter; TransformFunc = boost::mpl::identity; F = boost::_bi::bind_t, boost::arg<1> > >]’ /usr/include/boost/mpl/for_each.hpp:79:111: required from ‘static void boost::mpl::aux::for_each_impl::execute(Iterator*, LastIterator*, TransformFunc*, F) [with Iterator = boost::mpl::l_iter, mpl_::int_<5>, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> > >; LastIterator = boost::mpl::l_iter; TransformFunc = boost::mpl::identity; F = boost::_bi::bind_t, boost::arg<1> > >]’ /usr/include/boost/mpl/for_each.hpp:101:97: required from ‘void boost::mpl::for_each(F, Sequence*, TransformOp*) [with Sequence = boost::mpl::list, mpl_::int_<5>, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> >; TransformOp = boost::mpl::identity; F = boost::_bi::bind_t, boost::arg<1> > >]’ /usr/include/boost/mpl/for_each.hpp:111:38: required from ‘void boost::mpl::for_each(F, Sequence*) [with Sequence = boost::mpl::list, mpl_::int_<5>, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> >; F = boost::_bi::bind_t, boost::arg<1> > >]’ ../src/TestProj3.cpp:2678:77: required from here /usr/include/boost/mpl/list/aux_/iterator.hpp:39:33: error: no type named ‘item’ in ‘struct boost::mpl::l_end’ typedef typename Node::item type; ^ /usr/include/boost/mpl/list/aux_/iterator.hpp: In instantiation of ‘struct boost::mpl::next >’: /usr/include/boost/mpl/aux_/preprocessed/gcc/advance_forward.hpp:68:44: required from ‘struct boost::mpl::aux::advance_forward<4l>::apply
/usr/include/boost/mpl/aux_/preprocessed/gcc/apply_wrap.hpp:36:8:
required from ‘struct boost::mpl::apply_wrap1, boost::mpl::l_iter > > >’ /usr/include/boost/mpl/aux_/preprocessed/gcc/advance_forward.hpp:83:21: required from ‘struct boost::mpl::aux::advance_forward<27l>::apply ’ /usr/include/boost/mpl/aux_/preprocessed/gcc/apply_wrap.hpp:36:8:
required from ‘struct boost::mpl::apply_wrap1, boost::mpl::l_iter > > >’ /usr/include/boost/mpl/aux_/preprocessed/gcc/advance_forward.hpp:92:21: required from ‘struct boost::mpl::aux::advance_forward<31l>::apply, mpl_::int_<5>, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> > > >’ /usr/include/boost/mpl/aux_/preprocessed/gcc/apply_wrap.hpp:36:8: [ skipping 7 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ] /usr/include/boost/bind/bind_template.hpp:32:59: required from ‘boost::_bi::bind_t::result_type boost::_bi::bind_t::operator()(A1&) [with A1 = mpl_::int_<31>; R = boost::_bi::unspecified; F = do_this_wrapper; L = boost::_bi::list2, boost::arg<1> >; boost::_bi::bind_t::result_type = void]’ /usr/include/boost/mpl/for_each.hpp:79:111: recursively required from ‘static void boost::mpl::aux::for_each_impl::execute(Iterator*, LastIterator*, TransformFunc*, F) [with Iterator = boost::mpl::l_iter, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> > >; LastIterator = boost::mpl::l_iter; TransformFunc = boost::mpl::identity; F = boost::_bi::bind_t, boost::arg<1> > >]’ /usr/include/boost/mpl/for_each.hpp:79:111: required from ‘static void boost::mpl::aux::for_each_impl::execute(Iterator*, LastIterator*, TransformFunc*, F) [with Iterator = boost::mpl::l_iter, mpl_::int_<5>, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> > >; LastIterator = boost::mpl::l_iter; TransformFunc = boost::mpl::identity; F = boost::_bi::bind_t, boost::arg<1> > >]’ /usr/include/boost/mpl/for_each.hpp:101:97: required from ‘void boost::mpl::for_each(F, Sequence*, TransformOp*) [with Sequence = boost::mpl::list, mpl_::int_<5>, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> >; TransformOp = boost::mpl::identity; F = boost::_bi::bind_t, boost::arg<1> > >]’ /usr/include/boost/mpl/for_each.hpp:111:38: required from ‘void boost::mpl::for_each(F, Sequence*) [with Sequence = boost::mpl::list, mpl_::int_<5>, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> >; F = boost::_bi::bind_t, boost::arg<1> > >]’ ../src/TestProj3.cpp:2678:77: required from here /usr/include/boost/mpl/list/aux_/iterator.hpp:45:43: error: no type named ‘next’ in ‘struct boost::mpl::l_end*’
Would be thankfull for detailed descriprtion or sth to read. Thanks in advance
The issue is
do_this_wrapper::stat_c = mpl::at_c<out_type,U::value>::type::value;
This is asking MPL to find the element at the position in out_type that corresponds to the value of U. Since the inp_type has a value of 51 (among other things), it's failing be able to return the element in position 51 in out_type, since it only contains 5 elements.
Personally, I would attempt to join the two sequences into an associative sequence (mpl::map), and then use for_each over that.

Initialise a std::vector<std::vector<int> > with boost::assign::list_of

How can I use boost::assign::list_of to put values into an object of type std::vector<std::vector<int> >? Or have you any other suggestions as to how I can populate it for the purposes of unit testing?
UPDATED
I get the following compiler error for gcc 4.4.2
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_uninitialized.h: In static member function 'static _ForwardIterator std::__uninitialized_copy<<anonymous> >::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::_Deque_iterator<boost::assign_detail::generic_list<int>, boost::assign_detail::generic_list<int>&, boost::assign_detail::generic_list<int>*>, _ForwardIterator = std::vector<int, std::allocator<int> >*, bool <anonymous> = false]':
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_uninitialized.h:117: instantiated from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::_Deque_iterator<boost::assign_detail::generic_list<int>, boost::assign_detail::generic_list<int>&, boost::assign_detail::generic_list<int>*>, _ForwardIterator = std::vector<int, std::allocator<int> >*]'
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_uninitialized.h:257: instantiated from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::_Deque_iterator<boost::assign_detail::generic_list<int>, boost::assign_detail::generic_list<int>&, boost::assign_detail::generic_list<int>*>, _ForwardIterator = std::vector<int, std::allocator<int> >*, _Tp = std::vector<int, std::allocator<int> >]'
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_vector.h:1024: instantiated from 'void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = std::_Deque_iterator<boost::assign_detail::generic_list<int>, boost::assign_detail::generic_list<int>&, boost::assign_detail::generic_list<int>*>, _Tp = std::vector<int, std::allocator<int> >, _Alloc = std::allocator<std::vector<int, std::allocator<int> > >]'
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_vector.h:1002: instantiated from 'void std::vector<_Tp, _Alloc>::_M_initialize_dispatch(_InputIterator, _InputIterator, std::__false_type) [with _InputIterator = std::_Deque_iterator<boost::assign_detail::generic_list<int>, boost::assign_detail::generic_list<int>&, boost::assign_detail::generic_list<int>*>, _Tp = std::vector<int, std::allocator<int> >, _Alloc = std::allocator<std::vector<int, std::allocator<int> > >]'
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_vector.h:303: instantiated from 'std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const _Alloc&) [with _InputIterator = std::_Deque_iterator<boost::assign_detail::generic_list<int>, boost::assign_detail::generic_list<int>&, boost::assign_detail::generic_list<int>*>, _Tp = std::vector<int, std::allocator<int> >, _Alloc = std::allocator<std::vector<int, std::allocator<int> > >]'
/boost/boost_1_51_0/boost/assign/list_of.hpp:163: instantiated from 'Container boost::assign_detail::converter<DerivedTAssign, Iterator>::convert(const Container*, boost::assign_detail::default_type_tag) const [with Container = std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >, DerivedTAssign = boost::assign_detail::generic_list<boost::assign_detail::generic_list<int> >, Iterator = std::_Deque_iterator<boost::assign_detail::generic_list<int>, boost::assign_detail::generic_list<int>&, boost::assign_detail::generic_list<int>*>]'
/boost/boost_1_51_0/boost/assign/list_of.hpp:142: instantiated from 'Container boost::assign_detail::converter<DerivedTAssign, Iterator>::convert_to_container() const [with Container = std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >, DerivedTAssign = boost::assign_detail::generic_list<boost::assign_detail::generic_list<int> >, Iterator = std::_Deque_iterator<boost::assign_detail::generic_list<int>, boost::assign_detail::generic_list<int>&, boost::assign_detail::generic_list<int>*>]'
/boost/boost_1_51_0/boost/assign/list_of.hpp:436: instantiated from 'boost::assign_detail::generic_list<T>::operator Container() const [with Container = std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >, T = boost::assign_detail::generic_list<int>]'
main.cc:1: instantiated from here
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_uninitialized.h:74: error: call of overloaded 'vector(boost::assign_detail::generic_list<int>&)' is ambiguous
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_vector.h:241: note: candidates are: std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = int, _Alloc = std::allocator<int>]
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_vector.h:227: note: std::vector<_Tp, _Alloc>::vector(size_t, const _Tp&, const _Alloc&) [with _Tp = int, _Alloc = std::allocator<int>]
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_vector.h:215: note: std::vector<_Tp, _Alloc>::vector(const _Alloc&) [with _Tp = int, _Alloc = std::allocator<int>]
Here's my code:
std::vector< std::vector<int> > v = boost::assign::list_of
( boost::assign::list_of(1)(2) )
( boost::assign::list_of(3) )
( boost::assign::list_of(5)(6)(7)(8) );
#include <boost/range/algorithm.hpp>
#include <boost/foreach.hpp>
#include <boost/assign.hpp>
#include <iterator>
#include <iostream>
#include <ostream>
#include <vector>
int main()
{
using namespace boost;
using namespace assign;
using namespace std;
vector< vector<int> > v = list_of
( list_of(1)(2) )
( list_of(3) )
( list_of(5)(6)(7)(8) );
BOOST_FOREACH(const vector<int> &x, v)
{
copy(x,ostream_iterator<int>(cout," "));
cout << endl;
}
}
Output is:
1 2
3
5 6 7 8
I get the following compiler error for gcc 4.4.2
Follwing code works on GCC 4.4.6:
vector< vector<int> > v;
v += list_of(1)(2),
list_of(3),
list_of(5)(6)(7)(8);