我有这样一个类:
class Foo
{
...
template<template<typename...> class container>
void fillContainer(container<int> &out)
{
//add some numbers to the container
}
...
}
我这样做是为了能够处理不同的 STL 容器。现在我想为 std::vector 创建一个专门化来保留内存(我知道要插入的数字数量)。我读了this和 this发布,所以我做了以下事情:
class Foo
{
//Same Thing as above
}
template<>
void Foo::fillContainer(std::vector<int> &out)
{
//add some numbers to the container
}
现在我收到错误:error: no member function 'fillContainer' declared in 'Foo'
.我想问题是 template<template<typename...> class container>
.
是否有可能为 std::vector
专门化此功能? ?
最佳答案
没有理由尝试专门化它,只需添加一个重载:
class Foo
{
...
template<template<typename...> class container>
void fillContainer(container<int>& out)
{
//add some numbers to the container
}
void fillContainer(std::vector<int>& out)
{
//add some numbers to the container
}
...
};
(在一些模糊的情况下它会有所不同,例如如果有人想要获取函数模板版本的地址,但没有什么需要专门的,而不是更简单的重载方法。)
关于c++ - 为 STL 容器专门化成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31582826/