Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reference types and literal types

I looked in the standard for a definition of reference type but I could not find any. A naive interpretation of the reference type as the type of objects such as int & fails because according to the standard [basic.types] 10.3:

A type is a literal type if it is: […] a reference type

But the following static assert does not compile (in MSVC2015RC):

    static_assert( ::std::is_literal_type< ::std::wstring & >::value, "Nope" );

What is the definition of a reference type?

like image 336
Hector Avatar asked Nov 29 '25 23:11

Hector


2 Answers

The C++11 Standard defines references in §8.3.2 [dcl.ref]:

1 In a declaration T D where D has either of the forms

& attribute-specifier-seqopt D1
&& attribute-specifier-seqopt D1

and the type of the identifier in the declaration T D1 is “derived-declarator-type-list T,” then the type of the identifier of D is “derived-declarator-type-list reference to T.” The optional attribute-specifier-seq appertains to the reference type. Cv-qualified references are ill-formed except when the cv-qualifiers are introduced through the use of a typedef (7.1.3) or of a template type argument (14.3), in which case the cv-qualifiers are ignored. [...]

2 A reference type that is declared using & is called an lvalue reference, and a reference type that is declared using && is called an rvalue reference. Lvalue references and rvalue references are distinct types. Except where explicitly noted, they are semantically equivalent and commonly referred to as references.

Simply speaking, reference types are types declared as references to other types. There's much more to say about their behavior though.

like image 190
Paolo M Avatar answered Dec 01 '25 13:12

Paolo M


Compiles just fine, in both GCC and Clang, as it should. Your compiler has probably a bugged implementation.

like image 32
Shoe Avatar answered Dec 01 '25 12:12

Shoe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!