Data types and their byte size

Hello,

Is there a way print a list of data types and making a loop to
find the size of each data types, with out manually typing the name of
the data type and typing 'sizeof(...)' for each of them?

Maybe using an array with a loop but i'm not sure how to use the char or string
names and to insert the name into the 'sizeof' operator to find the byte and print
it to the user.

the types in question:
char, unsigned char, signed char, int , unsigned int, signed int, short int,
unsigned short int, signed short int, long int, signed long int, unsigned long int, float, double, long double, and wchar_t.
Last edited on
Something like this, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <typeinfo>

template < typename... > struct print_size ;

template <> struct print_size<> { static void print() {} } ;

template < typename T > struct print_size<T> { static void print()
{ std::cout << typeid(T).name() << ' ' << sizeof(T) << '\n' ; } };

template < typename F, typename... S > struct print_size<F,S...>
{ static void print() { print_size<F>::print() ; print_size<S...>::print() ; } };

int main()
{
    print_size< char, unsigned char, signed char, int , unsigned int, signed int, short int,
                unsigned short int, signed short int, long int, signed long int, unsigned long int,
                float, double, long double, wchar_t >::print() ;
}


https://rextester.com/ZSA43556

Note: to demangle the names of types on GNU / compatible implementations, see:
https://www.cplusplus.com/forum/beginner/175177/#msg866884
Topic archived. No new replies allowed.