sizeof(variableName or expression) 和 sizeof(decltype(variableName or expression)) 一样吗?

Mat*_*ara 12 c++ types

给定 c++ 关键字 decltype 并用代码示例进行说明:

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.
}
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,一般来说, sizeof(non-type) 和 sizeof(decltype(non-type)) 总是严格相等的吗?如果不是,它们会有什么不同?

Ast*_*ngs 7

是的。

您可能会想像decltype不同地返回引用类型或值类型会带来麻烦,这取决于您给它的是标识符还是表达式 ( ref )。

但是,对我们来说幸运的是:

当应用于引用类型时,结果 [of sizeof] 是引用类型的大小。(参考

所以,没关系。