Enabling Custom Types

Given this answer in favor of typedefs for basic types and why they are used, is there a way to ensure that you have not used a base type and used typedef instances in your project?

+7
source share
3 answers

If you really, absolutely want to ban native types, but allow typedefs , I think 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' 

However, I do not think that explicitly prohibiting native types is a good idea in the general case.

+8
source

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

+5
source

Given that typedef is simply a type synonym and does not actually create a new type, I don’t think there would be a reliable way to provide this. You can write a script to run the code and find the occurrence of primitive types compared to the expected equivalent of typedef.

0
source

All Articles