为什么类型知识会随着 Boost::MPL 消失?
我有以下代码并且工作正常。
#include <boost\mpl\vector.hpp>
#include <boost\mpl\fold.hpp>
#include <boost\mpl\for_each.hpp>
#include <boost\mpl\inherit.hpp>
#include <boost\mpl\inherit_linearly.hpp>
#include <iostream>
using namespace boost::mpl::placeholders;
typedef boost::mpl::vector<short[2], long, char*, int> member_types;
template <typename T>
struct wrap
{
T value;
};
struct print
{
template <typename T>
void operator()(T) const
{
std::cout << typeid(T).name() << std::endl;
}
};
typedef boost::mpl::inherit_linearly<member_types, boost::mpl::inherit<wrap<_2>, _1> >::type Generate;
void main()
{
Generate generated;
print p;
std::cout << static_cast<wrap<int>&>(generated).value << std::endl;
boost::mpl::for_each<member_types>(p);
}
但如果我这样修改它:
struct print
{
Generate generated;
template <typename T>
void operator()(T) const
{
std::cout << static_cast<wrap<int>&>(generated).value << std::endl;
}
};
我收到错误 错误 C2440:“static_cast”:无法从“const 生成”转换为“wrap &”和 [ T=整数 ]
为什么在main里可以用,但是放在模块里就不行了?如何将数据放入一个可以使用类型列表创建的数据值的位置,以便由类型列表驱动的一系列模板函数调用。基本上,我如何制作一个对这两个部分有用的对象?
I have the following code and it works fine.
#include <boost\mpl\vector.hpp>
#include <boost\mpl\fold.hpp>
#include <boost\mpl\for_each.hpp>
#include <boost\mpl\inherit.hpp>
#include <boost\mpl\inherit_linearly.hpp>
#include <iostream>
using namespace boost::mpl::placeholders;
typedef boost::mpl::vector<short[2], long, char*, int> member_types;
template <typename T>
struct wrap
{
T value;
};
struct print
{
template <typename T>
void operator()(T) const
{
std::cout << typeid(T).name() << std::endl;
}
};
typedef boost::mpl::inherit_linearly<member_types, boost::mpl::inherit<wrap<_2>, _1> >::type Generate;
void main()
{
Generate generated;
print p;
std::cout << static_cast<wrap<int>&>(generated).value << std::endl;
boost::mpl::for_each<member_types>(p);
}
but if I modify it like this:
struct print
{
Generate generated;
template <typename T>
void operator()(T) const
{
std::cout << static_cast<wrap<int>&>(generated).value << std::endl;
}
};
I get the error
error C2440: 'static_cast' : cannot convert from 'const Generate' to 'wrap &' with
[
T=int
]
Why does it work in main, but not if I put it into a module? How can I get the data into a place I can use the value of the data created by a typelist to be called by a sequence of template functions driven by a type list. Basically how do I make an object that does something useful with the two parts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果将
print
中的operator()
更改为以下内容,可能是可以编译代码:
或
If you change the
operator()
inprint
to the following, probably thecode can be compiled:
or