Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent an implicit cast double -> int?

Tags:

c++

casting

Question as above, more details below:

I have a class Money to deal with... well, you guessed what. I am very strict about not allowing Money and double to interact(*), so the following code is not possible:

Money m1( 4.50 );
double d = 1.5;
Money m2 = m1 * d; // <-- compiler error

Now I'm thinking about allowing multiplication of Money with int, as in "you have 6 pieces of cake for $4.50 each (so go and find cheaper cake somewhere)."

class Money
{
    Money();
    Money( const Money & other );
    explicit Money( double d );
    ...
    Money & operator*=( int i );
    ...
}
inline const Money operator*( const Money & m, int i ) { return Money( m ) *= i; }
inline const Money operator*( int i, const Money & m ) { return Money( m ) *= i; }

That works fine, but... unfortunately, C++ does implicit casts from double to int, so suddenly my first code snippet will compile. I don't want that. Is there any way to prevent implicit casts in this situation?

Thanks! -- Robin

(*) Reason: I have lot's of legacy code that handles all Money-related stuff with double, and I don't want those types confused until everything run with Money.

Edit: Added constructors for Money.

Edit: Thanks, everyone, for your answers. Almost all of them were great and helpful. R. Martinho Fernandes' comment "you can do inline const Money operator*( const Money & m, double d ) = delete;" was actually the answer (as soon as I switch to a C++11-supporting compiler). Kerrek SB gave a good none-C++11 alternative, but what I ended up with using is actually Nicola Musatti's "overload long" approach. That's why I'm flagging his answer as "the answer" (also because all the useful ideas came up as comments to his answer). Again, thanks!

like image 882
Robin Avatar asked Oct 26 '11 13:10

Robin


People also ask

Can int be implicitly cast to double?

We can convert int to double in java using assignment operator. There is nothing to do extra because lower type can be converted to higher type implicitly. It is also known as implicit type casting or type promotion.

How do you avoid implicit conversions in C++?

Keyword explicit tells compiler to not use the constructor for implicit conversion. For example declaring Bar's constructor explicit as - explicit Bar(int i); - would prevent us from calling ProcessBar as - ProcessBar(10); .

What is an implicit cast in C++?

The word “implicit” means 'understood' or 'embedded'. In implicit C++ type casting, the data type in which the value is to be converted is not specified in the program. It is automatically done by the C++ compiler.

What happens in implicit conversion?

An implicit conversion sequence is the sequence of conversions required to convert an argument in a function call to the type of the corresponding parameter in a function declaration. The compiler tries to determine an implicit conversion sequence for each argument.


1 Answers

How about a template plus compile-time trait check:

#include <type_traits>

// ...

template <typename T>
Money & operator*=(const T & n)
{
  static_assert(std::is_integral<T>::value, "Error: can only multiply money by integral amounts!");
  // ...
}
like image 155
Kerrek SB Avatar answered Oct 13 '22 01:10

Kerrek SB