|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r79777 - sandbox/type_erasure/libs/type_erasure/example
From: steven_at_[hidden]
Date: 2012-07-27 23:43:27
Author: steven_watanabe
Date: 2012-07-27 23:43:26 EDT (Fri, 27 Jul 2012)
New Revision: 79777
URL: http://svn.boost.org/trac/boost/changeset/79777
Log:
Add more complete description to associated types example.
Text files modified:
sandbox/type_erasure/libs/type_erasure/example/associated.cpp | 52 +++++++++++++++++++++++++++++++--------
1 files changed, 41 insertions(+), 11 deletions(-)
Modified: sandbox/type_erasure/libs/type_erasure/example/associated.cpp
==============================================================================
--- sandbox/type_erasure/libs/type_erasure/example/associated.cpp (original)
+++ sandbox/type_erasure/libs/type_erasure/example/associated.cpp 2012-07-27 23:43:26 EDT (Fri, 27 Jul 2012)
@@ -54,16 +54,20 @@
dereferenceable<deduced<pointee<T> >&, T>
>
{
+ // provide a typedef for convenience
typedef deduced<pointee<T> > element_type;
};
//]
-void basic1() {
+void associated2() {
//[associated2
/*`
- Now when we construct `x`, `pointer<>::element_type` is
- deduced as `boost::pointee<int*>::type` which is `int`.
+ Now the Concept of `x` uses two placeholders, `_self`
+ and `pointer<>::element_type`. When we construct `x`,
+ with an `int*`, `pointer<>::element_type` is deduced
+ as `pointee<int*>::type` which is `int`. Thus, dereferencing
+ `x` returns an __any that contains an `int`.
*/
int i = 10;
any<
@@ -76,16 +80,13 @@
//]
}
-void basic2() {
+void associated3() {
//[associated3
/*`
- Referring to the full name of the associated type
- can be cumbersome when it's used many times. Also,
- sometimes we want to require that the associated
- type be a specific type. Both of these can be
- solved using the __same_type concept. Here we
- create an any that can hold any pointer whose
- element type is `int`.
+ Sometimes we want to require that the associated
+ type be a specific type. This can be solved using
+ the __same_type concept. Here we create an any that
+ can hold any pointer whose element type is `int`.
*/
int i = 10;
any<
@@ -95,6 +96,34 @@
>
> x(&i);
std::cout << *x << std::endl; // prints 10
+ /*`
+ Using __same_type like this effectively causes the library to
+ replace all uses of `pointer<>::element_type` with `int`
+ and validate that it is always bound to `int`.
+ Thus, dereferencing `x` now returns an `int`.
+ */
+ //]
+}
+
+void associated4() {
+ //[associated4
+ /*`
+ __same_type can also be used for two placeholders.
+ This allows us to use a simple name instead of
+ writing out an associated type over and over.
+ */
+ int i = 10;
+ any<
+ mpl::vector<
+ pointer<>,
+ same_type<pointer<>::element_type, _a>,
+ typeid_<_a>,
+ copy_constructible<_a>,
+ addable<_a>,
+ ostreamable<std::ostream, _a>
+ >
+ > x(&i);
+ std::cout << (*x + *x) << std::endl; // prints 20
//]
}
@@ -104,4 +133,5 @@
//` [associated1]
//` [associated2]
//` [associated3]
+//` [associated4]
//]
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk