Matias Chara
Matias Chara

Reputation: 991

Is sizeof(variableName or expression) the same as sizeof(decltype(variableName or expression))?

Given the c++ keyword decltype and illustrating with a code example:

int main(){
    int variableName = 0;
    sizeof(variableName) == sizeof(decltype(variableName));//Always true for all types? And for all expressions?
    //
    //
    double variableDoubleName = 0;
    sizeof(variableName+variableDoubleName) == sizeof(decltype(variableName+variableDoubleName));//further example of an expression.
}

In the example above, and in general, are sizeof(non-type) and sizeof(decltype(non-type)) always and strictly equivalent? If not, how would they differ?

Upvotes: 13

Views: 377

Answers (1)

Asteroids With Wings
Asteroids With Wings

Reputation: 17454

Yes.

You might be imagining trouble from decltype variously returning a reference type or a value type, depending on whether you give it an identifier or an expression (ref).

But, fortunately for us:

When applied to a reference type, the result [of sizeof] is the size of the referenced type. (ref)

So, it doesn't matter.

Upvotes: 8

Related Questions