为 stl 容器专门化成员函数

Specialize member function for stl container

我有一个 class 这样的:

class Foo
{

    ...
    template<template<typename...> class container>
    void fillContainer(container<int> &out)
    {
        //add some numbers to the container
    }
    ...

}

我这样做是为了能够处理不同的 stl 容器。现在我想为 std::vector 创建一个特化来保留内存(我知道要插入的数字量)。我阅读了 this and this post,所以我做了以下操作:

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
    }

    ...
};

(在一些模糊的情况下它会有所不同,例如如果有人想要获取函数模板版本的地址,但没有什么需要专门的,而不是更简单的重载方法.)