Наличие шаблона:
template <typename T, template <typename ELEM, typename ALLOC=std::allocator<ELEM> > class Cont=std::vector>
class VehiclesContainer {
public:
VehiclesContainer(std::initializer_list<T> l):container(l){};
virtual ~VehiclesContainer(){};
virtual void addVehicle(T elem);
virtual T getFirst() const;
template
<typename U, template <typename ELEM2, typename ALLOC=std::allocator<ELEM2> > class Cont2>
friend std::ostream& operator<<(std::ostream& out, const VehiclesContainer<U,Cont2>& obj);
private:
Cont<T> container;
};
У меня есть оператор ‹‹ в качестве класса друга:
template
<typename T,template <typename ELEM,typename ALOC=std::allocator<ELEM> > class Cont>
std::ostream& operator<<(std::ostream& out,const VehiclesContainer<T,Cont>& obj){
typename Cont<T>::const_iterator it;
for(it=obj.container.begin(); it!=obj.container.end(); ++it)
out << *it << " ";
return out;
}
Что я хочу сделать, так это иметь специализацию для этой функции для целых чисел, в которой вместо пробела между элементами на выходе будет -
. Я старался
template
<int,template <typename ELEM,typename ALOC=std::allocator<ELEM> > class Cont>
std::ostream& operator<<(std::ostream& out,const VehiclesContainer<int,Cont>& obj){
typename Cont<int>::const_iterator it;
for(it=obj.container.begin(); it!=obj.container.end(); ++it)
out << *it << "-";
return out;
}
Но когда я компилирую, имея в основном
VehiclesContainer<int,std::vector > aStack1({10,20,30});
std::cout << aStack1;
Общая форма оператора ‹‹ называется вместо моей специализации. Я полагаю, что я действительно не специализировался на этом. Любая помощь, как можно объявить специализацию для класса друзей?
Решение основано на ответе WhozCraig
Предварительное объявление:
template <typename T, template <typename ELEM, typename ALLOC=std::allocator<ELEM> > class Cont=std::vector>
class VehiclesContainer;
template <typename T, template <typename ELEM, typename ALLOC=std::allocator<ELEM> > class Cont>
std::ostream& operator<< (std::ostream& out, const VehiclesContainer<T,Cont>& obj);
template <template <typename ELEM, typename ALLOC=std::allocator<ELEM> > class Cont>
std::ostream& operator<< (std::ostream& out, const VehiclesContainer<int,Cont>& obj);
Объявление внутри класса:
friend std::ostream& operator << <T,Cont>(std::ostream&,
const VehiclesContainer<T,Cont>&);
friend std::ostream& operator << <Cont>(std::ostream&,
const VehiclesContainer<int,Cont>&);
Определение функций друга:
template <typename T, template <typename ELEM, typename ALLOC=std::allocator<ELEM> > class Cont>
std::ostream& operator <<(std::ostream& os, const VehiclesContainer<T,Cont>& obj)
{
if (obj.container.size() > 0)
{
os << obj.container.front();
for (auto it = std::next(obj.container.begin()); it != obj.container.end(); ++it)
os << ' ' << *it;
}
return os;
}
template <template <typename ELEM, typename ALLOC=std::allocator<ELEM> > class Cont>
std::ostream& operator << (std::ostream& os, const VehiclesContainer<int,Cont>& obj)
{
if (obj.container.size() > 0)
{
os << obj.container.front();
for (auto it = std::next(obj.container.begin()); it != obj.container.end(); ++it)
os << '-' << *it;
}
return os;
}