Is MPL an undocumented metaphone?

The BOOST MPL find algorithm documentation has the following example:

 typedef vector<char,int,unsigned,long,unsigned long> types; typedef find<types,unsigned>::type iter; ... BOOST_MPL_ASSERT_RELATION( iter::pos::value, ==, 2 ); 

However, I cannot find the documentation for the iterator pos metafile. Can I use it reliably?

I would like to use it somehow like:

 typedef vector<type1, type2, type3> types; template <typename T> void File::write(T value) { BOOST_MPL_ASSERT((contains<types, T>)); unsigned typeID = find<types, T>::type::pos::value; fstr << typeID << value; } 

to save type information to a file along with the value itself.

EDIT

Thanks to Potatoswatter for the answer, it looks like this solution works:

 template <typename S, typename T> struct pos : distance< typename begin<S>::type, typename find<S, T>::type > {}; ... unsigned typeID = pos<types, T>::value; 
+3
source share
1 answer

Metafundations look like fn< iter >::value . It is just a member of an iterator type.

Inituitively, I would say that a member is specific to iterators that are the result of find or functions like it. In any case, as you say, this is undocumented. Do not assume that each iterator has a pos member.

Metafunction distance should provide this functionality, although it may be slower.

+4
source

All Articles