Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use decltype in C++

Shulou Source: shulou.com Published: 2022-06-02 07:29:11 10月03日 Update

This article is about how to use decltype in C++. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Grammar

Decltype (entity) (1) (11 cases)

Decltype (expression) (2) (since 11)

Explanation:

1) if the argument is an identification expression without parentheses or an access expression by a class member without parentheses, decltype produces the type of entity named after that expression. If there is no such entity or the argument specifies a set of overloaded functions, the program is not well constructed.

If the argument refers to an unparenthesized identity expression that names a structured binding, then decltype produces its referenced type (described in the note on the structured binding declaration).

If the argument refers to an unparenthesized identification expression that names a non-type template parameter, decltype generates the type of the template parameter (when the template parameter is declared as a placeholder type, the type does any required type derivation).

2) if the argument is any expression of other type T, and

If the value category of the expression is dead, decltype will generate a token &

If the value category of the expression is a left value, decltype will generate T &

If the value category of the expression is pure right, decltype will generate T.

If the expression is a function call that returns the pure right value of the class type, or the right Operand is a comma expression for such a function call, no temporary quantity is introduced for that pure right value.

If the expression is a pure right value other than (parenthesized) immediate invocation (starting from 20), the temporary object is not materialized from that pure right value: that is, the pure right value has no result object.

The type does not need to be a complete type or have a destructor available, and the type can be abstract. This rule does not apply to its subexpression: in decltype (f (g (), g () must have a full type, but f () does not.

Note:

If the object's name is parenthesized, it is treated as the usual left-value expression, so decltype (x) and decltype ((x)) are usually different types.

Decltype is useful for types that are difficult or impossible to declare in standard writing, such as lambda-related types or types that depend on template parameters.

two。 Keyword decltype

Example:

# include # include struct A {double x;}; const A* a; decltype (a-> x) y; / y is of type double (its declared type) decltype ((a-> x)) z = y The type of / / z is const double& (left-value expression) templateauto add (T t, U)-> decltype (t _ u) / / the return type depends on the template parameter {/ / cantilever 14 to derive the return type return tweeu;} int main () {int I = 33; decltype (I) j = I * 2; std::cout

Tags: Type expression parenthesis template formal parameter same function fruit entity object identity category Category different content more article structure structured good Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Information Linux Shulou Technology Microsoft