Related
I am chasing a rabbit down the hole, by trying to figure out the following:
I do not want empty public constructors (they can, and will create issues)
Two template classes must remain ignorant of each-other, else I will have template parameter cyclic problems
A third class apparently needs access to both previous classes, as it is the one that is being (de)serialized
A minified example is shown below:
template <class s_trait>
class state
{
public:
state(s_trait);
private:
state() = default;
friend class boost::serialization::access;
template <typename archive>
void serialize(archive &, const unsigned int);
};
template <class a_trait>
class action
{
public:
action(a_trait);
private:
action() = default;
friend class boost::serialization::access;
template <typename archive>
void serialize(archive &, const unsigned int);
};
template <class state_type, class action_type>
class policy
{
public:
/// ... some public methods
private:
friend class boost::serialization::access;
std::unordered_map<state_class,
std::unordered_map<action_class,
double>> __policies__;
template <typename archive>
void serialize(archive &, const unsigned int);
};
The above skeleton (without the boring details) has the following design issues:
class state cannot be aware of action, if action is to be aware of state
class action cannot be aware of state and vice versa
class policy templates the state and action on purpose
However, compiling an app using those classes creates issues arising from std::pair, which seem to stem from boost::serialization::access, which in turn appear to be from the fact that class policy needs access to the default empty constructors when calling serialize:
/usr/include/c++/5/bits/stl_pair.h: In instantiation of ‘constexpr std::pair<_T1, _T2>::pair() [with _T1 = const relearn::state<mumbler::state<std::__cxx11::basic_string<char> > >; _T2 = std::unordered_map<relearn::action<mumbler::action<std::__cxx11::basic_string<char> > >, double, relearn::hasher<relearn::action<mu
mbler::action<std::__cxx11::basic_string<char> > > >, std::equal_to<relearn::action<mumbler::action<std::__cxx11::basic_string<char> > > >, std::allocator<std::pair<const relearn::action<mumbler::action<std::__cxx11::basic_string<char> > >, double> > >]’:
/usr/include/boost/serialization/access.hpp:132:9: required from ‘static void boost::serialization::access::construct(T*) [with T = std::pair<const relearn::state<mumbler::state<std::__cxx11::basic_string<char> > >, std::unordered_map<relearn::action<mumbler::action<std::__cxx11::basic_string<char> > >, double, rel
earn::hasher<relearn::action<mumbler::action<std::__cxx11::basic_string<char> > > >, std::equal_to<relearn::action<mumbler::action<std::__cxx11::basic_string<char> > > >, std::allocator<std::pair<const relearn::action<mumbler::action<std::__cxx11::basic_string<char> > >, double> > > >]’
/usr/include/boost/serialization/serialization.hpp:93:22: required from ‘void boost::serialization::load_construct_data(Archive&, T*, unsigned int) [with Archive = boost::archive::binary_iarchive; T = std::pair<const relearn::state<mumbler::state<std::__cxx11::basic_string<char> > >, std::unordered_map<relearn::act
ion<mumbler::action<std::__cxx11::basic_string<char> > >, double, relearn::hasher<relearn::action<mumbler::action<std::__cxx11::basic_string<char> > > >, std::equal_to<relearn::action<mumbler::action<std::__cxx11::basic_string<char> > > >, std::allocator<std::pair<const relearn::action<mumbler::action<std::__cxx11::b
asic_string<char> > >, double> > > >]’
/usr/include/boost/serialization/serialization.hpp:158:28: required from ‘void boost::serialization::load_construct_data_adl(Archive&, T*, unsigned int) [with Archive = boost::archive::binary_iarchive; T = std::pair<const relearn::state<mumbler::state<std::__cxx11::basic_string<char> > >, std::unordered_map<relearn
::action<mumbler::action<std::__cxx11::basic_string<char> > >, double, relearn::hasher<relearn::action<mumbler::action<std::__cxx11::basic_string<char> > > >, std::equal_to<relearn::action<mumbler::action<std::__cxx11::basic_string<char> > > >, std::allocator<std::pair<const relearn::action<mumbler::action<std::__cxx
11::basic_string<char> > >, double> > > >]’
/usr/include/boost/serialization/detail/stack_constructor.hpp:54:54: required from ‘boost::serialization::detail::stack_construct<Archive, T>::stack_construct(Archive&, unsigned int) [with Archive = boost::archive::binary_iarchive; T = std::pair<const relearn::state<mumbler::state<std::__cxx11::basic_string<char> >
>, std::unordered_map<relearn::action<mumbler::action<std::__cxx11::basic_string<char> > >, double, relearn::hasher<relearn::action<mumbler::action<std::__cxx11::basic_string<char> > > >, std::equal_to<relearn::action<mumbler::action<std::__cxx11::basic_string<char> > > >, std::allocator<std::pair<const relearn::act
ion<mumbler::action<std::__cxx11::basic_string<char> > >, double> > > >]’
/usr/include/boost/serialization/unordered_map.hpp:45:55: required from ‘void boost::serialization::stl::archive_input_unordered_map<Archive, Container>::operator()(Archive&, Container&, unsigned int) [with Archive = boost::archive::binary_iarchive; Container = std::unordered_map<relearn::state<mumbler::state<std::
__cxx11::basic_string<char> > >, std::unordered_map<relearn::action<mumbler::action<std::__cxx11::basic_string<char> > >, double, relearn::hasher<relearn::action<mumbler::action<std::__cxx11::basic_string<char> > > >, std::equal_to<relearn::action<mumbler::action<std::__cxx11::basic_string<char> > > >, std::allocator
<std::pair<const relearn::action<mumbler::action<std::__cxx11::basic_string<char> > >, double> > >, relearn::hasher<relearn::state<mumbler::state<std::__cxx11::basic_string<char> > > >, std::equal_to<relearn::state<mumbler::state<std::__cxx11::basic_string<char> > > >, std::allocator<std::pair<const relearn::state<mu
mbler::state<std::__cxx11::basic_string<char> > >, std::unordered_map<relearn::action<mumbler::action<std::__cxx11::basic_string<char> > >, double, relearn::hasher<relearn::action<mumbler::action<std::__cxx11::basic_string<char> > > >, std::equal_to<relearn::action<mumbler::action<std::__cxx11::basic_string<char> > >
>, std::allocator<std::pair<const relearn::action<mumbler::action<std::__cxx11::basic_string<char> > >, double> > > > > >]’
/usr/include/boost/serialization/unordered_collections_load_imp.hpp:66:14: [ skipping 24 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/boost/archive/detail/iserializer.hpp:618:18: required from ‘void boost::archive::load(Archive&, T&) [with Archive = boost::archive::binary_iarchive; T = relearn::policy<relearn::state<mumbler::state<std::__cxx11::basic_string<char> > >, relearn::action<mumbler::action<std::__cxx11::basic_string<char> >
> >]’
/usr/include/boost/archive/detail/common_iarchive.hpp:66:22: required from ‘void boost::archive::detail::common_iarchive<Archive>::load_override(T&, int) [with T = relearn::policy<relearn::state<mumbler::state<std::__cxx11::basic_string<char> > >, relearn::action<mumbler::action<std::__cxx11::basic_string<char> > >
>; Archive = boost::archive::binary_iarchive]’
/usr/include/boost/archive/basic_binary_iarchive.hpp:76:7: required from ‘void boost::archive::basic_binary_iarchive<Archive>::load_override(T&, int) [with T = relearn::policy<relearn::state<mumbler::state<std::__cxx11::basic_string<char> > >, relearn::action<mumbler::action<std::__cxx11::basic_string<char> > > >;
Archive = boost::archive::binary_iarchive]’
/usr/include/boost/archive/binary_iarchive_impl.hpp:62:9: required from ‘void boost::archive::binary_iarchive_impl<Archive, Elem, Tr>::load_override(T&, int) [with T = relearn::policy<relearn::state<mumbler::state<std::__cxx11::basic_string<char> > >, relearn::action<mumbler::action<std::__cxx11::basic_string<char>
> > >; Archive = boost::archive::binary_iarchive; Elem = char; Tr = std::char_traits<char>]’
/usr/include/boost/archive/detail/interface_iarchive.hpp:60:9: required from ‘Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = relearn::policy<relearn::state<mumbler::state<std::__cxx11::basic_string<char> > >, relearn::action<mumbler::action<std::__cxx11::basic_string<char> >
> >; Archive = boost::archive::binary_iarchive]’
/home/alex/codez/mumbler/src/agent.cpp:9:11: required from here
/home/alex/codez/mumbler/src/relearn/src/relearn.hpp:90:5: error: ‘relearn::state<state_trait, value_type>::state() [with state_trait = mumbler::state<std::__cxx11::basic_string<char> >; value_type = double]’ is private
state() = default;
As far as I can see, I have two options:
leave a public empty default constructor
forward-declare class policy, then befriend it - however this creates the issue that the classes befriending it need be aware of its template parameters, thus they need to be aware of each-other (state-action) which is what I would like to avoid.
Is there some other way to go around this without exposing public empty constructors?
edit
This is not a duplicate, I know how to (de)serialize, the question is about hiding the default constructor for only the (de)serializer to have access to it.
Make your default constructors protected, then make a derived class from each that has a public constructor but hide those derived classes from the outside entirely (put them in a detail header file that nobody else includes). Then your policy class can use the derived classes internally, for serialization and storage, but only expose them as (sliced) base class objects.
I'm converting two existing functions which check if an iterator is the last and second last item in a std::map to templates. The isLastItem template compiles, the isSecondLastItem fails with an error message I don't understand.
The one line causing the error is typename T::reverse_iterator secondLastIt = collection.rbegin();
But the template and non-template versions look identical.
typedef std::map<time_t, std::string> TInfoMap;
class MainClass
{
public:
MainClass() {};
void test();
private:
/* Doesn't work */
template <typename T>
bool
isSecondLastTemplate(const T &collection, const typename T::const_iterator &searchItem)
{
typename T::reverse_iterator secondLastIt = collection.rbegin();
return true;
}
/* this works */
bool
isSecondLastNoTemplate(const TInfoMap &infoMap, const TInfoMap::const_iterator &searchItem)
{
TInfoMap::reverse_iterator secondLastIt = infoMap.rbegin();
return true;
}
/* This works */
template <typename T>
bool
isLastItem(const T &collection, const typename T::const_iterator &searchItem)
{
if (collection.size() == 1)
return true;
// point to last item
typename T::const_iterator lastItem = collection.end();
--lastItem;
return searchItem == lastItem;
}
private:
TInfoMap mInfoMap;
};
void
MainClass::test()
{
mInfoMap[1] = "One";
mInfoMap[2] = "Two";
mInfoMap[3] = "Three";
TInfoMap::iterator it = mInfoMap.begin();
if (isLastItem(mInfoMap, it))
printf("Last Item!\n");
if (isSecondLastTemplate(mInfoMap, it))
printf("Second Last!\n");
if (isSecondLastNoTemplate(mInfoMap, it))
printf("Second Last!\n");
}
int
main(int argc, char **argv)
{
MainClass theStuff;
theStuff.test();
}
Compiler error:
In constructor ‘std::reverse_iterator<_Iterator>::reverse_iterator(const std::reverse_iterator<_Iter>&)
[with _Iter = std::_Rb_tree_const_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >,
_Iterator = std::_Rb_tree_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]’:
junk3.cpp:19: instantiated from here
error: no matching function for call to ‘std::_Rb_tree_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::_Rb_tree_iterator(std::_Rb_tree_const_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >)’
candidates are:
std::_Rb_tree_iterator<_Tp>::_Rb_tree_iterator(std::_Rb_tree_node<_Tp>*) [with _Tp = std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]
std::_Rb_tree_iterator<_Tp>::_Rb_tree_iterator() [with _Tp = std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]
std::_Rb_tree_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>,
std::allocator<char> > > >::_Rb_tree_iterator(const std::_Rb_tree_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&)
You need a const_reverse_iterator since collection is const
template <typename T>
bool
isSecondLastTemplate(const T &collection, const typename T::const_iterator &searchItem)
{
typename T::const_reverse_iterator secondLastIt = collection.rbegin();
^^^^^^^^^^^^^^^^^^^^^^
return true;
}
I'm trying to use custom allocator with C++ STL containers, and it works with vector, but fails with map. Some strange error regarding mmap_allocator<std::_Rb_tree_node<std::pair<const int, int> > > and not using mmap_allocator<std::pair<const int, int> > as I was expecting
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/map:60,
from 4.cpp:2:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_tree.h: In member function ‘_Alloc std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::get_allocator() const [with _Key = int, _Val = std::pair<const int, int>, _KeyOfValue = std::_Select1st<std::pair<const int, int> >, _Compare = std::less<int>, _Alloc = mmap_allocator<std::pair<const int, int> >]’:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_tree.h:383: instantiated from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_destroy_node(std::_Rb_tree_node<_Val>*) [with _Key = int, _Val = std::pair<const int, int>, _KeyOfValue = std::_Select1st<std::pair<const int, int> >, _Compare = std::less<int>, _Alloc = mmap_allocator<std::pair<const int, int> >]’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_tree.h:972: instantiated from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_erase(std::_Rb_tree_node<_Val>*) [with _Key = int, _Val = std::pair<const int, int>, _KeyOfValue = std::_Select1st<std::pair<const int, int> >, _Compare = std::less<int>, _Alloc = mmap_allocator<std::pair<const int, int> >]’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_tree.h:614: instantiated from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::~_Rb_tree() [with _Key = int, _Val = std::pair<const int, int>, _KeyOfValue = std::_Select1st<std::pair<const int, int> >, _Compare = std::less<int>, _Alloc = mmap_allocator<std::pair<const int, int> >]’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_map.h:87: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_tree.h:354: error: no matching function for call to ‘mmap_allocator<std::pair<const int, int> >::mmap_allocator(const mmap_allocator<std::_Rb_tree_node<std::pair<const int, int> > >&)’
4.cpp:37: note: candidates are: mmap_allocator<T>::mmap_allocator(const mmap_allocator<T>&) [with T = std::pair<const int, int>]
4.cpp:36: note: mmap_allocator<T>::mmap_allocator() [with T = std::pair<const int, int>]
Here is the code:
#include <vector>
#include <map>
#include <stdio.h>
static size_t alloc;
template <typename T>
class mmap_allocator: public std::allocator<T>
{
public:
typedef size_t size_type;
typedef T* pointer;
typedef const T* const_pointer;
template<typename _Tp1>
struct rebind
{
typedef mmap_allocator<_Tp1> other;
};
pointer allocate(size_type n, const void *hint=0)
{
fprintf(stderr, "Alloc %d bytes.\n", n);
alloc += n;
return std::allocator<T>::allocate(n, hint);
}
void deallocate(pointer p, size_type n)
{
fprintf(stderr, "Dealloc %d bytes (%p).\n", n, p);
alloc -= n;
return std::allocator<T>::deallocate(p, n);
}
mmap_allocator() throw(): std::allocator<T>() { fprintf(stderr, "Hello allocator!\n"); }
mmap_allocator(const mmap_allocator &a) throw(): std::allocator<T>(a) { }
~mmap_allocator() throw() { }
};
int main(){
std::vector<int, mmap_allocator<int> > int_vec(1024, 0, mmap_allocator<int>());
std::map<int, int, std::less<int>, mmap_allocator<std::pair<int,int> > > x;
x[1] = 2;
printf("s=%lu\n", alloc);
return 0;
}
Linux, gcc 4.4.6.
I haven't tried fixing it but it seems you haven't defined a constructor which takes an allocator instantiation with a different template argument. That is, you are missing something like
template <typename T>
template <typename O>
mmap_allocator<T>::mmap_allocator(mmap_allocator<O> const& other) {
...
}
From the looks of it, the error stems from trying to construct an allocator type obtained from rebind with some other allocator.
I want to create multi_index_container with type A storing objects of type C, which is derived from B which is derived from A. Problem is that in A I have pure virtual function. When I try to compile it, I got errors which are described at the very bottom.
I guess it's not possible to make-it-right?
Is whole idea flawed?
Code
#include <iostream>
#include <string>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/mem_fun.hpp>
using boost::multi_index_container;
using namespace boost::multi_index;
class A{
public:
virtual void print()=0;
std::string getName() const {return name;};
void setName(std::string s){name=s;};
std::string name;
};
template<typename T>
class B: public A{
public:
virtual void print(){std::cout<<"B"<<name<<std::endl;}
};
template<typename T>
class C: public B<T> {
public:
virtual void print(){std::cout<<"C"<<reinterpret_cast<A*>(this)->name<<std::endl;}
};
typedef multi_index_container<
A,
indexed_by<
hashed_non_unique<const_mem_fun<A, std::string, &A::getName>>
>
> Container;
int main(){
C<int> c;
c.setName("c");
Container container;
container.insert(c);
return 0;
}
Errors
In file included from /usr/local/include/boost/aligned_storage.hpp:20:0,
from /usr/local/include/boost/type_traits/aligned_storage.hpp:11,
from /usr/local/include/boost/multi_index/detail/index_node_base.hpp:17,
from /usr/local/include/boost/multi_index/detail/node_type.hpp:23,
from /usr/local/include/boost/multi_index/detail/index_base.hpp:21,
from /usr/local/include/boost/multi_index/detail/base_type.hpp:21,
from /usr/local/include/boost/multi_index_container.hpp:33,
from wierdInheritance.cpp:13:
/usr/local/include/boost/type_traits/alignment_of.hpp: In instantiation of ‘boost::detail::alignment_of_hack<A>’:
/usr/local/include/boost/type_traits/alignment_of.hpp:71:5: instantiated from ‘const size_t boost::detail::alignment_of_impl<A>::value’
/usr/local/include/boost/type_traits/alignment_of.hpp:89:1: instantiated from ‘boost::alignment_of<A>’
/usr/local/include/boost/multi_index/detail/index_node_base.hpp:42:32: instantiated from ‘boost::multi_index::detail::pod_value_holder<A>’
/usr/local/include/boost/multi_index/detail/index_node_base.hpp:46:8: instantiated from ‘boost::multi_index::detail::index_node_base<A, std::allocator<A> >’
/usr/local/include/boost/multi_index/detail/hash_index_node.hpp:116:8: instantiated from ‘boost::multi_index::detail::hashed_index_node<boost::multi_index::detail::index_node_base<A, std::allocator<A> > >’
/usr/local/include/boost/multi_index/hashed_index.hpp:108:54: instantiated from ‘boost::multi_index::detail::hashed_index<boost::multi_index::const_mem_fun<A, std::basic_string<char>, &A::getName>, boost::hash<std::basic_string<char> >, std::equal_to<std::basic_string<char> >, boost::multi_index::detail::nth_layer<1, A, boost::multi_index::indexed_by<boost::multi_index::hashed_non_unique<boost::multi_index::const_mem_fun<A, std::basic_string<char>, &A::getName> > >, std::allocator<A> >, boost::mpl::vector0<mpl_::na>, boost::multi_index::detail::hashed_non_unique_tag>’
/usr/local/include/boost/multi_index_container.hpp:70:7: instantiated from ‘boost::multi_index::multi_index_container<A, boost::multi_index::indexed_by<boost::multi_index::hashed_non_unique<boost::multi_index::const_mem_fun<A, std::basic_string<char>, &A::getName> > > >’
wierdInheritance.cpp:57:19: instantiated from here
/usr/local/include/boost/type_traits/alignment_of.hpp:42:7: error: cannot declare field ‘boost::detail::alignment_of_hack<A>::t’ to be of abstract type ‘A’
wierdInheritance.cpp:24:7: note: because the following virtual functions are pure within ‘A’:
wierdInheritance.cpp:26:22: note: virtual void A::print()
In file included from /usr/local/include/boost/multi_index_container.hpp:20:0,
from wierdInheritance.cpp:13:
/usr/local/include/boost/detail/allocator_utilities.hpp: In function ‘void boost::detail::allocator::construct(void*, const Type&) [with Type = A]’:
/usr/local/include/boost/multi_index/detail/index_base.hpp:88:5: instantiated from ‘boost::multi_index::detail::index_base<Value, IndexSpecifierList, Allocator>::node_type* boost::multi_index::detail::index_base<Value, IndexSpecifierList, Allocator>::insert_(boost::multi_index::detail::index_base<Value, IndexSpecifierList, Allocator>::value_param_type, boost::multi_index::detail::index_base<Value, IndexSpecifierList, Allocator>::node_type*) [with Value = A, IndexSpecifierList = boost::multi_index::indexed_by<boost::multi_index::hashed_non_unique<boost::multi_index::const_mem_fun<A, std::basic_string<char>, &A::getName> > >, Allocator = std::allocator<A>, boost::multi_index::detail::index_base<Value, IndexSpecifierList, Allocator>::node_type = boost::multi_index::detail::index_node_base<A, std::allocator<A> >, boost::multi_index::detail::index_base<Value, IndexSpecifierList, Allocator>::value_param_type = const A&]’
/usr/local/include/boost/multi_index/hashed_index.hpp:701:63: instantiated from ‘boost::multi_index::detail::hashed_index<KeyFromValue, Hash, Pred, SuperMeta, TagList, Category>::node_type* boost::multi_index::detail::hashed_index<KeyFromValue, Hash, Pred, SuperMeta, TagList, Category>::insert_(boost::multi_index::detail::hashed_index<KeyFromValue, Hash, Pred, SuperMeta, TagList, Category>::value_param_type, boost::multi_index::detail::hashed_index<KeyFromValue, Hash, Pred, SuperMeta, TagList, Category>::node_type*) [with KeyFromValue = boost::multi_index::const_mem_fun<A, std::basic_string<char>, &A::getName>, Hash = boost::hash<std::basic_string<char> >, Pred = std::equal_to<std::basic_string<char> >, SuperMeta = boost::multi_index::detail::nth_layer<1, A, boost::multi_index::indexed_by<boost::multi_index::hashed_non_unique<boost::multi_index::const_mem_fun<A, std::basic_string<char>, &A::getName> > >, std::allocator<A> >, TagList = boost::mpl::vector0<mpl_::na>, Category = boost::multi_index::detail::hashed_non_unique_tag, boost::multi_index::detail::hashed_index<KeyFromValue, Hash, Pred, SuperMeta, TagList, Category>::node_type = boost::multi_index::detail::hashed_index_node<boost::multi_index::detail::index_node_base<A, std::allocator<A> > >, typename SuperMeta::type::node_type = boost::multi_index::detail::index_node_base<A, std::allocator<A> >, boost::multi_index::detail::hashed_index<KeyFromValue, Hash, Pred, SuperMeta, TagList, Category>::value_param_type = const A&]’
/usr/local/include/boost/multi_index_container.hpp:488:40: instantiated from ‘std::pair<typename boost::multi_index::detail::multi_index_base_type<Value, IndexSpecifierList, Allocator>::type::node_type*, bool> boost::multi_index::multi_index_container<Value, IndexSpecifierList, Allocator>::insert_(const Value&) [with Value = A, IndexSpecifierList = boost::multi_index::indexed_by<boost::multi_index::hashed_non_unique<boost::multi_index::const_mem_fun<A, std::basic_string<char>, &A::getName> > >, Allocator = std::allocator<A>, typename boost::multi_index::detail::multi_index_base_type<Value, IndexSpecifierList, Allocator>::type::node_type = boost::multi_index::detail::hashed_index_node<boost::multi_index::detail::index_node_base<A, std::allocator<A> > >]’
/usr/local/include/boost/multi_index/detail/index_base.hpp:150:30: instantiated from ‘std::pair<typename boost::multi_index::detail::multi_index_node_type<Value, IndexSpecifierList, Allocator>::type*, bool> boost::multi_index::detail::index_base<Value, IndexSpecifierList, Allocator>::final_insert_(boost::multi_index::detail::index_base<Value, IndexSpecifierList, Allocator>::value_param_type) [with Value = A, IndexSpecifierList = boost::multi_index::indexed_by<boost::multi_index::hashed_non_unique<boost::multi_index::const_mem_fun<A, std::basic_string<char>, &A::getName> > >, Allocator = std::allocator<A>, typename boost::multi_index::detail::multi_index_node_type<Value, IndexSpecifierList, Allocator>::type = boost::multi_index::detail::hashed_index_node<boost::multi_index::detail::index_node_base<A, std::allocator<A> > >, boost::multi_index::detail::index_base<Value, IndexSpecifierList, Allocator>::value_param_type = const A&]’
/usr/local/include/boost/multi_index/hashed_index.hpp:254:61: instantiated from ‘std::pair<boost::multi_index::detail::hashed_index_iterator<boost::multi_index::detail::hashed_index_node<typename SuperMeta::type::node_type>, boost::multi_index::detail::bucket_array<typename SuperMeta::type::final_allocator_type> >, bool> boost::multi_index::detail::hashed_index<KeyFromValue, Hash, Pred, SuperMeta, TagList, Category>::insert(boost::multi_index::detail::hashed_index<KeyFromValue, Hash, Pred, SuperMeta, TagList, Category>::value_param_type) [with KeyFromValue = boost::multi_index::const_mem_fun<A, std::basic_string<char>, &A::getName>, Hash = boost::hash<std::basic_string<char> >, Pred = std::equal_to<std::basic_string<char> >, SuperMeta = boost::multi_index::detail::nth_layer<1, A, boost::multi_index::indexed_by<boost::multi_index::hashed_non_unique<boost::multi_index::const_mem_fun<A, std::basic_string<char>, &A::getName> > >, std::allocator<A> >, TagList = boost::mpl::vector0<mpl_::na>, Category = boost::multi_index::detail::hashed_non_unique_tag, typename SuperMeta::type::final_allocator_type = std::allocator<A>, typename SuperMeta::type::node_type = boost::multi_index::detail::index_node_base<A, std::allocator<A> >, boost::multi_index::detail::hashed_index<KeyFromValue, Hash, Pred, SuperMeta, TagList, Category>::value_param_type = const A&]’
wierdInheritance.cpp:58:27: instantiated from here
/usr/local/include/boost/detail/allocator_utilities.hpp:178:3: error: cannot allocate an object of abstract type ‘A’
wierdInheritance.cpp:24:7: note: since type ‘A’ has pure virtual functions
You cannot define a container of A, but you can define a container of pointers (eg. shared_ptr) to A:
typedef multi_index_container<
A *,
indexed_by<
hashed_non_unique<const_mem_fun<A, std::string, &A::getName>>
>
> Container;
or:
typedef multi_index_container<
boost::shared_ptr<A>,
indexed_by<
hashed_non_unique<const_mem_fun<A, std::string, &A::getName>>
>
> Container;
I am playing with container types, gettingmore into the details and try to build an unmodifiable (or immutable) map.
For that, I built some kind like copy-constructor to add all elements from another unordered_map into my UnmodifiableMap (which is basically a wrapper for std::unordered_map). To make it unmodifiable, I only provide const iterators and read-only methods. But I get stuck with the constructor, I am sure I miss something, maybe somebody can point me the problem here. Maybe it is a totally wrong way like this, but his is what tried so far:
template <typename Key, typename T,
typename HashFcn = std::hash<Key>,
typename EqualKey = std::equal_to<Key>,
typename Alloc = std::allocator<std::pair<const Key, T> > >
class UnmodifiableMap {
public:
// The actual data
typedef std::unordered_map<T, Key, HashFcn, EqualKey> base;
typedef Key key_type;
typedef T data_type;
typedef T mapped_type;
typedef std::pair<const key_type, data_type> value_type;
typedef HashFcn hasher;
typedef EqualKey key_equal;
typedef Alloc allocator_type;
typedef typename base::size_type size_type;
typedef typename base::const_iterator const_iterator;
typedef typename base::iterator iterator;
private:
base _map;
/**
* Constructs an empty unordered_map
*/
UnmodifiableMap(
size_type n = 0, const hasher& hf = hasher(),
const key_equal& eql = key_equal(),
const allocator_type &alloc = allocator_type())
: _map(n, hf, eql, alloc) {}
public:
/** Constructs a copy of unordered_map */
UnmodifiableMap(const base& other)
: _map(static_cast<const base&>(other)) {}
~UnmodifiableMap() {}
iterator begin() { return _map.begin(); }
iterator end() { return _map.end(); }
const_iterator begin() const { return _map.begin(); }
const_iterator end() const { return _map.end(); }
bool empty() const { return _map.empty(); }
bool contains(const key_type& key) const {
return _map.find(key) != _map.end(); }
};
And here the main body:
int main(int argc, char **argv) {
typedef std::unordered_map<int, std::string> Items;
Items map;
map[1] = "first string";
map[4] = "string 4";
map[5] = "string 5";
map[22] = "string 22";
map[12] = "string 12";
map[18] = "string 18";
typedef UnmodifiableMap<int, std::string> ReadOnlyItems;
ReadOnlyItems readonlymap(map);
return 0;
}
The error I get is
Unmodifiable_map.cpp: In function ‘int main(int, char**)’:
Unmodifiable_map.cpp:56:25: error: no matching function for call to ‘UnmodifiableMap<int, std::basic_string<char> >::UnmodifiableMap(Items&)’
Unmodifiable_map.cpp:56:25: note: candidates are:
Unmodifiable_map.h:45:2: note: UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::UnmodifiableMap(const base&) [with Key = int, T = std::basic_string<char>, HashFcn = std::hash<int>, EqualKey = std::equal_to<int>, Alloc = std::allocator<std::pair<const int, std::basic_string<char> > >, UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::base = std::unordered_map<std::basic_string<char>, int, std::hash<int>, std::equal_to<int>, std::allocator<std::pair<const std::basic_string<char>, int> > >]
Unmodifiable_map.h:45:2: note: no known conversion for argument 1 from ‘Items {aka std::unordered_map<int, std::basic_string<char> >}’ to ‘const base& {aka const std::unordered_map<std::basic_string<char>, int, std::hash<int>, std::equal_to<int>, std::allocator<std::pair<const std::basic_string<char>, int> > >&}’
Unmodifiable_map.h:37:2: note: UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::UnmodifiableMap(UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::size_type, const hasher&, const key_equal&, const allocator_type&) [with Key = int, T = std::basic_string<char>, HashFcn = std::hash<int>, EqualKey = std::equal_to<int>, Alloc = std::allocator<std::pair<const int, std::basic_string<char> > >, UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::size_type = long unsigned int, UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::hasher = std::hash<int>, UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::key_equal = std::equal_to<int>, UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::allocator_type = std::allocator<std::pair<const int, std::basic_string<char> > >]
Unmodifiable_map.h:37:2: note: no known conversion for argument 1 from ‘Items {aka std::unordered_map<int, std::basic_string<char> >}’ to ‘long unsigned int’
Unmodifiable_map.h:14:7: note: UnmodifiableMap<int, std::basic_string<char> >::UnmodifiableMap(const UnmodifiableMap<int, std::basic_string<char> >&)
Unmodifiable_map.h:14:7: note: no known conversion for argument 1 from ‘Items {aka std::unordered_map<int, std::basic_string<char> >}’ to ‘const UnmodifiableMap<int, std::basic_string<char> >&’
Unmodifiable_map.h:14:7: note: UnmodifiableMap<int, std::basic_string<char> >::UnmodifiableMap(UnmodifiableMap<int, std::basic_string<char> >&&)
Unmodifiable_map.h:14:7: note: no known conversion for argument 1 from ‘Items {aka std::unordered_map<int, std::basic_string<char> >}’ to ‘UnmodifiableMap<int, std::basic_string<char> >&&’
Hope somebody can shed some light on that. Also I think I need to do more in the copy constructor, propably copy the elements (like a swap function) and an implementaton of operator= ?!
You are swapping the role of T and Key in your base type definition inside your wrapper class:
template <typename Key, typename T,
typename HashFcn = std::hash<Key>,
typename EqualKey = std::equal_to<Key>,
typename Alloc = std::allocator<std::pair<const Key, T> > >
class UnmodifiableMap {
public:
// typedef std::unordered_map<T, Key, HashFcn, EqualKey> base; // ERROR
typedef std::unordered_map<Key, T, HashFcn, EqualKey> base; // OK
...
Hence, your underlying map ends up being an unordered_map<string, int> rather than an unordered_map<int, string>, and the compiler complains about the mismatch.
Also notice, that you have an unnecessary static_cast in your constructor:
UnmodifiableMap(const base& other)
// : _map(static_cast<const base&>(other)) {} // NOT NEEDED. Just do:
: _map(other) {}