* Re: Flag to Stop Implicit Conversion
[not found] <69aa5df20701101529h4cd17030l3378b77623a62170@mail.gmail.com>
@ 2007-01-10 23:38 ` Roopesh Varier
2007-01-11 14:14 ` John Love-Jensen
0 siblings, 1 reply; 2+ messages in thread
From: Roopesh Varier @ 2007-01-10 23:38 UTC (permalink / raw)
To: gcc-help
Hi all,
Does anyone know of a flag that I can use to give an error/warning for
an implicit conversion from an "enum" to "int" ?
Thanks,
Roopesh.
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Flag to Stop Implicit Conversion
2007-01-10 23:38 ` Flag to Stop Implicit Conversion Roopesh Varier
@ 2007-01-11 14:14 ` John Love-Jensen
0 siblings, 0 replies; 2+ messages in thread
From: John Love-Jensen @ 2007-01-11 14:14 UTC (permalink / raw)
To: Roopesh Varier, MSX to GCC
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2007-01-11 14:14 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <69aa5df20701101529h4cd17030l3378b77623a62170@mail.gmail.com>
2007-01-10 23:38 ` Flag to Stop Implicit Conversion Roopesh Varier
2007-01-11 14:14 ` John Love-Jensen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).