Как объявить специализацию функции друга шаблона

Наличие шаблона:

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;
}

person Avraam Mavridis    schedule 15.02.2014    source источник


Ответы (2)


Это один из способов сделать то, что вы хотите. Я позволил себе использовать вариативные параметры в параметрах шаблона, чтобы сэкономить время на наборе текста, но в конечном счете предпосылка должна быть очевидной:

#include <iostream>
#include <vector>
#include <cstdlib>

// forward declaration of template class
template <class T, template<class, class...> class Cont = std::vector, class... Args>
class VehiclesContainer;

// generic template for all T and all container types
template<class T, template<class, class...> class Cont = std::vector, class... Args>
std::ostream& operator <<(std::ostream&, const VehiclesContainer<T,Cont,Args...>&);

// specific template for only int and all container types
template<template<class, class...> class Cont = std::vector, class... Args>
std::ostream& operator << (std::ostream& os, const VehiclesContainer<int,Cont,Args...>& obj);


template <class T, template<class, class...> class Cont, class... Args>
class VehiclesContainer
{
public:
    VehiclesContainer(std::initializer_list<T> l)
        : container(l)
    {};

    // friend overloaded to `int` type
    friend std::ostream& operator << <T,Cont,Args...>(std::ostream&,
                const VehiclesContainer<T,Cont,Args...>&);

    friend std::ostream& operator << <Cont, Args...>(std::ostream&,
                const VehiclesContainer<int,Cont,Args...>&);

private:
    Cont<T,Args...> container;
};

template<class T, template<class, class...> class Cont, class... Args>
std::ostream& operator <<(std::ostream& os, const VehiclesContainer<T,Cont,Args...>& 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<class, class...> class Cont, class... Args>
std::ostream& operator << (std::ostream& os, const VehiclesContainer<int,Cont,Args...>& 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;
}


int main()
{
    VehiclesContainer<std::string> vcString { "Camero", "Corvette" };
    VehiclesContainer<int> vcInt { 1,2,3 };

    std::cout << vcString << '\n';
    std::cout << vcInt << '\n';

    return 0;
}

Вывод

Camero Corvette
1-2-3
person WhozCraig    schedule 15.02.2014
comment
Я попытался сделать что-то подобное, имея встроенные общие friend std::ostream& operator<<(std::ostream& out, const VehiclesContainer<T,Cont>& obj) и friend std::ostream& operator<<(std::ostream& out, const VehiclesContainer<int,Cont>& obj). Но компилятор жалуется на переопределение. - person Avraam Mavridis; 15.02.2014
comment
@Avraam Какой компилятор, если не возражаете, если я спрошу? Вышеизложенное дословно не работает для вас? - person WhozCraig; 15.02.2014
comment
Я использую g++ в Windows с wingw, я не пробовал именно ваш код, я просто объявляю два встроенных friend operator<<, как вы (опуская вариации), только для проверки, но компилятор жалуется на переопределение. Я попробую ваш код и сообщу вам. Спасибо. - person Avraam Mavridis; 15.02.2014
comment
Хорошо, я обновил его для чего-то, что, как я знаю, совместимо с g++ 4.7.2, скомпилированным с -std=c++11, так что, надеюсь, это сработает и для вас. Удачи вам. - person WhozCraig; 15.02.2014

Я думаю, что правильный способ сделать это таков:

template
<template <typename ELEM2, typename ALLOC=std::allocator<ELEM2> > class Cont2>
friend std::ostream& operator<<(std::ostream& out, const VehiclesContainer<int,Cont2>& obj);

Вы должны сделать это до объявления друга, иначе, конечно, это будет приватным.

template
<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;
}
person Community    schedule 15.02.2014
comment
Я уже пробовал это и не компилируется. Существует целый список ошибок компиляции. - person Avraam Mavridis; 15.02.2014