是MPL POS无证元函数? [英] Is MPL pos an undocumented metafunction?
问题描述
有在的的BOOST MPL文档找到
算法:
的typedef矢量<字符,整型,无符号长,无符号长>类型;
typedef的查找和LT;类型,无符号> ::类型ITER;
...
BOOST_MPL_ASSERT_RELATION(ITER :: POS ::价值,== 2);
不过,我找不到迭代器的 POS
元函数的文档。我可以可靠地使用它吗?
我想以某种方式使用它作为:
的typedef矢量< 1型,2型,3型>类型;模板< typename的T>
无效文件::写(T值){
BOOST_MPL_ASSERT((包含<类型,T>));
无符号的typeid =找到<类型,T> ::类型:: POS ::值;
FSTR<< TYPEID<<值;
}
到的类型的信息连同值本身存储到文件中。
修改
感谢Potatoswatter的答案,这个方案似乎工作:
模板< typename的S,typename的T>
结构POS:距离<类型名称开始< S> ::类型,类型名称查找和LT; S,T> ::类型>
{};...
无符号的typeid = POS<类型,T> ::值;
元函数看起来像 FN< ITER> ::值
。这简直是迭代器类型的成员。
Inituitively,我会说,成员是专门针对从结果迭代找到
或者类似的功能。在任何情况下,如你所说,这是无证。不要以为每个迭代器有一个 POS
成员。
的距离
元函数应该提供这个功能,尽管它可能会比较慢。
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.
这篇关于是MPL POS无证元函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!