C++域¶
Added in version 1.0.
C++域(名称) cpp )支持记录C++项目。
声明实体的指令¶
以下指令可用。所有声明都可以以Visibility语句开头 (public
, private
或 protected
)
- .. cpp:class:: class specifier¶
- .. cpp:struct:: class specifier¶
描述一个类/结构,可能带有继承规范,例如::
.. cpp:class:: MyClass : public MyBase, MyOtherBase
两者之间的区别
cpp:class
和cpp:struct
只是修饰的:输出中呈现的前缀,以及索引中显示的说明符。类可以直接在嵌套范围内声明,例如::
.. cpp:class:: OuterScope::MyClass : public MyBase, MyOtherBase
类模板可以声明为:
.. cpp:class:: template<typename T, std::size_t N> std::array
或者换行:
.. cpp:class:: template<typename T, std::size_t N> \ std::array
可以声明完整和部分模板特殊性::
.. cpp:class:: template<> \ std::array<bool, 256> .. cpp:class:: template<typename T> \ std::array<T, 42>
Added in version 2.0: 这个
cpp:struct
指令。
- .. cpp:function:: (member) function prototype¶
描述一个函数或成员函数,例如:
.. cpp:function:: bool myMethod(int arg1, std::string arg2) A function with parameters and types. .. cpp:function:: bool myMethod(int, double) A function with unnamed parameters. .. cpp:function:: const T &MyClass::operator[](std::size_t i) const An overload for the indexing operator. .. cpp:function:: operator bool() const A casting operator. .. cpp:function:: constexpr void foo(std::string &bar[2]) noexcept A constexpr function. .. cpp:function:: MyClass::MyClass(const MyClass&) = default A copy constructor with default implementation.
还可以描述函数模板:
.. cpp:function:: template<typename U> \ void print(U &&u)
功能模板专业:
.. cpp:function:: template<> \ void print(int i)
- :single-line-parameter-list: (no value)¶
确保函数的参数将在单个逻辑行上发出,从而重写
cpp_maximum_signature_line_length
和maximum_signature_line_length
。Added in version 7.1.
- .. cpp:member:: (member) variable declaration¶
- .. cpp:var:: (member) variable declaration¶
描述变量或成员变量,例如:
.. cpp:member:: std::string MyClass::myMember .. cpp:var:: std::string MyClass::myOtherMember[N][M] .. cpp:member:: int a = 42
变量模板也可以描述为:
.. cpp:member:: template<class T> \ constexpr T pi = T(3.1415926535897932385)
- .. cpp:type:: typedef declaration¶
- .. cpp:type:: name
- .. cpp:type:: type alias declaration
将类型描述为typedef声明、类型别名声明,或者简单地描述具有未指定类型的类型的名称,例如::
.. cpp:type:: std::vector<int> MyList A typedef-like declaration of a type. .. cpp:type:: MyContainer::const_iterator Declaration of a type alias with unspecified type. .. cpp:type:: MyType = std::unordered_map<int, std::string> Declaration of a type alias.
类型别名也可以模板化:
.. cpp:type:: template<typename T> \ MyContainer = std::vector<T>
示例呈现如下。
-
typedef std::vector<int> MyList¶
一个类型的类typlef声明。
-
type MyContainer::const_iterator¶
声明具有未指定类型的类型别名。
-
using MyType = std::unordered_map<int, std::string>¶
类型别名的声明。
-
typedef std::vector<int> MyList¶
- .. cpp:enum:: unscoped enum declaration¶
- .. cpp:enum-struct:: scoped enum declaration¶
- .. cpp:enum-class:: scoped enum declaration¶
描述(作用域)枚举,可能具有指定的基础类型。在非范围枚举中声明的任何枚举数都将在枚举范围和父范围中声明。实例:
.. cpp:enum:: MyEnum An unscoped enum. .. cpp:enum:: MySpecificEnum : long An unscoped enum with specified underlying type. .. cpp:enum-class:: MyScopedEnum A scoped enum. .. cpp:enum-struct:: protected MyScopedVisibilityEnum : std::underlying_type<MySpecificEnum>::type A scoped enum with non-default visibility, and with a specified underlying type.
- .. cpp:enumerator:: name¶
- .. cpp:enumerator:: name = constant
描述枚举器,可选择定义其值,例如:
.. cpp:enumerator:: MyEnum::myEnumerator .. cpp:enumerator:: MyEnum::myOtherEnumerator = 42
- .. cpp:union:: name¶
描述工会。
Added in version 1.8.
- .. cpp:concept:: template-parameter-list name¶
警告
对概念的支持是实验性的。它以现行标准草案和概念技术规范为基础。这些特性可能随着发展而变化。
描述一个概念。它必须正好有一个模板参数列表。名称可以是嵌套名称。例子::
.. cpp:concept:: template<typename It> std::Iterator Proxy to an element of a notional sequence that can be compared, indirected, or incremented. **Notation** .. cpp:var:: It r An lvalue. **Valid Expressions** - :cpp:expr:`*r`, when :cpp:expr:`r` is dereferenceable. - :cpp:expr:`++r`, with return type :cpp:expr:`It&`, when :cpp:expr:`r` is incrementable.
这将呈现如下:
-
template<typename It>
concept std::Iterator¶ 概念序列中可以比较、间接或递增的元素的代理。
Notation
Valid Expressions
Added in version 1.5.
-
template<typename It>
选项¶
一些指令支持选项:
:no-index-entry:
和:no-contents-entry:
,见 基本标记 。:tparam-line-spec:
,用于模板化声明。如果指定,每个模板参数将在单独的行上呈现。Added in version 1.6.
匿名实体¶
C++支持匿名命名空间、类、枚举和联合。为了便于记录,必须给它们起一个以 @
, e.g., @42
or @data
. These names can also be used in cross-references and (type) expressions, though nested symbols will be found even when omitted. The @...
名称将始终呈现为 [[anonymous]] (可能作为链接)。
例子::
.. cpp:class:: Data
.. cpp:union:: @data
.. cpp:var:: int a
.. cpp:var:: double b
Explicit ref: :cpp:var:`Data::@data::a`. Short-hand ref: :cpp:var:`Data::a`.
这将呈现为:
明确裁判: Data::[anonymous]::a
. 短手裁判: Data::a
.
Added in version 1.8.
别名声明¶
有时,它可能是有帮助的列表声明,而不是它们的主文档,例如,在创建类接口的概要时。以下指令可用于此目的。
- .. cpp:alias:: name or function signature¶
插入一个或多个别名声明。每个实体都可以在
cpp:any
角色。如果给定了函数名(而不是完整的签名),那么将列出函数的所有重载。例如::
.. cpp:alias:: Data::a overload_example::C::f
变成
反之::
.. cpp:alias:: void overload_example::C::f(double d) const void overload_example::C::f(double d)
变成
Added in version 2.0.
选项
- :maxdepth: int¶
同时插入嵌套声明,直到指定的总深度。使用0表示无限深,使用1表示上述声明。默认为1。
Added in version 3.5.
- :noroot:¶
跳过前面提到的声明,只呈现嵌套声明。要求
maxdepth
0或至少2。Added in version 3.5.
受约束的模板¶
警告
对概念的支持是实验性的。它以现行标准草案和概念技术规范为基础。这些特性可能随着发展而变化。
备注
Sphinx目前不支持 requires
条款。
占位符¶
声明可以使用概念的名称来引入受约束的模板参数,或者使用关键字 auto
引入不受约束的模板参数:
.. cpp:function:: void f(auto &&arg)
A function template with a single unconstrained template parameter.
.. cpp:function:: void f(std::Iterator it)
A function template with a single template parameter, constrained by the
Iterator concept.
模板介绍¶
简单的约束函数或类模板可以用 template introduction 而不是模板参数列表:
.. cpp:function:: std::Iterator{It} void advance(It &it)
A function template with a template parameter constrained to be an
Iterator.
.. cpp:class:: std::LessThanComparable{T} MySortedContainer
A class template with a template parameter constrained to be
LessThanComparable.
它们呈现如下。
-
std::LessThanComparable{T}
class MySortedContainer¶ 模板参数被约束为LessThanCompare的类模板。
但是请注意,没有对参数兼容性执行任何检查。例如。, Iterator{{A, B, C}}
将被接受作为一个介绍,即使它将是无效的C++。
内联表达式和类型¶
- :cpp:expr:¶
- :cpp:texpr:¶
插入C++表达式或类型作为内联代码 (
cpp:expr
)或内嵌文本 (cpp:texpr
)例如::.. cpp:var:: int a = 42 .. cpp:function:: int f(int i) An expression: :cpp:expr:`a * f(a)` (or as text: :cpp:texpr:`a * f(a)`). A type: :cpp:expr:`const MySortedContainer<int>&` (or as text :cpp:texpr:`const MySortedContainer<int>&`).
将按以下方式呈现:
-
int a = 42¶
-
int f(int i)¶
表达式: a * f(a) (或作为文本: a * f(a) )
类型: const MySortedContainer<int>& (或作为文本) const MySortedContainer<int>& )
Added in version 1.7: 这个
cpp:expr
角色。Added in version 1.8: 这个
cpp:texpr
角色。-
int a = 42¶
名称空间¶
C++域中的声明默认为全局范围。可以使用三个名称空间指令更改当前范围。它们管理一个堆栈声明,其中 cpp:namespace
重置堆栈并更改给定范围。
这个 cpp:namespace-push
指令将作用域更改为当前作用域的给定内部作用域。
这个 cpp:namespace-pop
指令撤消最近的 cpp:namespace-push
指令。
- .. cpp:namespace:: scope specification¶
将后续对象的当前作用域更改为给定作用域,并重置命名空间指令堆栈。请注意,命名空间不需要对应于C++命名空间,而是可以以类的名称结束,例如,:
.. cpp:namespace:: Namespace1::Namespace2::SomeClass::AnInnerClass
所有随后的对象都将被定义为其名称是用预先准备好的作用域声明的。随后的交叉引用将从当前作用域开始搜索。
使用
NULL
,0
或nullptr
因为范围将变为全局范围。命名空间声明也可以模板化,例如::
.. cpp:class:: template<typename T> \ std::vector .. cpp:namespace:: template<typename T> std::vector .. cpp:function:: std::size_t size() const
宣布
size
作为类模板的成员函数std::vector
. 同样,可以使用以下方式声明:.. cpp:class:: template<typename T> \ std::vector .. cpp:function:: std::size_t size() const
或:
.. cpp:class:: template<typename T> \ std::vector
- .. cpp:namespace-push:: scope specification¶
将范围相对更改为当前范围。例如,之后::
.. cpp:namespace:: A::B .. cpp:namespace-push:: C::D
当前范围为
A::B::C::D
.Added in version 1.4.
- .. cpp:namespace-pop::¶
撤消上一个
cpp:namespace-push
指令(指令) not 打开一个示波器)。例如,之后::.. cpp:namespace:: A::B .. cpp:namespace-push:: C::D .. cpp:namespace-pop::
当前范围为
A::B
( notA::B::C
)如果没有以前
cpp:namespace-push
已使用指令,但只有cpp:namespace
指令,则当前作用域将重置为全局作用域。也就是说,.. cpp:namespace:: A::B
等于:.. cpp:namespace:: nullptr .. cpp:namespace-push:: A::B
Added in version 1.4.
信息字段列表¶
所有用于声明实体的C++指令都支持以下信息字段(另请参阅 信息字段列表 ):
tparam
:模板参数的说明。
这个 cpp:function
指令还支持以下字段:
param
,parameter
,arg
,argument
:参数的说明。returns
,return
:返回值的描述。retval
,retvals
:一种替代returns
用于描述函数的结果。throws
,throw
,exception
:对可能引发的异常的描述。
Added in version 4.3: 这个 retval
字段类型。
交叉引用¶
这些角色链接到给定的声明类型:
- :cpp:any:¶
- :cpp:class:¶
- :cpp:struct:¶
- :cpp:func:¶
- :cpp:member:¶
- :cpp:var:¶
- :cpp:type:¶
- :cpp:concept:¶
- :cpp:enum:¶
- :cpp:enumerator:¶
以名称引用C++声明(详情见下文)。相对于链接的位置,名称必须正确限定。
Added in version 2.0: 这个
cpp:struct
角色作为的别名cpp:class
角色。
关于带有模板参数/参数的引用的说明
这些角色遵循Sphinx 交叉引用语法 规则。这意味着在引用(部分)模板专门化时必须小心,例如,如果链接如下所示: :cpp:class:`MyClass<int>
'.这被解释为 int
有一个标题 MyClass
. 在这种情况下,用反斜杠退出开口角括号,如下所示: :cpp:class:`MyClass\<int>
'.
当不需要自定义标题时,使用内联表达式的角色可能很有用, cpp:expr
和 cpp:texpr
,其中尖括号不需要转义。
没有模板参数和模板参数的声明¶
要链接到非模板化声明,名称必须是嵌套名称,例如, f
或 MyClass::f
.
重载(成员)函数¶
当(成员)函数仅使用其名称被引用时,引用将指向任意匹配的重载。这个 cpp:any
和 cpp:func
角色使用另一种格式,这只是一个完整的函数声明。这将解析为完全匹配的重载。例如,考虑以下类声明:
参考文献使用 cpp:func
角色:
任意过载:
C::f
,C::f()
也可任意超载:
C::f()
,C::f()
特定过载:
void C::f()
,void C::f()
特定过载:
void C::f(int)
,void C::f(int)
特定过载:
void C::f(double)
,void C::f(double)
特定过载:
void C::f(double) const
,void C::f(double) const
请注意 add_function_parentheses
配置变量不影响特定的重载引用。
模板化声明¶
假设以下声明。
一般来说,引用必须包括模板参数声明和限定名称前缀的模板参数。例如:
template\<typename TOuter> Wrapper::Outer
(template<typename TOuter> Wrapper::Outer
)template\<typename TOuter> template\<typename TInner> Wrapper::Outer<TOuter>::Inner
(template<typename TOuter> template<typename TInner> Wrapper::Outer<TOuter>::Inner
)
当前,仅当模板参数标识符为相等字符串时,查找才会成功。也就是说, template\<typename UOuter> Wrapper::Outer
不会起作用。
作为一个简写符号,如果省略了模板参数列表,那么查找将采用主模板或非模板,而不是部分模板专业化。这意味着以下参考也有效:
Wrapper::Outer
(Wrapper::Outer
)Wrapper::Outer::Inner
(Wrapper::Outer::Inner
)template\<typename TInner> Wrapper::Outer::Inner
(template<typename TInner> Wrapper::Outer::Inner
)
(完整)模板专业¶
假设以下声明。
通常,引用必须包含每个模板参数列表的模板参数列表。因此,上述全部专业可参考 template\<> Outer\<int>
(template<> Outer<int>
) template\<> template\<> Outer\<int>::Inner\<bool>
(template<> template<> Outer<int>::Inner<bool>
)简而言之,可以省略空模板参数列表,例如, Outer\<int>
(Outer<int>
) Outer\<int>::Inner\<bool>
(Outer<int>::Inner<bool>
)
部分模板专业¶
假设以下声明。
对部分特殊化的引用必须始终包括模板参数列表,例如, template\<typename T> Outer\<T*>
(template<typename T> Outer<T*>
)当前,仅当模板参数标识符为相等字符串时,查找才会成功。
配置变量¶
见 C++域的选项 .