To test and display the result of some functions of my library, I am creating a set of handy functions.
I have an execute
function that looks like :
template <typename R, typename I>
std::string execute( const std::string& func_name, R(*func_ptr)( const I& ), const I& func_input );
It calls the function, and display the results and arguments in a formatted string that I can send to std::cout
.
The problem is that some of my functions do not return convertible-to-string results. I thought I could simply overload the global ::operator std::string
with something like:
template <typename T>
operator std::string( const std::vector<T>& v );
But GCC complains:
error: 'operator std::string(const std::vector<T, std::allocator<_CharT> >&)' must be a nonstatic member function
Well, the problem of course is that I cannot add member operators to std::vector
, and even for my classes, I don't want to pollute them with "for testing" conversion operators.
I guess that I can add a layer of indirection and use a function instead of a conversion operator, but that would not be the more aesthetic solution. I could also overload ::operator <<
for std::ostream
and use a std::ostringstream
, but that also is not the cleanest solution.
I wondered if the global conversion operator is really not overloadable, and if so, why.