3
#include <vector>

using std::vector;

template<template<typename> class x>
void test() {
    x<int> a;
    a.push_back(1);
}

int main() {
    test<vector>();
    return 0;
}

For some reason, I get no matching call error, despite my expectations. Why does this happen?

RomaValcer
  • 2,786
  • 4
  • 19
  • 29

2 Answers2

4

std::vector does not take one argument, but two (there is Allocator), so it cannot be matched against a template that should take only one. You need to change to either:

template <template <typename, typename> class x>
// or:
template <template <typename... > class x>

You also need to change the return type of your function because it is unlikely that x<int> is void.

Note that if you use the version with two typename, you would need to specify each parameters in the return statement (e.g. x<int, std::allocator<int>>), which is why you should prefer the variadic version (typename...).

Holt
  • 36,600
  • 7
  • 92
  • 139
2

std::vector template looks as follows:

template<
    class T,
    class Allocator = std::allocator<T>
> class vector;

so you have typename T and allocator for it, so the correct code should be:

template<template<typename,typename> class x>
void test() {
    x<int, std::allocator<int>>();
}
marcinj
  • 48,511
  • 9
  • 79
  • 100