Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading the global type conversion operator

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.

like image 521
NewbiZ Avatar asked Feb 10 '10 07:02

NewbiZ


People also ask

What is the operator overloading and type conversion?

Operator Overloading is the method by which we can change the function of some specific operators to do some different task. In the above syntax Return_Type is value type to be returned to another object, operator op is the function where the operator is a keyword and op is the operator to be overloaded.

What is operator overloading in C++?

Operator Overloading in C++ This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. For example, we can overload an operator '+' in a class like String so that we can concatenate two strings by just using +.

What is C++ conversion operator?

Conversion Operators in C++ C++ supports object oriented design. So we can create classes of some real world objects as concrete types. Sometimes we need to convert some concrete type objects to some other type objects or some primitive datatypes. To make this conversion we can use conversion operator.

What is type conversion in C++ with example?

Example 1: Conversion From int to double In the program, we have assigned an int data to a double variable. num_double = num_int; Here, the int value is automatically converted to double by the compiler before it is assigned to the num_double variable. This is an example of implicit type conversion.


2 Answers

Conversion operators (cast operators) must be a member of the convertible class that produces the converted type. As assignment operators, they must be member functions, as your compiler is telling you.

Depending on how much effort you want to put into the debug part of it, you could try to use metaprogramming to forward your execute method to different actual implementations, providing specific ones for containers that will print the contents.

Why don´t you want to provide operator<< for your types? I think that is actually the idiomatic solution. Unlike other languages that you use methods that convert to string to produce printable results, in C++ the idiomatic way is providing operator<< and then using stringstreams (or boost::lexical_cast or some similar solution) to convert to strings based on the operator<< implementation. There is a simple utility class here to create a string from elements that override operator<< if you want to use that for a start point.

like image 147
David Rodríguez - dribeas Avatar answered Oct 06 '22 03:10

David Rodríguez - dribeas


I wondered if the global conversion operator is really not overloadable, and if so, why.

No, there is no such thing. Conversion functions must be a member of a class. If it weren't so, it would make overload resolution a particularly vexing problem for the compiler by introducing ambiguities.

like image 28
dirkgently Avatar answered Oct 06 '22 03:10

dirkgently