Advertisement
devincpp

std_vector_as_template_arg

Dec 11th, 2024
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. // 这种写法只有GCC可以编译通过
  6. template<typename DataType, template<typename T, typename Alloc = std::allocator<T>> class Container>
  7. void f1(Container<DataType>& c)
  8. {
  9.     for (const auto &ele : c) {
  10.         std::cout << ele << " ";
  11.     }  
  12. }
  13.  
  14. // 这种写法GCC/Clang都可以编译通过
  15. template<typename DataType, template<typename...> class Container>
  16. void f2(Container<DataType>& c)
  17. {
  18.     for (const auto &ele : c) {
  19.         std::cout << ele << " ";
  20.     }  
  21. }
  22.  
  23. int main()
  24. {
  25.     std::vector<int> vec{1, 2, 3};
  26.  
  27.     f1(vec);
  28.     f2(vec);
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement