Rule A13-5-3 (advisory, implementation, automated)

User-defined conversion operators should not be used.

Rationale

Explicitly named conversions using dedicated member function eliminate any potential errors that can arise if the type conversion operators have to be used. If using conversion operators is fundamental in an application domain, see A13-5-2.

Example

// $Id: A13-5-3.cpp 305629 2018-01-29 13:29:25Z piotr.serwa $
#include <iostream>

class Complex
{
public:
Complex (double r, double i = 0.0) : re(r), im(i) {}
explicit operator double() const noexcept { return re; }
double AsDouble() const noexcept { return re; }
private:
double re;
double im;

};

int main(void)
{
Complex c(2.0f);

std::cout << (double) c << std::endl; //compliant with A13-5-2, non-compliant with A13-5-3

std::cout << c.AsDouble() << std::endl; //compliant

return 0;
}

See also

JSF December 2005 [8]: AV Rule 177: User-defined conversion functions should be avoided. C++ Core Guidelines [11]: C.164: Avoid conversion operators.