我有一个处理不同容器的通用函数。
template<template<class, class> class C, class T, class A>
void handle(C<T, A> const& c)
{
cout << "General handling\n";
}
现在,如果我将自定义容器传递给它,我希望它做出不同的 react 。
为简单起见,我首先尝试以单独的方式处理 vector ,尝试将此函数部分专门化为 vector 。
这就是我认为它应该是什么样子。
template<class T, class A>
void handle<std::vector>(std::vector<T, A> const& c)
{
cout << "vector handling\n";
}
然而gcc给出以下错误:
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) |
这可以通过部分模板特化来完成吗?
最佳答案
函数模板不能是 partial specialized ;仅适用于类模板和变量模板(C++14 起)。您可以申请 function template overloading反而。
例如
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";
}
关于c++ - 部分模板模板 vector 特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60599038/