The difference between auto and decltype

First, the auto type specifier lets the compiler infer its type from the initial value, while decltype also lets the compiler analyze the expression, but doesn't actually evaluate the expression.

Second, the auto type inferred by the compiler is sometimes not the same as the initial value type, and the compiler will appropriately change the result type to make it more in line with the rules. For example, auto ignores top-level const. And decltype will keep the top-level const.

Third, the result type of decltype is closely related to the expression form. If it is a layer of parentheses, the resulting type is the type of the variable. In the case of double parentheses, the reference type is most inferred.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325886260&siteId=291194637