473,238 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 software developers and data experts.

Implicit conversion is evil?

In a newer version of a chess program I am writing, I have created classes
that are (more or less) drop in replacements for things that used to be
plain old integer or enumerated variables (colors, piece types, squares,
etc.). To accomplish this, I used implicit conversions. For instance, a
color used to be:

typedef int Color;
// and a few constants...

Now a color is (paraphrased):

class Color {
private:
int color;
public:
// ...
operator int () { return color; }
// ...
};

The reason for moving to these simple classes (which are just int wrappers)
was so that I could verify the validity of the data. For instance:

operator int () { assert(color == white || color == black); return color; }

However, I've been told by several people that implicit conversions are
"evil". I can see how they could be the source of very nasty bugs for a more
hefty class, but what about for something as simple as these classes which
are nothing more than int wrappers? The way I'm using them, they kind of
_are_ int's, so how could it cause bugs if there is an implicit conversion?

Would it be possible for an instance of Color to be implicitly converted and
not validate its internals any longer? If that is possible, I could foresee
a potentially hard to find bug. That would give a sense of false security,
and I'd probably look for the bug elsewhere, assuming (incorrectly) that my
color was still valid since the assert didn't fail.
Jul 19 '05 #1
2 3050
"Russell Reagan" <rr*****@attbi.com> wrote...
In a newer version of a chess program I am writing, I have created classes
that are (more or less) drop in replacements for things that used to be
plain old integer or enumerated variables (colors, piece types, squares,
etc.). To accomplish this, I used implicit conversions. For instance, a
color used to be:

typedef int Color;
// and a few constants...

Now a color is (paraphrased):

class Color {
private:
int color;
public:
// ...
operator int () { return color; }
// ...
};

The reason for moving to these simple classes (which are just int wrappers) was so that I could verify the validity of the data. For instance:

operator int () { assert(color == white || color == black); return color; }
However, I've been told by several people that implicit conversions are
"evil". I can see how they could be the source of very nasty bugs for a more hefty class, but what about for something as simple as these classes which
are nothing more than int wrappers? The way I'm using them, they kind of
_are_ int's, so how could it cause bugs if there is an implicit conversion?
Would it be possible for an instance of Color to be implicitly converted and not validate its internals any longer? If that is possible, I could foresee a potentially hard to find bug. That would give a sense of false security,
and I'd probably look for the bug elsewhere, assuming (incorrectly) that my color was still valid since the assert didn't fail.


Let me start with this statement: no feature in the language is
"evil" per se. Implicit conversions are features of the language,
therefore they are not "evil" by themselves. They, just like many
other features of the language, carry some responsibility and can
be mis-used.

Let me continue with this question: there is nothing in your post
that demonstrates the need to have the conversion operator. Do you
use the value to do arithmetic? If not, why would you need to have
the conversion to an arithmetic type? Do you use those "Color",
"Piece", etc., types to index anything? Then usually you don't
need to convert to int in those types, you _could_ limit such
conversion to the module (function, method) where the numeric
value of an object is used. In all other places it should be enough
to use the enumerated values (symbolic constants).

As soon as you can answer this question, you will be on your way to
better understanding of the conversion necessities and probably that
they are unneeded in your model.

Victor
Jul 19 '05 #2
"Victor Bazarov" <v.********@comAcast.net> wrote
Do you use those "Color",
"Piece", etc., types to index anything?
Yes. I use colors and piece types to index piece lists by color and piece
type, and squares are nothing but indexes into the board.

you _could_ limit such
conversion to the module (function, method) where the numeric
value of an object is used.


I've been considering writing an explicit conversion member function and
letting the other functions call it as needed. This is probably best since
there really isn't any need for implicit conversion, especially at the costs
of unforeseen bugs. The implicit conversion was more of a syntactic sugar
than anything.
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Christophe Poucet | last post by:
Hellom I have an issue with implicit conversions. Apparently when one calls an operator on a class Y which has a conversion operator to class X which has the operator . Sadly it will not do...
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
9
by: Girish | last post by:
Im trying to understand implicit type conversions from object -> string and vice versa. I have two classes, one Driver and one called StringWrapper. These are just test classes that try and...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
10
by: Pieter Breed | last post by:
Hi All, Please excuse me, but the bulk of my post will be a code post. It describes some weirdness with regards to the implicit casting operator. The crux of the problem is this: I want to...
3
by: utab | last post by:
Dear all, I was trying to write a more complex program, and while searching for sth in my reference C++ primer, by Lippman. I had a question in the mind, see the code below #include <string>...
1
by: drop | last post by:
Hi all, I'd like to know if it's possible to declare an implicit type conversion for when I use declarative Synthax in html. For example, when I have the DropDownList, I can declatively set...
8
by: jonpb | last post by:
Hi, Is it possible to define a implicit operator from base to derived types in C#? I have a Point class and would like to derive a Vector class from it and add a couple new vector related...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.