There is the following example code in the BOOST MPL documentation of the find
algorithm:
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's pos
metafunction. Can I use it reliably?
I'd like to use it somehow as:
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 store the type information into a file together with the value itself.
EDIT
Thanks Potatoswatter for the answer, this solution seems to work:
template <typename S, typename T>
struct pos : distance< typename begin<S>::type, typename find<S, T>::type >
{};
...
unsigned typeID = pos<types, T>::value;
Metafunctions look like fn< iter >::value
. That is simply a member of the iterator type.
Inituitively, I would say that member is specific to iterators resulting from find
or functions like it. In any case, as you say, it is undocumented. Do not assume that every iterator has a pos
member.
The distance
metafunction should provide this functionality, although it might be slower.