This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Flag to Stop Implicit Conversion
- From: John Love-Jensen <eljay at adobe dot com>
- To: Roopesh Varier <rvarier at gmail dot com>, MSX to GCC <gcc-help at gcc dot gnu dot org>
- Date: Thu, 11 Jan 2007 08:13:56 -0600
- Subject: Re: Flag to Stop Implicit Conversion
Hi Roopesh,
Although I, too, desire the same kind of warning you mention, it just isn't
C or C++.
Moreso, I wish I could disable all implicit conversions. But, doing so,
would be a language that isn't C or C++. It would be an "almost C" or
"almost C++" language, which would break a lot of code.
What you need is a different language, such as Ada or D Programming
Language. Both of which are available for GCC.
Or in C, you need to wrap your int in a struct. Like this:
----------------------------------------
#include <stdio.h>
typedef struct myint_s
{
int m;
} myint;
void Foo(myint in)
{
printf("%d\n", in.m);
}
enum Numero { uno, dos, tres };
int main()
{
Foo(uno); // Bzzt.
Foo(1); // Bzzt.
myint i = { 1 };
Foo(i); // Good.
}
----------------------------------------
Or in C++ (q.v. Stroupstrup C++PL 11.7.1)...
----------------------------------------
#include <iostream>
class myint
{
int m;
public:
explicit myint(int i) : m(i) { }
operator int () const { return m; }
};
void Foo(myint in)
{
std::cout << in << std::endl;
}
enum MetasyntacticSugar { foo, bar, quux };
int main()
{
Foo(quux); // Bzzt.
Foo(3); // Bzzt.
Foo(myint(3)); // Good.
myint i(3);
Foo(i); // Good.
}
----------------------------------------
CAVEAT: I just wrote these off the cuff, I haven't tried to compile them.
HTH,
--Eljay