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?
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.
decltype is a compile time evaluation (like sizeof ), and so can only use the static type.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With