@@ -1870,21 +1870,40 @@ and so on.
1870
1870
1871
1871
@subsection tutorial-containers-elements Container elements
1872
1872
1873
- In Hana, containers own their elements. When a container is created, it makes
1874
- a _copy_ of the elements used to initialize it and stores them inside the
1875
- container. Of course, unnecessary copies are avoided by using move semantics.
1876
- Because of those owning semantics, the lifetime of the objects inside the
1877
- container is the same as that of the container.
1873
+ In Hana, containers own their elements. When a container is created, it
1874
+ normally makes a _copy_ of the elements used to initialize it and stores them
1875
+ inside the container. Of course, unnecessary copies are avoided by using move
1876
+ semantics. Because of those owning semantics, the lifetime of the objects
1877
+ inside the container is the same as that of the container.
1878
1878
1879
1879
@snippet example/tutorial/containers.cpp lifetime
1880
1880
1881
- Much like containers in the standard library, containers in Hana expect their
1882
- elements to be objects. For this reason, references _may not_ be stored in
1883
- them. When references must be stored inside a container, one should use a
1884
- `std::reference_wrapper` instead:
1881
+ However, some containers allow storing references instead of actual objects.
1882
+ In that case, the owning semantics explained above do not hold anymore. For
1883
+ example, it is possible to create a `hana::tuple` holding references as
1884
+ follows:
1885
+
1886
+ @snippet example/tutorial/containers.cpp reference_tuple
1887
+
1888
+ @note
1889
+ Of course, a single tuple can also hold some elements by value and other
1890
+ elements by reference.
1891
+
1892
+ Since explicitly specifying the type of the container to achieve by-reference
1893
+ semantics can be cumbersome (and sometimes downright impossible when that
1894
+ type is implementation-defined), the `make_xxx` family of functions also
1895
+ support the use of `reference_wrapper`s:
1885
1896
1886
1897
@snippet example/tutorial/containers.cpp reference_wrapper
1887
1898
1899
+ When passed to a `hana::make_xxx` function, `std::reference_wrapper`s and
1900
+ `boost::reference_wrapper`s will cause the container to hold a reference
1901
+ instead of a `reference_wrapper`. Of course, only the `make_xxx` functions
1902
+ associated to containers that support holding references implement this
1903
+ special behavior. To know whether a container is able to hold references
1904
+ (and implements this behavior), one should consult the reference documentation
1905
+ for that container.
1906
+
1888
1907
1889
1908
1890
1909
0 commit comments