c++11 - begin() and end() free function overload on template -
i have templated class, iterable
; want overload begin()
, end()
free functions. stores info vector
of unique_ptr
, interface uses boost::indirect_iterator
convenience.
my code builds , runs under clang-3.5
, tried on g++-4.9
, did not. not know why ? (and compiler has right behaviour).
template<typename t> using simplevec = std::vector<t, std::allocator<t>>; template <typename t, template <typename> class container = simplevec, class string = std::string> class iterable { template <typename friendt, template <typename> class friendcontainer, class friendstring> friend boost::indirect_iterator<typename friendcontainer<std::unique_ptr<friendt>>::iterator> begin(iterable<friendt, friendcontainer, friendstring>& i); template <typename friendt, template <typename> class friendcontainer, class friendstring> friend boost::indirect_iterator<typename friendcontainer<std::unique_ptr<friendt>>::iterator> end(iterable<friendt, friendcontainer, friendstring>& i); };
and free functions :
template <typename t, template <typename> class container = simplevec, class string = std::string> boost::indirect_iterator<typename container<std::unique_ptr<t>>::iterator> begin(iterable<t, container, string>& i) { homecoming boost::indirect_iterator<typename container<std::unique_ptr<t>>::iterator>(begin(i._c)); } template <typename t, template <typename> class container = simplevec, class string = std::string> boost::indirect_iterator<typename container<std::unique_ptr<t>>::iterator> end(iterable<t, container, string>& i) { homecoming boost::indirect_iterator<typename container<std::unique_ptr<t>>::iterator>(end(i._c)); }
on g++
, error :
../../api/net/session/clientsession.h:83:29: error: phone call of overloaded 'begin(groupmanager&)' ambiguous for(auto& grp : groups()) ^ ../../api/net/session/clientsession.h:83:29: note: candidates are: in file included ../../api/net/session/../permission/full/permissionmanager.h:5:0, ../../api/net/session/session.h:3, ../../api/net/session/clientsession.h:2, ../../api/net/session/clientsessionbuilder.h:2, ../client/main.cpp:2: ../../api/net/session/../permission/full/../../iterable.h:22:4: note: boost::indirect_iterator<typename friendcontainer<std::unique_ptr<friendt> >::iterator> begin(iterable<friendt, friendcontainer, friendstring>&) [with friendt = group; friendcontainer = simplevec; friendstring = std::basic_string<char>; t = permission; container = simplevec; string = std::basic_string<char>; typename friendcontainer<std::unique_ptr<friendt> >::iterator = __gnu_cxx::__normal_iterator<std::unique_ptr<group, std::default_delete<group> >*, std::vector<std::unique_ptr<group, std::default_delete<group> >, std::allocator<std::unique_ptr<group, std::default_delete<group> > > > >] begin(iterable<friendt, friendcontainer, friendstring>& i); ^ ../../api/net/session/../permission/full/../../iterable.h:142:2: note: boost::indirect_iterator<typename container<std::unique_ptr<_tp> >::iterator> begin(iterable<t, container, string>&) [with t = group; container = simplevec; string = std::basic_string<char>; typename container<std::unique_ptr<_tp> >::iterator = __gnu_cxx::__normal_iterator<std::unique_ptr<group, std::default_delete<group> >*, std::vector<std::unique_ptr<group, std::default_delete<group> >, std::allocator<std::unique_ptr<group, std::default_delete<group> > > > >] begin(iterable<t, container, string>& i) ^
so looks g++
sees friend
declaration function ?
i managed find reply (which much more readable) @sehe's reply , this cppreference page.
template <typename t, template <typename> class container = simplevec, class string = std::string> class iterable { friend boost::indirect_iterator<typename container<std::unique_ptr<t>>::iterator> begin(iterable& i) { homecoming boost::indirect_iterator<typename container<std::unique_ptr<t>>::iterator>(begin(i._c)); } friend boost::indirect_iterator<typename container<std::unique_ptr<t>>::iterator> end(iterable& i) { homecoming boost::indirect_iterator<typename container<std::unique_ptr<t>>::iterator>(end(i._c)); } };
templates c++11 boost c++14 free-function
No comments:
Post a Comment