Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add_lvalue_reference/add_rvalue_reference and cv-qualified type

cppreference.com about 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.

Does it mean that if T is const or volatile than T is not converted to reference? If no, then what does it mean "has no cv-qualifier".

like image 874
anton_rh Avatar asked Mar 04 '23 11:03

anton_rh


2 Answers

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

Yes, but only if it is a function.

The quote from cppreference can be a little confusing. cppreference has

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.

which can make you believe the cv-qualifer part applies to both objects and functions, but this is not the case. The actual text from the standard is

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

so any object type and any function without a cv-qualifier and ref-qualifier will yield a reference. references or functions with a cv-qualifier and/or ref-qualifier will yield T


When you see cv-qualifier that means const and or volatile qualifier, i.e. it is a const and or volatile object or const or volatile qualified function. ref-qualifier that means reference qualifier, i.e. the function can only be called on an lvalue or rvalue: void foo() & or void foo() &&.

like image 66
NathanOliver Avatar answered Mar 23 '23 00:03

NathanOliver


It's trying to say that the result is respectively T& or T&& if either:

  • T is an object type, or
  • T is a function type that does not have any cv-qualifiers and does not have a ref-qualifier.

The restriction "has no cv- or ref-qualifier" does not apply to object types. It applies to function types because if a function type has a cv- or ref-qualifier, then it is not possible to create the referenced type. See Abominable Function Types for some discussion.

like image 23
Brian Bi Avatar answered Mar 23 '23 00:03

Brian Bi