multi_index_container with protected constructor
Hi, I want to use boost::multi_index_container with a class that have protected constructors, but the compile fails. I think I should make multi_index_container a friend of my class, but I don't know how. #include <iostream> #include <boost/shared_ptr.hpp> #include <boost/multi_index_container.hpp> #include <boost/multi_index/ordered_index.hpp> #include <boost/multi_index/composite_key.hpp> #include <boost/multi_index/mem_fun.hpp> class Item { public: typedef boost::shared_ptr<Item> P; static P create() { return P(new Item()); } static P create(P item) { return P(new Item(item)); } protected: Item() { } Item(P item) : source(item->get_source()) , target(item->get_target()) { } public: std::string source; std::string target; std::string get_source() const {return source;} std::string get_target() const {return target;} struct key : boost::multi_index::composite_key< P, boost::multi_index::const_mem_fun<Item, std::string, &Item::get_source>, boost::multi_index::const_mem_fun<Item, std::string, &Item::get_target> >{}; typedef boost::multi_index_container< P, boost::multi_index::indexed_by< boost::multi_index::ordered_non_unique< key, boost::multi_index::composite_key_result_less<key::result_type> > > > index_t; }; int main() { Item::index_t index; Item::P t1 = Item::create(); index.insert(t1); return 0; } Thanks in advance: Imre Horvath
On 17.7.2013 16:07, Horváth Imre wrote:
Hi,
I want to use boost::multi_index_container with a class that have protected constructors, but the compile fails. I think I should make multi_index_container a friend of my class, but I don't know how.
I guess you don't want to do that (as quite likely you'd like to pass the instance around, ie. P, not create new n copies of Item), but add: explicit
Item(P item) : source(item->get_source()) , target(item->get_target()) { }
and it compiles. Yes, copy constructing via create(P item) works as expected with that trick done. My compiler choked on types too much, so didn't look where it actually tries to construct shared_ptr< Item > and feed that to constructor. But I think this is the main cause anyway and explicit keyword is there to help you out. -- Pekka
Thank you, it works! Best regards: Imre Horvath 2013. 07. 17, szerda keltezéssel 16.47-kor Pekka Seppänen ezt írta:
On 17.7.2013 16:07, Horváth Imre wrote:
Hi,
I want to use boost::multi_index_container with a class that have protected constructors, but the compile fails. I think I should make multi_index_container a friend of my class, but I don't know how.
I guess you don't want to do that (as quite likely you'd like to pass the instance around, ie. P, not create new n copies of Item), but add:
explicit
Item(P item) : source(item->get_source()) , target(item->get_target()) { }
and it compiles. Yes, copy constructing via create(P item) works as expected with that trick done.
My compiler choked on types too much, so didn't look where it actually tries to construct shared_ptr< Item > and feed that to constructor. But I think this is the main cause anyway and explicit keyword is there to help you out.
-- Pekka _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Horváth Imre
-
Pekka Seppänen