Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

partial template template vector specialization

I have a general function that handles different containers.

template<template<class, class> class C, class T, class A>
void handle(C<T, A> const& c)
{
    cout << "General handling\n";
}

Now I would like it to react differently if I pass it a custom container. For simplicity's sake I first tried it with handling a vector in a separate way by trying to partially specialize this function for a vector.
This is what I thought it should look like.

template<class T, class A>
void handle<std::vector>(std::vector<T, A> const& c)
{
    cout << "vector handling\n";
}

However gcc gives the following error:

Could not execute the program Compiler returned: 1 Compiler stderr :16:36: error: template-id 'handle class std::vector>' in declaration of primary template 16 | (std::vector const& c) |

Can this be done with partial template specialization?

like image 729
turoni Avatar asked Dec 23 '22 18:12

turoni


1 Answers

Function templates can't be partial specialized; which only works with class templates and variable templates (since C++14). You can apply function template overloading instead.

e.g.

template<template<class, class> class C, class T, class A>
void handle(C<T, A> const& c)
{
    cout << "General handling\n";
}

template<class T, class A>
void handle(std::vector<T, A> const& c)
{
    cout << "vector handling\n";
}
like image 164
songyuanyao Avatar answered Dec 25 '22 23:12

songyuanyao