@@ -1806,21 +1806,40 @@ and so on.
1806
1806
1807
1807
@subsection tutorial-containers-elements Container elements
1808
1808
1809
- In Hana, containers own their elements. When a container is created, it makes
1810
- a _ copy_ of the elements used to initialize it and stores them inside the
1811
- container. Of course, unnecessary copies are avoided by using move semantics.
1812
- Because of those owning semantics, the lifetime of the objects inside the
1813
- container is the same as that of the container.
1809
+ In Hana, containers own their elements. When a container is created, it
1810
+ normally makes a _ copy_ of the elements used to initialize it and stores them
1811
+ inside the container. Of course, unnecessary copies are avoided by using move
1812
+ semantics. Because of those owning semantics, the lifetime of the objects
1813
+ inside the container is the same as that of the container.
1814
1814
1815
1815
@snippet example/tutorial/containers.cpp lifetime
1816
1816
1817
- Much like containers in the standard library, containers in Hana expect their
1818
- elements to be objects. For this reason, references _ may not_ be stored in
1819
- them. When references must be stored inside a container, one should use a
1820
- ` std::reference_wrapper ` instead:
1817
+ However, some containers allow storing references instead of actual objects.
1818
+ In that case, the owning semantics explained above do not hold anymore. For
1819
+ example, it is possible to create a ` hana::tuple ` holding references as
1820
+ follows:
1821
+
1822
+ @snippet example/tutorial/containers.cpp reference_tuple
1823
+
1824
+ @note
1825
+ Of course, a single tuple can also hold some elements by value and other
1826
+ elements by reference.
1827
+
1828
+ Since explicitly specifying the type of the container to achieve by-reference
1829
+ semantics can be cumbersome (and sometimes downright impossible when that
1830
+ type is implementation-defined), the ` make_xxx ` family of functions also
1831
+ support the use of ` reference_wrapper ` s:
1821
1832
1822
1833
@snippet example/tutorial/containers.cpp reference_wrapper
1823
1834
1835
+ When passed to a ` hana::make_xxx ` function, ` std::reference_wrapper ` s and
1836
+ ` boost::reference_wrapper ` s will cause the container to hold a reference
1837
+ instead of a ` reference_wrapper ` . Of course, only the ` make_xxx ` functions
1838
+ associated to containers that support holding references implement this
1839
+ special behavior. To know whether a container is able to hold references
1840
+ (and implements this behavior), one should consult the reference documentation
1841
+ for that container.
1842
+
1824
1843
1825
1844
1826
1845
0 commit comments