What is the rationale behind decltype behavior?

前端 未结 3 1693
清酒与你
清酒与你 2020-12-24 05:04

As I understood in C++11 decltype(expression) is used to deduce the exact same type of the given expression. But when the expression is put into parentheses its

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-24 05:22

    There is some need for discriminating between an entity and an expression.

    Consider the following question:

    How long is Mississippi?

    There are two answers to this question:

    1. Mississippi is 2,320 miles long.
    2. Mississippi is 11 letters long.

    Similarly when you ask about the type of x, and x is an identifier, it is not clear whether you mean the type that was used to declare that identifier (i.e. the type associated with the name x), or the type of the expression consisting of the sole mentioning of that identifier. In fact there could be two different keywords (e.g. entity_type and expr_type) instead of a single overloaded decltype. For some reason, the committee chose to overload decltype for those two different uses.

提交回复
热议问题