473,395 Members | 1,441 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,395 software developers and data experts.

Avoiding Implicit Casting

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)
Nov 13 '05 #1
9 8486
In article <bl**********@titan.btinternet.com>,
Simon <so*********@no.no> wrote:
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

--
Dave Vandervies dj******@csclub.uwaterloo.ca
The n869 draft does not define the language, but this non-definition is more
authoritative (though less readable) than K&R's non-definition of the language.
--Richard Heathfield in comp.lang.c
Nov 13 '05 #2
Simon wrote:
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.
Nov 13 '05 #3
Simon wrote:

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?
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #4
Joe Wright wrote:
Simon wrote:

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?


Only the fact that he wants your code to generate a diagnostic (or, perhaps,
a run-time error).
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #5
Simon wrote:
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?

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #6
Richard Heathfield wrote:

Joe Wright wrote:
Simon wrote:

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?


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? :-)
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #7
In <bl**********@titan.btinternet.com> "Simon" <so*********@no.no> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #8
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.
"Dan Pop" <Da*****@cern.ch> wrote in message
news:bl**********@sunnews.cern.ch...
In <bl**********@titan.btinternet.com> "Simon" <so*********@no.no> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de

Nov 13 '05 #9
"Kris Wempa" <calmincents(NO_SPAM)@yahoo.com> writes:
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
Nov 13 '05 #10

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

Similar topics

3
by: Brian Byrne | last post by:
I've recently developed a minor interest in Expression Templates and, in attempt to further my understanding of template metaprogramming, have been trying to create my own implementation. It seems...
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...
6
by: Gecko | last post by:
I would like to know if there is a way to stop the runtime from implicitly casting values. For exampel, I would like the following code to crash: byte someByte = 5; int someInt = someByte; I...
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...
7
by: Paminu | last post by:
On a gentoo linux system i don't get any warnings when I comlpile this code: #include <stdio.h> typedef struct test { void *content; struct test_ *bob; } test_;
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...
17
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
5
by: johanatan | last post by:
Does anyone know the reasons for the lack of an implicit casting operator in any greater depth than: A. Automatic conversion is believed to be too error prone. (from the FAQ at the bottom of:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.