我有一个模板类,Iterable
;为此我想超载begin()
和end()
免费功能。它将数据存储为的unique_ptr
,但为方便起见,界面使用boost::indirect_iterator
。模板上的begin()和end()自由函数超载
我的代码在CLang-3.5
下生成并运行,但我尝试了g++-4.9
,但它没有。但我不知道为什么? (哪个编译器有正确的行为)。
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);
};
和自由的功能:
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)
{
return 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)
{
return boost::indirect_iterator<typename Container<std::unique_ptr<T>>::iterator>(end(i._c));
}
在g++
,错误的是:
../../API/net/session/ClientSession.h:83:29: error: call of overloaded 'begin(GroupManager&)' is ambiguous
for(auto& grp : groups())
^
../../API/net/session/ClientSession.h:83:29: note: candidates are:
In file included from ../../API/net/session/../permission/full/PermissionManager.h:5:0,
from ../../API/net/session/Session.h:3,
from ../../API/net/session/ClientSession.h:2,
from ../../API/net/session/ClientSessionBuilder.h:2,
from ../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)
^
所以它看起来像g++
看到friend
声明为其他功能?
@Yakk oops。固定。该消息现在也更具描述性。虽然,OP的问题可能更多的是模板函数,而不是自由函数的类内定义。我的+1与其他答案 – sehe 2014-10-20 19:12:37
接下来的问题是,OP的两个版本如何不相同?而OP的“朋友”没有身体,你的确如此。 – Yakk 2014-10-20 19:51:57
@Yakk。叹。 [我搞砸了](http://coliru.stacked-crooked.com/a/606a4d268719ca09),没有我:| |你是对的。除了提示内联定义之外,我没有解释任何这个谜团。将在早上删除。干杯 – sehe 2014-10-20 21:24:19