Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

284
Views
Warn if another typedef'd name of a type is used in an argument list

Consider a large project, where many types are typedef'd, e.g.

typedef int age;
typedef int height;

and some functions getting arguments of those types:

void printPerson(age a, height h) {
    printf("Age %d, Height %d\n", a, h);
}

Is there a way to warn at compile time, if those arguments are of the wrong type, e.g.

age a = 30;
height h = 180;
printPerson(h, a); /* No warning, because a and h are both integers */

Does gcc (or some static code analysis tool) have an option to warn in such cases?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

There is no built-in support for this in GCC.

There is a feature request to add this, based on the Sparse nocast attribute. However, this hasn't been implemented. If you can use Sparse, though, you could do this by marking each typedef with __attribute__((nocast)).

In C++ you can do this by making wrapper classes rather than typedefs, and then simply not defining implicit conversions for them.

over 4 years ago · Santiago Trujillo Report

0

Klocwork has some checks related to what they call "strong typing".

For your code it throws STRONG.TYPE.ASSIGN.ARG because argument types do not match.

It also complains about assigning int values (the consts) to age and height typed variables and about using the variables as int in printf.

I heard it is quite expensive, though.

over 4 years ago · Santiago Trujillo Report

0

As has been made clear by the other responses, you're not going to get this for free from gcc. You are definitely into the world of static analysis tools to solve this.

There have been several suggestions for this, some of which require extra annotation, some of which don't but may be more than you're looking for. I therefore thought I'd throw one more into the mix...

A long stand by for me has been the various command line lint tools. In your case, I think PC-lint/flexelint fits very well even though it is a commercial tool. See here for its strong type checking.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!