引用类型和文字类型
reference types and literal types
我在标准中查找了 引用类型 的定义,但找不到任何内容。将 引用类型 天真解释为 int &
等对象的类型会失败,因为根据标准 [basic.types] 10.3:
A type is a literal type if it is: […] a reference type
但以下静态断言无法编译(在 MSVC2015RC 中):
static_assert( ::std::is_literal_type< ::std::wstring & >::value, "Nope" );
引用类型的定义是什么?
C++11 标准在 §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.
简单来说,引用类型就是声明为对其他类型的引用的类型。不过,关于他们的行为还有很多话要说。
我在标准中查找了 引用类型 的定义,但找不到任何内容。将 引用类型 天真解释为 int &
等对象的类型会失败,因为根据标准 [basic.types] 10.3:
A type is a literal type if it is: […] a reference type
但以下静态断言无法编译(在 MSVC2015RC 中):
static_assert( ::std::is_literal_type< ::std::wstring & >::value, "Nope" );
引用类型的定义是什么?
C++11 标准在 §8.3.2 中定义引用 [dcl.ref]:
1 In a declaration
T D
whereD
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 ofD
is “derived-declarator-type-list reference toT
.” 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.
简单来说,引用类型就是声明为对其他类型的引用的类型。不过,关于他们的行为还有很多话要说。