sizeof(value) vs sizeof(type)?

前端 未结 3 544
一生所求
一生所求 2021-01-02 23:37

Considering :

double data; 
double array[10]; 
std::vector vec(4, 100); 
MyClass myclass;   

Is there a difference between :

3条回答
  •  孤街浪徒
    2021-01-03 00:00

    The latter is defined in terms of the former. Given an expression, it returns the size of the type of the expression. So sizeof(vec) translates to sizeof(std::vector).

    Both are evaluated at compile-time; the only run-time sizeof is in C, not C++, with variable-length arrays. The operand to sizeof is unevaluated, so there isn't any real possibly the expression itself could generate code anyway.

    I prefer to use expressions over types, because chances are stating the type is redundant in some fashion.

提交回复
热议问题