Debian Codegraph @

doc/usage/domains/cpp.rst

raw
1 .. highlight:: rst
2
3 ==============
4 The C++ Domain
5 ==============
6
7 .. versionadded:: 1.0
8
9 The C++ domain (name **cpp**) supports documenting C++ projects.
10
11 Directives for Declaring Entities
12 ---------------------------------
13
14 The following directives are available. All declarations can start with a
15 visibility statement (``public``, ``private`` or ``protected``).
16
17 .. rst:directive:: .. cpp:class:: class specifier
18 .. cpp:struct:: class specifier
19
20 Describe a class/struct, possibly with specification of inheritance, e.g.,::
21
22 .. cpp:class:: MyClass : public MyBase, MyOtherBase
23
24 The difference between :rst:dir:`cpp:class` and :rst:dir:`cpp:struct` is
25 only cosmetic: the prefix rendered in the output, and the specifier shown
26 in the index.
27
28 The class can be directly declared inside a nested scope, e.g.,::
29
30 .. cpp:class:: OuterScope::MyClass : public MyBase, MyOtherBase
31
32 A class template can be declared::
33
34 .. cpp:class:: template<typename T, std::size_t N> std::array
35
36 or with a line break::
37
38 .. cpp:class:: template<typename T, std::size_t N> \
39 std::array
40
41 Full and partial template specialisations can be declared::
42
43 .. cpp:class:: template<> \
44 std::array<bool, 256>
45
46 .. cpp:class:: template<typename T> \
47 std::array<T, 42>
48
49 .. versionadded:: 2.0
50 The :rst:dir:`cpp:struct` directive.
51
52 .. rst:directive:: .. cpp:function:: (member) function prototype
53
54 Describe a function or member function, e.g.,::
55
56 .. cpp:function:: bool myMethod(int arg1, std::string arg2)
57
58 A function with parameters and types.
59
60 .. cpp:function:: bool myMethod(int, double)
61
62 A function with unnamed parameters.
63
64 .. cpp:function:: const T &MyClass::operator[](std::size_t i) const
65
66 An overload for the indexing operator.
67
68 .. cpp:function:: operator bool() const
69
70 A casting operator.
71
72 .. cpp:function:: constexpr void foo(std::string &bar[2]) noexcept
73
74 A constexpr function.
75
76 .. cpp:function:: MyClass::MyClass(const MyClass&) = default
77
78 A copy constructor with default implementation.
79
80 Function templates can also be described::
81
82 .. cpp:function:: template<typename U> \
83 void print(U &&u)
84
85 and function template specialisations::
86
87 .. cpp:function:: template<> \
88 void print(int i)
89
90 .. rst:directive:option:: single-line-parameter-list
91 :type: no value
92
93 Ensures that the function's parameters will be emitted on a single logical
94 line, overriding :confval:`cpp_maximum_signature_line_length` and
95 :confval:`maximum_signature_line_length`.
96
97 .. versionadded:: 7.1
98
99 .. rst:directive:: .. cpp:member:: (member) variable declaration
100 .. cpp:var:: (member) variable declaration
101
102 Describe a variable or member variable, e.g.,::
103
104 .. cpp:member:: std::string MyClass::myMember
105
106 .. cpp:var:: std::string MyClass::myOtherMember[N][M]
107
108 .. cpp:member:: int a = 42
109
110 Variable templates can also be described::
111
112 .. cpp:member:: template<class T> \
113 constexpr T pi = T(3.1415926535897932385)
114
115 .. rst:directive:: .. cpp:type:: typedef declaration
116 .. cpp:type:: name
117 .. cpp:type:: type alias declaration
118
119 Describe a type as in a typedef declaration, a type alias declaration, or
120 simply the name of a type with unspecified type, e.g.,::
121
122 .. cpp:type:: std::vector<int> MyList
123
124 A typedef-like declaration of a type.
125
126 .. cpp:type:: MyContainer::const_iterator
127
128 Declaration of a type alias with unspecified type.
129
130 .. cpp:type:: MyType = std::unordered_map<int, std::string>
131
132 Declaration of a type alias.
133
134 A type alias can also be templated::
135
136 .. cpp:type:: template<typename T> \
137 MyContainer = std::vector<T>
138
139 The example are rendered as follows.
140
141 .. cpp:type:: std::vector<int> MyList
142 :no-contents-entry:
143 :no-index-entry:
144
145 A typedef-like declaration of a type.
146
147 .. cpp:type:: MyContainer::const_iterator
148 :no-contents-entry:
149 :no-index-entry:
150
151 Declaration of a type alias with unspecified type.
152
153 .. cpp:type:: MyType = std::unordered_map<int, std::string>
154 :no-contents-entry:
155 :no-index-entry:
156
157 Declaration of a type alias.
158
159 .. cpp:type:: template<typename T> \
160 MyContainer = std::vector<T>
161 :no-contents-entry:
162 :no-index-entry:
163
164 .. rst:directive:: .. cpp:enum:: unscoped enum declaration
165 .. cpp:enum-struct:: scoped enum declaration
166 .. cpp:enum-class:: scoped enum declaration
167
168 Describe a (scoped) enum, possibly with the underlying type specified. Any
169 enumerators declared inside an unscoped enum will be declared both in the
170 enum scope and in the parent scope. Examples::
171
172 .. cpp:enum:: MyEnum
173
174 An unscoped enum.
175
176 .. cpp:enum:: MySpecificEnum : long
177
178 An unscoped enum with specified underlying type.
179
180 .. cpp:enum-class:: MyScopedEnum
181
182 A scoped enum.
183
184 .. cpp:enum-struct:: protected MyScopedVisibilityEnum : std::underlying_type<MySpecificEnum>::type
185
186 A scoped enum with non-default visibility, and with a specified
187 underlying type.
188
189 .. rst:directive:: .. cpp:enumerator:: name
190 .. cpp:enumerator:: name = constant
191
192 Describe an enumerator, optionally with its value defined, e.g.,::
193
194 .. cpp:enumerator:: MyEnum::myEnumerator
195
196 .. cpp:enumerator:: MyEnum::myOtherEnumerator = 42
197
198 .. rst:directive:: .. cpp:union:: name
199
200 Describe a union.
201
202 .. versionadded:: 1.8
203
204 .. rst:directive:: .. cpp:concept:: template-parameter-list name
205
206 .. warning:: The support for concepts is experimental. It is based on the
207 current draft standard and the Concepts Technical Specification.
208 The features may change as they evolve.
209
210 Describe a concept. It must have exactly 1 template parameter list. The name
211 may be a nested name. Example::
212
213 .. cpp:concept:: template<typename It> std::Iterator
214
215 Proxy to an element of a notional sequence that can be compared,
216 indirected, or incremented.
217
218 **Notation**
219
220 .. cpp:var:: It r
221
222 An lvalue.
223
224 **Valid Expressions**
225
226 - :cpp:expr:`*r`, when :cpp:expr:`r` is dereferenceable.
227 - :cpp:expr:`++r`, with return type :cpp:expr:`It&`, when
228 :cpp:expr:`r` is incrementable.
229
230 This will render as follows:
231
232 .. cpp:concept:: template<typename It> std::Iterator
233 :no-contents-entry:
234 :no-index-entry:
235
236 Proxy to an element of a notional sequence that can be compared,
237 indirected, or incremented.
238
239 **Notation**
240
241 .. cpp:var:: It r
242
243 An lvalue.
244
245 **Valid Expressions**
246
247 - :cpp:expr:`*r`, when :cpp:expr:`r` is dereferenceable.
248 - :cpp:expr:`++r`, with return type :cpp:expr:`It&`, when :cpp:expr:`r`
249 is incrementable.
250
251 .. versionadded:: 1.5
252
253
254 Options
255 ~~~~~~~
256
257 Some directives support options:
258
259 - ``:no-index-entry:`` and ``:no-contents-entry:``, see :ref:`basic-domain-markup`.
260 - ``:tparam-line-spec:``, for templated declarations.
261 If specified, each template parameter will be rendered on a separate line.
262
263 .. versionadded:: 1.6
264
265 Anonymous Entities
266 ------------------
267
268 C++ supports anonymous namespaces, classes, enums, and unions.
269 For the sake of documentation they must be given some name that starts with
270 ``@``, e.g., ``@42`` or ``@data``.
271 These names can also be used in cross-references and (type) expressions,
272 though nested symbols will be found even when omitted.
273 The ``@...`` name will always be rendered as **[anonymous]** (possibly as a
274 link).
275
276 Example::
277
278 .. cpp:class:: Data
279
280 .. cpp:union:: @data
281
282 .. cpp:var:: int a
283
284 .. cpp:var:: double b
285
286 Explicit ref: :cpp:var:`Data::@data::a`. Short-hand ref: :cpp:var:`Data::a`.
287
288 This will be rendered as:
289
290 .. cpp:class:: Data
291 :no-contents-entry:
292 :no-index-entry:
293
294 .. cpp:union:: @data
295 :no-contents-entry:
296 :no-index-entry:
297
298 .. cpp:var:: int a
299 :no-contents-entry:
300 :no-index-entry:
301
302 .. cpp:var:: double b
303 :no-contents-entry:
304 :no-index-entry:
305
306 Explicit ref: :cpp:var:`Data::@data::a`. Short-hand ref: :cpp:var:`Data::a`.
307
308 .. versionadded:: 1.8
309
310
311 Aliasing Declarations
312 ---------------------
313
314 Sometimes it may be helpful list declarations elsewhere than their main
315 documentation, e.g., when creating a synopsis of a class interface.
316 The following directive can be used for this purpose.
317
318 .. rst:directive:: .. cpp:alias:: name or function signature
319
320 Insert one or more alias declarations. Each entity can be specified
321 as they can in the :rst:role:`cpp:any` role.
322 If the name of a function is given (as opposed to the complete signature),
323 then all overloads of the function will be listed.
324
325 For example::
326
327 .. cpp:alias:: Data::a
328 overload_example::C::f
329
330 becomes
331
332 .. cpp:alias:: Data::a
333 overload_example::C::f
334
335 whereas::
336
337 .. cpp:alias:: void overload_example::C::f(double d) const
338 void overload_example::C::f(double d)
339
340 becomes
341
342 .. cpp:alias:: void overload_example::C::f(double d) const
343 void overload_example::C::f(double d)
344
345 .. versionadded:: 2.0
346
347
348 .. rubric:: Options
349
350 .. rst:directive:option:: maxdepth: int
351
352 Insert nested declarations as well, up to the total depth given.
353 Use 0 for infinite depth and 1 for just the mentioned declaration.
354 Defaults to 1.
355
356 .. versionadded:: 3.5
357
358 .. rst:directive:option:: noroot
359
360 Skip the mentioned declarations and only render nested declarations.
361 Requires ``maxdepth`` either 0 or at least 2.
362
363 .. versionadded:: 3.5
364
365
366 Constrained Templates
367 ---------------------
368
369 .. warning:: The support for concepts is experimental. It is based on the
370 current draft standard and the Concepts Technical Specification.
371 The features may change as they evolve.
372
373 .. note:: Sphinx does not currently support ``requires`` clauses.
374
375 Placeholders
376 ~~~~~~~~~~~~
377
378 Declarations may use the name of a concept to introduce constrained template
379 parameters, or the keyword ``auto`` to introduce unconstrained template
380 parameters::
381
382 .. cpp:function:: void f(auto &&arg)
383
384 A function template with a single unconstrained template parameter.
385
386 .. cpp:function:: void f(std::Iterator it)
387
388 A function template with a single template parameter, constrained by the
389 Iterator concept.
390
391 Template Introductions
392 ~~~~~~~~~~~~~~~~~~~~~~
393
394 Simple constrained function or class templates can be declared with a `template
395 introduction` instead of a template parameter list::
396
397 .. cpp:function:: std::Iterator{It} void advance(It &it)
398
399 A function template with a template parameter constrained to be an
400 Iterator.
401
402 .. cpp:class:: std::LessThanComparable{T} MySortedContainer
403
404 A class template with a template parameter constrained to be
405 LessThanComparable.
406
407 They are rendered as follows.
408
409 .. cpp:function:: std::Iterator{It} void advance(It &it)
410 :no-contents-entry:
411 :no-index-entry:
412
413 A function template with a template parameter constrained to be an Iterator.
414
415 .. cpp:class:: std::LessThanComparable{T} MySortedContainer
416 :no-contents-entry:
417 :no-index-entry:
418
419 A class template with a template parameter constrained to be
420 LessThanComparable.
421
422 Note however that no checking is performed with respect to parameter
423 compatibility. E.g., ``Iterator{A, B, C}`` will be accepted as an introduction
424 even though it would not be valid C++.
425
426 Inline Expressions and Types
427 ----------------------------
428
429 .. rst:role:: cpp:expr
430 cpp:texpr
431
432 Insert a C++ expression or type either as inline code (``cpp:expr``)
433 or inline text (``cpp:texpr``). For example::
434
435 .. cpp:var:: int a = 42
436
437 .. cpp:function:: int f(int i)
438
439 An expression: :cpp:expr:`a * f(a)` (or as text: :cpp:texpr:`a * f(a)`).
440
441 A type: :cpp:expr:`const MySortedContainer<int>&`
442 (or as text :cpp:texpr:`const MySortedContainer<int>&`).
443
444 will be rendered as follows:
445
446 .. cpp:var:: int a = 42
447 :no-contents-entry:
448 :no-index-entry:
449
450 .. cpp:function:: int f(int i)
451 :no-contents-entry:
452 :no-index-entry:
453
454 An expression: :cpp:expr:`a * f(a)` (or as text: :cpp:texpr:`a * f(a)`).
455
456 A type: :cpp:expr:`const MySortedContainer<int>&`
457 (or as text :cpp:texpr:`const MySortedContainer<int>&`).
458
459 .. versionadded:: 1.7
460 The :rst:role:`cpp:expr` role.
461
462 .. versionadded:: 1.8
463 The :rst:role:`cpp:texpr` role.
464
465 Namespacing
466 -----------
467
468 Declarations in the C++ domain are as default placed in global scope. The
469 current scope can be changed using three namespace directives. They manage a
470 stack declarations where ``cpp:namespace`` resets the stack and changes a given
471 scope.
472
473 The ``cpp:namespace-push`` directive changes the scope to a given inner scope
474 of the current one.
475
476 The ``cpp:namespace-pop`` directive undoes the most recent
477 ``cpp:namespace-push`` directive.
478
479 .. rst:directive:: .. cpp:namespace:: scope specification
480
481 Changes the current scope for the subsequent objects to the given scope, and
482 resets the namespace directive stack. Note that the namespace does not need
483 to correspond to C++ namespaces, but can end in names of classes, e.g.,::
484
485 .. cpp:namespace:: Namespace1::Namespace2::SomeClass::AnInnerClass
486
487 All subsequent objects will be defined as if their name were declared with
488 the scope prepended. The subsequent cross-references will be searched for
489 starting in the current scope.
490
491 Using ``NULL``, ``0``, or ``nullptr`` as the scope will change to global
492 scope.
493
494 A namespace declaration can also be templated, e.g.,::
495
496 .. cpp:class:: template<typename T> \
497 std::vector
498
499 .. cpp:namespace:: template<typename T> std::vector
500
501 .. cpp:function:: std::size_t size() const
502
503 declares ``size`` as a member function of the class template
504 ``std::vector``. Equivalently this could have been declared using::
505
506 .. cpp:class:: template<typename T> \
507 std::vector
508
509 .. cpp:function:: std::size_t size() const
510
511 or::
512
513 .. cpp:class:: template<typename T> \
514 std::vector
515
516 .. rst:directive:: .. cpp:namespace-push:: scope specification
517
518 Change the scope relatively to the current scope. For example, after::
519
520 .. cpp:namespace:: A::B
521
522 .. cpp:namespace-push:: C::D
523
524 the current scope will be ``A::B::C::D``.
525
526 .. versionadded:: 1.4
527
528 .. rst:directive:: .. cpp:namespace-pop::
529
530 Undo the previous ``cpp:namespace-push`` directive (*not* just pop a scope).
531 For example, after::
532
533 .. cpp:namespace:: A::B
534
535 .. cpp:namespace-push:: C::D
536
537 .. cpp:namespace-pop::
538
539 the current scope will be ``A::B`` (*not* ``A::B::C``).
540
541 If no previous ``cpp:namespace-push`` directive has been used, but only a
542 ``cpp:namespace`` directive, then the current scope will be reset to global
543 scope. That is, ``.. cpp:namespace:: A::B`` is equivalent to::
544
545 .. cpp:namespace:: nullptr
546
547 .. cpp:namespace-push:: A::B
548
549 .. versionadded:: 1.4
550
551 Info field lists
552 ----------------
553
554 All the C++ directives for declaring entities support the following
555 info fields (see also :ref:`info-field-lists`):
556
557 * ``tparam``: Description of a template parameter.
558
559 The :rst:dir:`cpp:function` directive additionally supports the
560 following fields:
561
562 * ``param``, ``parameter``, ``arg``, ``argument``: Description of a parameter.
563 * ``returns``, ``return``: Description of a return value.
564 * ``retval``, ``retvals``: An alternative to ``returns`` for describing
565 the result of the function.
566 * ``throws``, ``throw``, ``exception``: Description of a possibly thrown exception.
567
568 .. versionadded:: 4.3
569 The ``retval`` field type.
570
571 .. _cpp-xref-roles:
572
573 Cross-referencing
574 -----------------
575
576 These roles link to the given declaration types:
577
578 .. rst:role:: cpp:any
579 cpp:class
580 cpp:struct
581 cpp:func
582 cpp:member
583 cpp:var
584 cpp:type
585 cpp:concept
586 cpp:enum
587 cpp:enumerator
588
589 Reference a C++ declaration by name (see below for details). The name must
590 be properly qualified relative to the position of the link.
591
592 .. versionadded:: 2.0
593 The :rst:role:`cpp:struct` role as alias for the :rst:role:`cpp:class`
594 role.
595
596 .. admonition:: Note on References with Templates Parameters/Arguments
597
598 These roles follow the Sphinx :ref:`xref-syntax` rules. This means care must
599 be taken when referencing a (partial) template specialization, e.g. if the
600 link looks like this: ``:cpp:class:`MyClass<int>```.
601 This is interpreted as a link to ``int`` with a title of ``MyClass``.
602 In this case, escape the opening angle bracket with a backslash,
603 like this: ``:cpp:class:`MyClass\<int>```.
604
605 When a custom title is not needed it may be useful to use the roles for
606 inline expressions, :rst:role:`cpp:expr` and :rst:role:`cpp:texpr`, where
607 angle brackets do not need escaping.
608
609 Declarations without template parameters and template arguments
610 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
611
612 For linking to non-templated declarations the name must be a nested name, e.g.,
613 ``f`` or ``MyClass::f``.
614
615
616 Overloaded (member) functions
617 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
618
619 When a (member) function is referenced using just its name, the reference
620 will point to an arbitrary matching overload.
621 The :rst:role:`cpp:any` and :rst:role:`cpp:func` roles use an alternative
622 format, which simply is a complete function declaration.
623 This will resolve to the exact matching overload.
624 As example, consider the following class declaration:
625
626 .. cpp:namespace-push:: overload_example
627 .. cpp:class:: C
628 :no-contents-entry:
629 :no-index-entry:
630
631 .. cpp:function:: void f(double d) const
632 :no-contents-entry:
633 :no-index-entry:
634 .. cpp:function:: void f(double d)
635 :no-contents-entry:
636 :no-index-entry:
637 .. cpp:function:: void f(int i)
638 :no-contents-entry:
639 :no-index-entry:
640 .. cpp:function:: void f()
641 :no-contents-entry:
642 :no-index-entry:
643
644 References using the :rst:role:`cpp:func` role:
645
646 - Arbitrary overload: ``C::f``, :cpp:func:`C::f`
647 - Also arbitrary overload: ``C::f()``, :cpp:func:`C::f()`
648 - Specific overload: ``void C::f()``, :cpp:func:`void C::f()`
649 - Specific overload: ``void C::f(int)``, :cpp:func:`void C::f(int)`
650 - Specific overload: ``void C::f(double)``, :cpp:func:`void C::f(double)`
651 - Specific overload: ``void C::f(double) const``,
652 :cpp:func:`void C::f(double) const`
653
654 Note that the :confval:`add_function_parentheses` configuration variable
655 does not influence specific overload references.
656
657 .. cpp:namespace-pop::
658
659
660 Templated declarations
661 ~~~~~~~~~~~~~~~~~~~~~~
662
663 Assume the following declarations.
664
665 .. cpp:class:: Wrapper
666 :no-contents-entry:
667 :no-index-entry:
668
669 .. cpp:class:: template<typename TOuter> \
670 Outer
671 :no-contents-entry:
672 :no-index-entry:
673
674 .. cpp:class:: template<typename TInner> \
675 Inner
676 :no-contents-entry:
677 :no-index-entry:
678
679 In general the reference must include the template parameter declarations,
680 and template arguments for the prefix of qualified names. For example:
681
682 - ``template\<typename TOuter> Wrapper::Outer``
683 (:cpp:class:`template\<typename TOuter> Wrapper::Outer`)
684 - ``template\<typename TOuter> template\<typename TInner> Wrapper::Outer<TOuter>::Inner``
685 (:cpp:class:`template\<typename TOuter> template\<typename TInner> Wrapper::Outer<TOuter>::Inner`)
686
687 Currently the lookup only succeed if the template parameter identifiers are
688 equal strings. That is, ``template\<typename UOuter> Wrapper::Outer`` will not
689 work.
690
691 As a shorthand notation, if a template parameter list is omitted,
692 then the lookup will assume either a primary template or a non-template,
693 but not a partial template specialisation.
694 This means the following references work as well:
695
696 - ``Wrapper::Outer``
697 (:cpp:class:`Wrapper::Outer`)
698 - ``Wrapper::Outer::Inner``
699 (:cpp:class:`Wrapper::Outer::Inner`)
700 - ``template\<typename TInner> Wrapper::Outer::Inner``
701 (:cpp:class:`template\<typename TInner> Wrapper::Outer::Inner`)
702
703 (Full) Template Specialisations
704 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
705
706 Assume the following declarations.
707
708 .. cpp:class:: template<typename TOuter> \
709 Outer
710 :no-contents-entry:
711 :no-index-entry:
712
713 .. cpp:class:: template<typename TInner> \
714 Inner
715 :no-contents-entry:
716 :no-index-entry:
717
718 .. cpp:class:: template<> \
719 Outer<int>
720 :no-contents-entry:
721 :no-index-entry:
722
723 .. cpp:class:: template<typename TInner> \
724 Inner
725 :no-contents-entry:
726 :no-index-entry:
727
728 .. cpp:class:: template<> \
729 Inner<bool>
730 :no-contents-entry:
731 :no-index-entry:
732
733 In general the reference must include a template parameter list for each
734 template argument list. The full specialisation above can therefore be
735 referenced with ``template\<> Outer\<int>`` (:cpp:class:`template\<>
736 Outer\<int>`) and ``template\<> template\<> Outer\<int>::Inner\<bool>``
737 (:cpp:class:`template\<> template\<> Outer\<int>::Inner\<bool>`). As a
738 shorthand the empty template parameter list can be omitted, e.g.,
739 ``Outer\<int>`` (:cpp:class:`Outer\<int>`) and ``Outer\<int>::Inner\<bool>``
740 (:cpp:class:`Outer\<int>::Inner\<bool>`).
741
742 Partial Template Specialisations
743 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
744
745 Assume the following declaration.
746
747 .. cpp:class:: template<typename T> \
748 Outer<T*>
749 :no-contents-entry:
750 :no-index-entry:
751
752 References to partial specialisations must always include the template
753 parameter lists, e.g., ``template\<typename T> Outer\<T*>``
754 (:cpp:class:`template\<typename T> Outer\<T*>`). Currently the lookup only
755 succeed if the template parameter identifiers are equal strings.
756
757 Configuration Variables
758 -----------------------
759
760 See :ref:`cpp-config`.