Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The standard way to get sizeof(promoted(x)) [duplicate]

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)
...
like image 710
Eugene Ryabtsev Avatar asked Jun 05 '15 13:06

Eugene Ryabtsev


1 Answers

auto s = sizeof(+x);

should do the trick for integers.

+x makes use of the unary + operator, which performs integer promotion just like any other arithmetic operator.

I am not aware of any standard promotion rules for float that would apply here (in the integer promotion sense) since you can do arithmetic with them without promoting. If you always want to promote to at least double you can try

auto s = sizeof(x + 0.);

and then distinguish between floating point and integers before you get there.

Again, I do not think that you can handle integers and floating points at once because of the different meanings of "promotion" we are applying here.

like image 173
Baum mit Augen Avatar answered Sep 19 '22 19:09

Baum mit Augen