【发布时间】:2018-11-30 19:11:23
【问题描述】:
我有一个模板如下:
template <class T>
vector<T> read_vector(int day)
{
vector<T> the_vector;
{...}
return the_vector;
}
我希望能够做类似的事情
vector<int> ints = read_vector(3);
vector<double> doubles = read_vector(4);
C++ 模板是否可以从它们被调用时推断出返回类型,或者我应该将一个虚拟参数传递给具有我希望向量具有的类型的模板?后者有效,但更混乱。
【问题讨论】:
-
不行,这种情况下需要指定类型
read_vector<int>。编译器无法推断返回类型。
标签: c++ templates generic-programming