Avoiding Implicit Casting

S

Simon

Hi All,

Is it possible to disallow implicit casting for an operand of a function
written in C?

i.e.
void foo(int a) {..}
short b;
foo(b) // error without explicit cast

Thanks,

Simon ;o)
 
D

Dave Vandervies

Hi All,

Is it possible to disallow implicit casting for an operand of a function
written in C?

Terminology nitpick: There's no such thing as an implicit cast; a
cast is a source-level construct (which forces a coversion) and is by
definition explicit.
The term you're looking for is "implicit conversion".
i.e.
void foo(int a) {..}
short b;
foo(b) // error without explicit cast

No, there's no way to avoid implicit conversions other than not writing
code that uses them in the first place.


The canonical question to ask at this point is "What are you really
trying to do?".


dave
 
J

Jeremy Yallop

Simon said:
Is it possible to disallow implicit casting for an operand of a function
written in C?

i.e.
void foo(int a) {..}
short b;
foo(b) // error without explicit cast

Not really. (Why would you want to?) I can think of a couple of ways
to achieve a similar effect:

* use user-defined types (struct or union) instead of builtins.

typedef struct { int value; } Int;
typedef struct { short value; } Short;
void foo(Int);
Short b;
foo(b); /* error */

* pass pointers instead of values around.

void foo(const int *);
short b;
foo(&b); /* error */

These are likely to be fairly cumbersome to use, though.

(As a side note, I wouldn't describe C's various implicit conversions
as "casting". "Casting" always refers, I think, to conversions which
the programmer explicitly requests by using a cast operator.)

Jeremy.
 
J

Joe Wright

Simon said:
Hi All,

Is it possible to disallow implicit casting for an operand of a function
written in C?

i.e.
void foo(int a) {..}
short b;
foo(b) // error without explicit cast
#include <stdio.h>

void foo(int a) {
printf("%d\n", a);
}

int main(void) {
short b = 42;
foo(b);
return 0;
}
Works fine. What am I missing?
 
R

Richard Heathfield

Joe said:
#include <stdio.h>

void foo(int a) {
printf("%d\n", a);
}

int main(void) {
short b = 42;
foo(b);
return 0;
}
Works fine. What am I missing?

Only the fact that he wants your code to generate a diagnostic (or, perhaps,
a run-time error).
 
R

Richard Heathfield

Simon said:
Hi All,

Is it possible to disallow implicit casting for an operand of a function
written in C?

Yes. I hereby disallow it. (There is no such thing as implicit casting in C.
A cast is an explicit conversion, so "implicit cast" is an oxymoron.)
i.e.
void foo(int a) {..}
short b;
foo(b) // error without explicit cast

Disabling normal conversion between short int and int would be short-sighted
(but not int-sighted), since it would break a lot of existing code. *Why*
do you want to do this? Cui bono?
 
J

Joe Wright

Richard said:
Only the fact that he wants your code to generate a diagnostic (or, perhaps,
a run-time error).
Do you still have "Reading for Comprehension" available? :)
 
D

Dan Pop

In said:
Is it possible to disallow implicit casting for an operand of a function
written in C?

i.e.
void foo(int a) {..}
short b;
foo(b) // error without explicit cast

These implicit function argument conversions are a standard feature of the
language, no conforming compiler is allowed not to perform them.

At best, your compiler may have an option to warn when such conversions
are performed. However, compilers usually can be made to warn about
implicit conversions to narrower types, not to wider types (the latter
being always value-preserving and, therefore, not a source of problems).

You can also usually get warnings about implicit conversions to types
of a different signedness (e.g. from short int to unsinged int).

Dan
 
K

Kris Wempa

You can force these to be errors by creating your own data type (struct or
union) and "wrapping" your value in the data type. I don't know how viable
that is for you, but it's a solution. Depending on your compiler, you may
also have the option to treat warnings like errors. You may want to look
into that as well.
 
M

Micah Cowan

Kris Wempa said:
You can force these to be errors by creating your own data type (struct or
union) and "wrapping" your value in the data type. I don't know how viable
that is for you, but it's a solution. Depending on your compiler, you may
also have the option to treat warnings like errors. You may want to look
into that as well.

(Please don't top-post. Luckily, I didn't need any of what Dan
said for my response to you).

In addition to being ugly, this solution also only achieves an
extra level of indirection; you're still able to do:

struct foo { short bar; } quux;

int quuux = 10;
quux.bar = quuux;

-Micah
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top