Is MPL pos an undocumented metafunction?

こ雲淡風輕ζ 提交于 2019-12-14 01:23:28

问题


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;

回答1:


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.



来源:https://stackoverflow.com/questions/5666394/is-mpl-pos-an-undocumented-metafunction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!