add_lvalue_reference/add_rvalue_reference 和 cv 限定类型

add_lvalue_reference/add_rvalue_reference and cv-qualified type

cppreference.com 关于 std::add_lvalue_reference/std::add_rvalue_reference:

If T is an object type or a function type that has no cv- or ref- qualifier, provides a member typedef type which is T&&, otherwise type is T.

这是否意味着如果 T 是 constvolatile 则 T 不会转换为引用?如果不是,那是什么意思 "has no cv-qualifier".

Does it mean that if T is const or volatile than T is not converted to reference?

是的,但前提是它是一个函数。

cppreference 中的引述可能有点令人困惑。 cppreference 有

If T is an object type or a function type that has no cv- or ref- qualifier, provides a member typedef type which is T&&, otherwise type is T.

这会让您相信 cv-qualifer 部分适用于对象和函数,但事实并非如此。实际的text from the standard

an object type, a function type that does not have cv-qualifiers or a ref-qualifier, or a reference type

所以没有 cv-qualifierref-qualifier 的任何对象类型和任何函数都会产生引用。带有 cv-qualifier and/or ref-qualifier 的引用或函数将产生 T


当您看到 cv-qualifier 时,表示 const 和/或 volatile 限定符,即它是 const 和/或 volatile 对象或 const 或 volatile 限定函数。 ref-qualifier 表示引用限定符,即函数只能在左值或右值上调用:void foo() &void foo() &&.

它试图说结果分别是 T&T&& 如果两者之一:

  • T是对象类型,或者
  • T 是一种函数类型,没有任何 cv 限定符,也没有 ref 限定符。

限制 "has no cv- or ref-qualifier" 不适用于对象类型。它适用于函数类型,因为如果函数类型具有 cv 或 ref 限定符,则无法创建引用类型。有关讨论,请参阅 Abominable Function Types