Question about partial specialization of a C ++ template

I am having problems compiling with the following code:

  template <typename T, 
            template <class T, class Allocator = std::allocator<T> > class C>
  bool is_in(const C<T>& a, const C<T>& b);

  template <typename T, std::vector> // HERE
  bool is_in(const std::vector<T>& a, const std::vector<T>& b)
  {
    return false; // implementation tbd
  }

...

vector<int> a, b;

cout << is_in(a,b) << endl;

Error message (in the line with the inscription "HERE"):

error: 'std::vector' is not a type

(of course, I included the vector from std!). Any suggestion? I worked a bit with it, but I guess I can use some help :-) I need to partially specialize the declaration of the original template so that I can implement compiler switch implementations depending on the actual type of container C (for sets there will be is_in, one for vectors, one for ranges ... with different algorithms every time).

Thank!

+5
source share
3 answers

.

.

template <typename T> 
bool is_in(const std::vector<T>& a, const std::vector<T>& b)
{
  return false; // implementation tbd
}

. .

:

namespace detail
{
    template<typename T, typename C>
    struct S
    {
        static bool impl(const C & a, const C & b)
        {
            //primary template
            //...
        }
    }
    template<typename T>
    struct S<T, std::vector<T> >
    {
        static bool impl(const std::vector<T> & a, const std::vector<T> & b)
        {
            //partial specialization for std::vector
            return false;
        }
    }
}

template <typename T,  template <class T, class Allocator = std::allocator<T> > class C>
bool is_in(const C<T>& a, const C<T>& b)
{
   return detail::S<T, C<T> >::impl(a,b);
}
+6

. , . :

template <typename T>
bool is_in(const std::vector<T>& a, const std::vector<T>& b)
{
    return false; // implementation tbd
}

, :

template <typename T> // std::vector is not a template parameter,
                      // so we wouldn't put it here
bool is_in<T, std::vector>(const std::vector<T>& a, const std::vector<T>& b)
// instead, it'd appear ^ here, when we're specializing the base template
{
    return false; // implementation tbd
}
+1

, ( ), ,

template <typename T>
bool is_in(const std::vector<T>& a, const std::vector<T>& b)
{
    ...
}

.

EDIT: , . , , .

0
source

All Articles