Samaursa
Samaursa

Reputation: 17241

Ensure use of custom types

Considering this answer for the benefit of typedefs on basic types and why they are used, is there any way to ensure that in your project you did not use a basic type and used the typedef counterparts?

Upvotes: 7

Views: 205

Answers (3)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 263037

If you really, absolutely want to ban native types but allow typedefs, I guess you can always do something like:

#include <stdint.h>

#define int please_use_stdint_typedefs_rather_than_native_types

int main()
{
    int32_t good;  // Good typedef.
    int evil;      // Evil native type.
}

$ gcc -c int_forbidden.c 
int_forbidden.c: In function ‘main’:
int_forbidden.c:8: error: ‘please_use_stdint_typedefs_rather_than_native_types’ undeclared (first use in this function)
int_forbidden.c:8: error: (Each undeclared identifier is reported only once
int_forbidden.c:8: error: for each function it appears in.)
int_forbidden.c:8: error: expected ‘;’ before ‘evil’

That said, I don't think outright banning native types is a good idea in the general case.

Upvotes: 8

LainIwakura
LainIwakura

Reputation: 3031

Considering a typedef is just a synonym for a type and does not actually create a new type, I don't think there would be any reliable way to ensure this. You could write a script to run through the code and look for occurrences of primitive types vs. the expected typedef counterpart.

Upvotes: 0

Joel Falcou
Joel Falcou

Reputation: 6357

You can make these typedefs Strong Typedefs as proposed in this boost library : http://www.boost.org/doc/libs/1_40_0/boost/strong_typedef.hpp

Upvotes: 5

Related Questions