c++clangoclint

How to get the size of a type in C++?


I am looking for a C++ function that returns the size of a type. For example :

#include <stdint.h> 

int16_t my_var; 
int32_t size_of_var; 

// magical_function is what I'm looking for
// size_of_var should be equal to 16 because my_var is a int16_t
size_of_var = my_var.magical_function() ; 

I know that size() exists to get the length of a string, so I guess there is a function indicating that too.

Moreover, I'm working with Clang library, so I can get a Type (http://clang.llvm.org/doxygen/classclang_1_1Type.html) but I currently have no idea to compare 2 types and to know the bigger between those two.


Solution

  • In case you want 16 instead of 2 for int16_t

    sizeof(my_var) * CHAR_BIT
    

    sizeof give you how many bytes, and CHAR_BIT give you how many bits in a byte (normally 8)