@@ -1847,21 +1847,40 @@ and so on.
1847
1847
1848
1848
@subsection tutorial-containers-elements Container elements
1849
1849
1850
- In Hana, containers own their elements. When a container is created, it makes
1851
- a _ copy_ of the elements used to initialize it and stores them inside the
1852
- container. Of course, unnecessary copies are avoided by using move semantics.
1853
- Because of those owning semantics, the lifetime of the objects inside the
1854
- container is the same as that of the container.
1850
+ In Hana, containers own their elements. When a container is created, it
1851
+ normally makes a _ copy_ of the elements used to initialize it and stores them
1852
+ inside the container. Of course, unnecessary copies are avoided by using move
1853
+ semantics. Because of those owning semantics, the lifetime of the objects
1854
+ inside the container is the same as that of the container.
1855
1855
1856
1856
@snippet example/tutorial/containers.cpp lifetime
1857
1857
1858
- Much like containers in the standard library, containers in Hana expect their
1859
- elements to be objects. For this reason, references _ may not_ be stored in
1860
- them. When references must be stored inside a container, one should use a
1861
- ` std::reference_wrapper ` instead:
1858
+ However, some containers allow storing references instead of actual objects.
1859
+ In that case, the owning semantics explained above do not hold anymore. For
1860
+ example, it is possible to create a ` hana::tuple ` holding references as
1861
+ follows:
1862
+
1863
+ @snippet example/tutorial/containers.cpp reference_tuple
1864
+
1865
+ @note
1866
+ Of course, a single tuple can also hold some elements by value and other
1867
+ elements by reference.
1868
+
1869
+ Since explicitly specifying the type of the container to achieve by-reference
1870
+ semantics can be cumbersome (and sometimes downright impossible when that
1871
+ type is implementation-defined), the ` make_xxx ` family of functions also
1872
+ support the use of ` reference_wrapper ` s:
1862
1873
1863
1874
@snippet example/tutorial/containers.cpp reference_wrapper
1864
1875
1876
+ When passed to a ` hana::make_xxx ` function, ` std::reference_wrapper ` s and
1877
+ ` boost::reference_wrapper ` s will cause the container to hold a reference
1878
+ instead of a ` reference_wrapper ` . Of course, only the ` make_xxx ` functions
1879
+ associated to containers that support holding references implement this
1880
+ special behavior. To know whether a container is able to hold references
1881
+ (and implements this behavior), one should consult the reference documentation
1882
+ for that container.
1883
+
1865
1884
1866
1885
1867
1886
0 commit comments