Why are qualifiers of template arguments stripped when deducing the type?

前端 未结 2 1951
半阙折子戏
半阙折子戏 2021-01-12 12:07

While building a little sample program with Microsoft VisualStudio 2008 I noticed an odd thing about the deduction of types passed to templates. Consider this example:

2条回答
  •  無奈伤痛
    2021-01-12 12:31

    The answer is that although the variable v has type const int &, the expression v is an lvalue expression with type const int.

    litb provides the text (5/6): "If an expression initially has the type “reference to T” (8.3.2, 8.5.3), the type is adjusted to “T” prior to any further analysis, the expression designates the object or function denoted by the reference, and the expression is an lvalue."

    An "argument" is "an expression in the comma-separated list bounded by the parentheses in a function call expression" (1.3.1). So in 14.8.2.1:

    • "the corresponding argument type of the call (call it A)" is const int.
    • "If A is a cv-qualified type, the top-level cv-qualifiers of A's type are ignored for type deduction" (hence, int).
    • "the deduction process attempts to find template argument values that will make the deduced A identical to A" (so T is int)

提交回复
热议问题