Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ensure use of custom types

Tags:

c++

types

typedef

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?

like image 868
Samaursa Avatar asked Jun 25 '11 21:06

Samaursa


People also ask

What is type assertion in TypeScript?

In Typescript, Type assertion is a technique that informs the compiler about the type of a variable. Type assertion is similar to typecasting but it doesn't reconstruct code. You can use type assertion to specify a value's type and tell the compiler not to deduce it.

What is the difference between type and interface in TypeScript?

// One major difference between type aliases vs interfaces are that interfaces are open and type aliases are closed. This means you can extend an interface by declaring it a second time. // In the other case a type cannot be changed outside of its declaration.

What is type guard in TypeScript?

A type guard is a TypeScript technique used to get information about the type of a variable, usually within a conditional block. Type guards are regular functions that return a boolean, taking a type and telling TypeScript if it can be narrowed down to something more specific.


2 Answers

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.

like image 79
Frédéric Hamidi Avatar answered Sep 16 '22 15:09

Frédéric Hamidi


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

like image 29
Joel Falcou Avatar answered Sep 18 '22 15:09

Joel Falcou