Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Homepage
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
| Variable | Returns |
| char | char |
| uint8_t | uint8_t |
| signed char | int8_t |
| int8_t | int8_t |
| unsigned short | uint16_t |
| uint16_t | uint16_t |
| short | int16_t |
| int16_t | int16_t |
| unsigned int | uint32_t |
| uint32_t | uint32_t |
| int | int32_t |
| int32_t | int32_t |
| unsigned long long | uint64_t |
| uint64_t | uint64_t |
| long long | int64_t |
| int64_t | int64_t |
| float | float |
| double | double |
| bool | bool |