A tool that returns the variable type (char, uint8_t, ...)

Dependents:   GetTypeName_demo

Get a variable type using type traits - a replacement for RTTI typeid().

Sources
https://developer.mbed.org/forum/bugs-suggestions/topic/4494/?page=1#comment-22386
http://stackoverflow.com/questions/81870/print-variable-type-in-c

In RTTI, we were able to use something like.

// Depending on the compiler used, this returns 'int', 'i', ....
    int main() {
        int a= 4;
        printf("%s", typeid(a).name());
    }


To reduce the code overhead, this can no longer be used in the mbed compiler.
However, we can replace this with type traits.
Example

#include "mbed.h"
#include "GetTypeName.h"

int main()
{
    char a = 65;
    printf("Type name for <char> '%c' is %s\r\n", a, GetTypeName(a));
    // Check whether GetTypeName is of 'char' type.
    // Note that strcmp returns 0 when both strings are equal.
    if(!strcmp(GetTypeName(a),"char")) printf("'%c' is of 'char' type\r\n", a);
}

Supported declarations

VariableReturns
charchar
uint8_tuint8_t
signed charint8_t
int8_tint8_t
unsigned shortuint16_t
uint16_tuint16_t
shortint16_t
int16_tint16_t
unsigned intuint32_t
uint32_tuint32_t
intint32_t
int32_tint32_t
unsigned long longuint64_t
uint64_tuint64_t
long longint64_t
int64_tint64_t
floatfloat
doubledouble
boolbool
Download repository: zip gz

Files at revision 0:9723189e1955

Name Size Actions
[up]
GetTypeName.h 4341 Revisions Annotate