More Effective C++: 35 New Ways to Improve Your Programs and Designs

Category: Programming
Author: Scott Meyers
This Month Stack Overflow 5


by anonymous   2019-07-21

In theory, you could add implicit conversion operators to one or both libraries (although implicit conversions aren't necessarily a good idea). But you've said you can't edit either libary, so that rules that out.

So one alternative would be to introduce your own vector class, and supply it with the necessary conversion operators. Then whenever you need a vector in your code, you always store it as an object of your custom class, and (implicitly) convert when you need to use the libraries.

But again, the trouble that implicit conversion can cause might outweigh the superficial benefits (see Item 5 of More Effective C++ for more details on this).

by anonymous   2019-07-21

Yes, you can overload type-conversion operators:

class Rofl {
    operator float() const { return b; }


See for a demo.

However, see Item 5 of Scott Meyers' More Effective C++, entitled "Be wary of user-defined conversion operations". Allowing implicit conversions to-and-from complex types can often lead to all sorts of subtle typo bugs.

by anon   2019-07-21

There is no way of doing this - and if you need to do it, there is something wrong with your design. There is a discussion of why you can't do this in More Effective C++.

by anonymous   2019-07-21

Yes, that is allowed. But general practice is to avoid concrete base classes of any sort (see e.g. Item 33 of Scott Meyers' More Effective C++).

Attempting to persist complex objects by storing their binary representation is bound to lead to all sorts of problems; I would suggest finding a better mechanism (serialization, etc.).