Warnings for implicit casts to a narrower type

P

Pete

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...
 
D

David Hilsee

Pete said:
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.
 
P

Phlip

Pete said:
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.
 
J

Jack Klein

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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top