小编Sun*_*tar的帖子

How to get the data type in a vector using template?

I'm writing a function template like this:

template <class T>
class MyClass() {}

template <typename A, typename B>
auto func(A<B>& data) {
    return MyClass<B>();
}
Run Code Online (Sandbox Code Playgroud)

So I can using the function like this:

vector<int> vi;    auto a = func(vi);
vector<string> vs;    auto b = func(vs);
list<int> li;    auto c = func(li);
list<string> ls;    auto d = func(ls);
Run Code Online (Sandbox Code Playgroud)

But obviously it's not allowed. How should I write a template to reach my goal?

c++ templates stl

3
推荐指数
1
解决办法
52
查看次数

标签 统计

c++ ×1

stl ×1

templates ×1