By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Warnings for implicit casts to a narrower type

P: n/a
I'd like to have warnings for code like this:

void blah(char c) {}

int main() {
**int*a*=*500;
**blah(a);*//*want*a*warning*here
**char*b*=*a;*//*warning*here*would*be*nice*too
}
Can this be achieved via any trick of the language, or is it
compiler-specific?

Thanks...
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Pete" <yo**************@spamsrus.co.uk> wrote in message
news:fo*******************@fe1.news.blueyonder.co. uk...
I'd like to have warnings for code like this:

void blah(char c) {}

int main() {
int a = 500;
blah(a); // want a warning here
char b = a; // warning here would be nice too
}
Can this be achieved via any trick of the language, or is it
compiler-specific?


Check your compiler's documentation. Warnings are usually configurable.
For example, g++ has a -W flag to control which warnings it displays.
Warnings for those implicit conversions are probably turned off because most
programmers find them annoying, distracting, and not helpful.

--
David Hilsee
Jul 22 '05 #2

P: n/a
Pete wrote:
I'd like to have warnings for code like this:

void blah(char c) {}

int main() {
int a = 500;
blah(a); // want a warning here
char b = a; // warning here would be nice too
}
Can this be achieved via any trick of the language, or is it
compiler-specific?


The C languages permit all kinds of abuses, so an independent system called
"lint" was invented to report all sorts of situations of varying
consequence.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #3

P: n/a
On Sun, 08 Aug 2004 18:35:55 GMT, Pete
<yo**************@spamsrus.co.uk> wrote in comp.lang.c++:
I'd like to have warnings for code like this:

void blah(char c) {}

int main() {
**int*a*=*500;
**blah(a);*//*want*a*warning*here
**char*b*=*a;*//*warning*here*would*be*nice*too
}
Can this be achieved via any trick of the language, or is it
compiler-specific?
Most compilers will emit such warnings if you set the appropriate
compile time option. Consult your compiler's documentation.
Thanks...


There are also static code analysis tools that will evaluate your
source code and emit such warnings. AFAIK, there are no free ones for
C++. The least expensive would be PC-Lint (http://www.gimpel.com) if
you work on a Windows platform.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.