c++ - 部分模板模板 vector 特化

标签 c++ templates c++14 partial-specialization

我有一个处理不同容器的通用函数。

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/

相关文章:

c++ - 将大型单 block 单线程应用程序转换为多线程体系结构的建议?

c++ - Google re2 namedcapture,如何在 C++ 中解析结果?

c++ - 不使用这个的 constexpr 成员函数?

c++ - operator<< with boost::variant 是如何实现的

c++ - std::reference_wrapper 不能很好地与 std::bind 一起工作

c++ - 调用删除器时 shared_ptr 是否仍然拥有它的对象?

c++ - 使用enable_if来防止声明?

c++ - 如何在c++中包含头文件?

模板破坏 http HEAD 方法

c++ - 如何 static_assert 给定的函数调用表达式是否可以编译?