Better way of enforcing this template?

Posted by Dennis Ritchie on Stack Overflow See other posts from Stack Overflow or by Dennis Ritchie
Published on 2012-12-14T22:34:48Z Indexed on 2012/12/14 23:03 UTC
Read the original article Hit count: 219

Filed under:

Currently, I have a function template like this that converts a vector into a string (just a natural string, separating the elements with a comma):

//the type T must be passable into std::to_string
template<typename T>
std::string vec_to_str(const std::vector<T> &vec);

As you can see, this is only meant for vectors whose elements can be passed into the built-in std::to_string function (such as int, double, etc.)

Is it considered a good practice to document with comments the allowed T? If not, what should I do? Is it possible to enforce this in a better way?

© Stack Overflow or respective owner

Related posts about c++