2

我需要实现一个接受任何 STL 容器的模板函数。并且基于什么样的容器来执行某些动作。

例子:

template <class Container, class T>
void func(Container<T> container) {
    if (container == std::map) {
        ...
    } else {
        ... 
    }
}


int main() {
    std::vector<int> v1; 
    func(v1); // ok
    std::vector<double> v2;
    func(v2); // ok
    std::map<int, double> m1;
    func(m1); // ok
    std::list<int> l1;
    func(l1); // ok
}
4

2 回答 2

6

您可以应用Constexpr If (C++17 起) (with std::is_same) 在编译时检查类型,并应用参数包(C++11 起),因为这些容器采用多个模板参数。例如

template <template <typename...> class Container, class... T>
void func(const Container<T...>& container) {
    if constexpr (std::is_same_v<Container<T...>, std::map<T...>>) {
        ...
    } else if constexpr (std::is_same_v<Container<T...>, std::vector<T...>>) {
        ...
    } else if constexpr (std::is_same_v<Container<T...>, std::list<T...>>) {
        ...
    } else {
        ...
    }
}

PS:这取决于您的意图,但将参数更改为 pass-by-reference-to-const 以避免不必要的复制可能是一个好主意。

于 2020-05-27T08:29:08.627 回答
2

由于无论如何您要为每个容器制作一个实现,因此您可以直接进行重载。它将在 C++11 中工作,并且具有在每个重载中轻松使用模板参数的好处。

template <class T, size_t N>
void func(const std::array<T,N>& c) {
    std::cout << "array " << c.size() << '\n';
}

template <class T, class Alloc>
void func(const std::vector<T,Alloc>& c) {
    std::cout << "vector " << c.size() << '\n';
}

template <class T, class Alloc>
void func(const std::list<T,Alloc>& c) {
    std::cout << "list " << c.size() << '\n';
}

template <class Key, class T, class Comp, class Alloc>
void func(const std::map<Key,T,Comp,Alloc>& c) {
    std::cout << "map " << c.size() << '\n';
}

template <class CharT, class Traits, class Alloc>
void func(const std::basic_string<CharT,Traits,Alloc>& c) {
    std::cout << "basic_string " << c.size() << '\n';
}

// add more of the containers you aim to support here
于 2020-05-27T10:09:11.010 回答