Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

types

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?

like image 757
Matias Chara Avatar asked Jul 15 '20 15:07

Matias Chara


People also ask

What does decltype do in c++?

The decltype type specifier yields the type of a specified expression. The decltype type specifier, together with the auto keyword, is useful primarily to developers who write template libraries. Use auto and decltype to declare a template function whose return type depends on the types of its template arguments.

Is decltype evaluated at compile time?

decltype is a compile time evaluation (like sizeof ), and so can only use the static type.


1 Answers

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.

like image 128
Asteroids With Wings Avatar answered Sep 20 '22 12:09

Asteroids With Wings