1 [/==============================================================================
2 Copyright (C) 2001-2011 Joel de Guzman
3 Copyright (C) 2006 Dan Marsden
5 Use, modification and distribution is subject to the Boost Software
6 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
8 ===============================================================================/]
11 A couple of classes and metafunctions provide basic support for Fusion.
17 Metafunction that evaluates to `mpl::true_` if a certain type `T` is a
18 conforming Fusion __sequence__, `mpl::false_` otherwise. This may be
19 specialized to accommodate clients which provide Fusion conforming sequences.
28 typedef __unspecified__ type;
35 [[Parameter] [Requirement] [Description]]
36 [[`T`] [Any type] [The type to query.]]
39 [heading Expression Semantics]
41 typedef traits::is_sequence<T>::type c;
43 [*Return type]: An __mpl_boolean_constant__.
45 [*Semantics]: Metafunction that evaluates to `mpl::true_` if a certain type
46 `T` is a conforming Fusion sequence, `mpl::false_` otherwise.
50 #include <boost/fusion/support/is_sequence.hpp>
51 #include <boost/fusion/include/is_sequence.hpp>
55 BOOST_MPL_ASSERT_NOT(( traits::is_sequence< std::vector<int> > ));
56 BOOST_MPL_ASSERT_NOT(( is_sequence< int > ));
57 BOOST_MPL_ASSERT(( traits::is_sequence<__list__<> > ));
58 BOOST_MPL_ASSERT(( traits::is_sequence<__list__<int> > ));
59 BOOST_MPL_ASSERT(( traits::is_sequence<__vector__<> > ));
60 BOOST_MPL_ASSERT(( traits::is_sequence<__vector__<int> > ));
68 Metafunction that evaluates to `mpl::true_` if a certain type `T` is a
69 conforming Fusion __view__, `mpl::false_` otherwise. A view is a
70 specialized sequence that does not actually contain data. Views hold
71 sequences which may be other views. In general, views are held by other
72 views by value, while non-views are held by other views by reference. `is_view`
73 may be specialized to accommodate clients providing Fusion conforming views.
82 typedef __unspecified__ type;
89 [[Parameter] [Requirement] [Description]]
90 [[`T`] [Any type] [The type to query.]]
93 [heading Expression Semantics]
95 typedef traits::is_view<T>::type c;
97 [*Return type]: An __mpl_boolean_constant__.
99 [*Semantics]: Metafunction that evaluates to `mpl::true_` if a certain type
100 `T` is a conforming Fusion view, `mpl::false_` otherwise.
104 #include <boost/fusion/support/is_view.hpp>
105 #include <boost/fusion/include/is_view.hpp>
109 BOOST_MPL_ASSERT_NOT(( traits::is_view<std::vector<int> > ));
110 BOOST_MPL_ASSERT_NOT(( traits::is_view<int> ));
113 using boost::is_pointer;
114 typedef __vector__<int*, char, long*, bool, double> vector_type;
115 typedef __filter_view__<vector_type, is_pointer<_> > filter_view_type;
116 BOOST_MPL_ASSERT(( traits::is_view<filter_view_type> ));
122 [heading Description]
124 All conforming Fusion sequences and iterators have an associated tag type. The
125 purpose of the tag is to enable __tag_dispatching__ from __intrinsic__
126 functions to implementations appropriate for the type.
128 This metafunction may be specialized to accommodate clients providing Fusion
129 conforming sequences.
135 template<typename Sequence>
138 typedef __unspecified__ type;
145 [[Parameter] [Requirement] [Description]]
146 [[`T`] [Any type] [The type to query.]]
149 [heading Expression Semantics]
151 typedef traits::tag_of<T>::type tag;
153 [*Return type]: Any type.
155 [*Semantics]: Returns the tag type associated with `T`.
159 #include <boost/fusion/support/tag_of.hpp>
160 #include <boost/fusion/include/tag_of.hpp>
164 typedef traits::tag_of<__list__<> >::type tag1;
165 typedef traits::tag_of<__list__<int> >::type tag2;
166 typedef traits::tag_of<__vector__<> >::type tag3;
167 typedef traits::tag_of<__vector__<int> >::type tag4;
169 BOOST_MPL_ASSERT((boost::is_same<tag1, tag2>));
170 BOOST_MPL_ASSERT((boost::is_same<tag3, tag4>));
174 [section category_of]
176 [heading Description]
178 A metafunction that establishes the conceptual classification of a particular
179 __sequence__ or __iterator__ (see __iterator_concepts__ and
180 __sequence_concepts__).
186 template <typename T>
189 typedef __unspecified__ type;
196 [[Parameter] [Requirement] [Description]]
197 [[`T`] [Any type] [The type to query.]]
200 [heading Expression Semantics]
202 typedef traits::category_of<T>::type category;
206 The return type is derived from one of:
208 namespace boost { namespace fusion
210 struct incrementable_traversal_tag {};
212 struct single_pass_traversal_tag
213 : incrementable_traversal_tag {};
215 struct forward_traversal_tag
216 : single_pass_traversal_tag {};
218 struct bidirectional_traversal_tag
219 : forward_traversal_tag {};
221 struct random_access_traversal_tag
222 : bidirectional_traversal_tag {};
227 namespace boost { namespace fusion
229 struct associative_tag {};
231 struct unbounded_tag {};
234 [*Semantics]: Establishes the conceptual classification of a particular
235 __sequence__ or __iterator__.
239 #include <boost/fusion/support/category_of.hpp>
240 #include <boost/fusion/include/category_of.hpp>
244 using boost::is_base_of;
245 typedef traits::category_of<__list__<> >::type list_category;
246 typedef traits::category_of<__vector__<> >::type vector_category;
247 BOOST_MPL_ASSERT(( is_base_of<forward_traversal_tag, list_category> ));
248 BOOST_MPL_ASSERT(( is_base_of<random_access_traversal_tag, vector_category> ));
254 [heading Description]
255 Metafunction to apply __element_conversion__ to the full argument type.
257 It removes references to `const`, references to array types are kept, even
258 if the array is `const`. Reference wrappers are removed (see
259 __note_ref_wrappers__)[footnote Since C++11, the standard reference wrappers
264 #include <boost/fusion/support/deduce.hpp>
265 #include <boost/fusion/include/deduce.hpp>
270 template <typename T>
273 typedef __unspecified__ type;
278 template <typename T>
281 typename traits::deduce<T const &>::type element;
288 template <typename T>
289 holder<T> make_holder(T const & a)
295 * __deduce_sequence__
299 [section deduce_sequence]
301 [heading Description]
302 Applies __element_conversion__ to each element in a __forward_sequence__.
303 The resulting type is a __random_access_sequence__ that provides a converting
304 constructor accepting the original type as its argument.
308 #include <boost/fusion/support/deduce_sequence.hpp>
309 #include <boost/fusion/include/deduce_sequence.hpp>
314 template <class Sequence>
315 struct deduce_sequence
317 typedef __unspecified__ type;
325 typename traits::deduce_sequence<Seq>::type element;
327 holder(Seq const & a)
332 template <typename T0, typename T1>
333 holder< __vector__<T0 const &, T1 const &> >
334 make_holder(T0 const & a0, T1 const & a1)
336 typedef __vector__<T0 const &, T1 const &> arg_vec_t;
337 return holder<arg_vec_t>( arg_vec_t(a0,a1) );
347 [heading Description]
349 Fusion `pair` type is a half runtime pair. A half runtime pair is similar
350 to a __std_pair__, but, unlike __std_pair__, the first type does not have data.
351 It is used as elements in __map__\ s, for example.
355 template <typename First, typename Second>
360 template <typename Pair>
363 template <typename Pair>
366 template <typename First, typename Second>
370 template <typename First, typename Second>
371 typename result_of::make_pair<First,Second>::type
372 make_pair(Second const &);
374 [heading Template parameters]
377 [[Parameter] [Description]]
378 [[First] [The first type. This is purely a type. No data is held.]]
379 [[Second] [The second type. This contains data.]]
382 [variablelist Notation
383 [[`P`] [Fusion pair type]]
384 [[`p`, `p2`] [Fusion pairs]]
385 [[`F`, `S`] [Arbitrary types]]
386 [[`s`] [Value of type `S`]]
387 [[`o`] [Output stream]]
388 [[`i`] [Input stream]]
391 [heading Expression Semantics]
394 [[Expression] [Semantics]]
395 [[`P::first_type`] [The type of the first template parameter, `F`, equivalent to
396 `result_of::first<P>::type`. ]]
397 [[`P::second_type`] [The type of the second template parameter, `S`, equivalent to
398 `result_of::second<P>::type`. ]]
399 [[`P()`] [Default construction.]]
400 [[`P(s)`] [Construct a pair given value for the second type, `s`.]]
401 [[`P(p2)`] [Copy constructs a pair from another pair, `p2`.]]
402 [[`p.second`] [Get the data from `p1`.]]
403 [[`p = p2`] [Assigns a pair, `p1`, from another pair, `p2`.]]
404 [[make_pair<F>(s)] [Make a pair given the first type, `F`, and a value for
405 the second type, `s`. The second type assumes the type of `s`]]
406 [[`o << p`] [Output `p` to output stream, `o`.]]
407 [[`i >> p`] [Input `p` from input stream, `i`.]]
408 [[`p == p2`] [Tests two pairs for equality.]]
409 [[`p != p2`] [Tests two pairs for inequality.]]
414 #include <boost/fusion/support/pair.hpp>
415 #include <boost/fusion/include/pair.hpp>
419 pair<int, char> p('X');
420 std::cout << p << std::endl;
421 std::cout << make_pair<int>('X') << std::endl;
422 assert((p == make_pair<int>('X')));