Accept any stl container as argument but require type
Hi,
I recently learned that you can accept any container by using a template like so:
1 2 3 4 5
|
template <typename T>
void SomeFunc(T container)
{
container.insert(container.end(), 1);
}
|
But i was wondering: is there a way to "require" that container to be of a specific type itself?
something like
template <typename T<int>>
?
Last edited on
This code won't compile until you take out the attempt to use SomeFunc with a container of not-int.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <vector>
#include <list>
using namespace std;
template <typename T>
void SomeFunc(T container)
{
static_assert(std::is_same<typename T::value_type, int>::value,
"Container must hold int values");
container.insert(container.end(), 1);
}
int main()
{
vector<int> vec;
SomeFunc(vec);
list<float> list2;
SomeFunc(list2);
}
|
Topic archived. No new replies allowed.