With built in (non-class) types, it is not possible to prevent unwanted implicit type conversions.
Some compilers can be configured to give warnings for operations involving suspicious conversions, but that does not cover all possible implicit conversions (after all, a conversion from short
to long
is value preserving, so not all compilers will report it as suspicious). And some of those compiles may also be configured to give errors where they give warnings.
With C++ class types, it is possible to prevent implicit conversions by making constructors explicit
, and not defining conversion operators (for example, a class member function named operator int()
).
It is also possible for a class type to supply numeric operators (operator+()
, etc), which only accept operands of the required types. The problem is that this doesn't necessarily prevent promotion of built in types participating in such expressions. For example, a class that provides an operator+(int) const
(so some_object = some_other_object + some_int
would work) would not stop an expression like some_other_object + some_short
from compiling (as some_short
can be implicitly promoted to int
).
Which basically means it is possible to prevent implicit conversions to class types, but not prevent promotions occurring in expressions with numeric operators.