# c++ - pointer - sizeof class

## The standard way to get sizeof(promoted(x)) (2)

To generalise Baum mit Augen's answer, you could write function templates like this:

```
template <typename T>
auto promoted(T)
-> std::enable_if_t<std::is_integral<T>::value, decltype(+T{})>;
template <typename T>
auto promoted(T)
-> std::enable_if_t<std::is_floating_point<T>::value, decltype(T{}+0.)>;
//usage
sizeof(promoted(a))
```

Or a version using type traits:

```
template <typename T, typename = void>
struct promoted;
template <typename T>
struct promoted<T, std::enable_if_t<std::is_integral<T>::value>>
{ using type = decltype(+T{}); };
template <typename T>
struct promoted<T, std::enable_if_t<std::is_floating_point<T>::value>>
{ using type = decltype(T{} + 0.); };
template <typename T>
using promoted_t = typename promoted<T>::type;
//usage
sizeof(promoted_t<decltype(a)>)
```

This question already has an answer here:

Is there a standard way to get the size of the type a variable would be promoted to when passed as a variadic argument?

```
auto x = ...;
auto y = sizeof(promoted(x));
```

The results should be:

```
char -> sizeof(int)
int -> sizeof(int)
float -> sizeof(double)
...
```

We can simply declare overloaded `promoted`

functions with the proper types:

```
int promoted(char);
int promoted(short);
int promoted(int);
long promoted(long);
long long promoted(long long);
double promoted(float);
double promoted(double);
long double promoted(long double);
```

Note that the functions need no implementations, because we are never actually calling them.

Here is a simple test run which prints 1, 4 and 4, 8 on my machine:

```
std::cout << sizeof('a') << '\n';
std::cout << sizeof(promoted('a')) << '\n';
std::cout << sizeof(3.14f) << '\n';
std::cout << sizeof(promoted(3.14f)) << '\n';
```