ulf
ulf

Reputation: 190

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. 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?

I tried to pass an empty vector of type double

template <typename T>
void my_fct( vector<T> = vector<double> ){...}

but this does not work.

Thanks!

Upvotes: 0

Views: 126

Answers (1)

David G
David G

Reputation: 96790

Make T a default argument:

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.

Upvotes: 1

Related Questions