[英]Default argument for STL container in templated function
I have a templated function of the form 我有形式的模板功能
template <typename T>
void my_fct( vector<T> ){...}
and would like to provide a default argument, such that the my_fct() can also be called. 并希望提供一个默认参数,以便也可以调用my_fct()。 Clearly, the compiler does not know what type "T" is in that case, but is there some way to give it a default type in that case?
显然,在这种情况下,编译器不知道“ T”是什么类型,但是在这种情况下,是否可以通过某种方式为其提供默认类型?
I tried to pass an empty vector of type double 我试图传递类型为double的空向量
template <typename T>
void my_fct( vector<T> = vector<double> ){...}
but this does not work. 但这不起作用。
Thanks! 谢谢!
Make T
a default argument: 将
T
设为默认参数:
template <typename T = double>
void my_fct( vector<T> = vector<T>() ) {...}
So when the user calls the function with zero arguments, the type of the vector will be std::vector<double>
initialized as a default argument. 因此,当用户使用零参数调用函数时,向量的类型将被初始化为
std::vector<double>
作为默认参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.