为什么类型知识在Boost::MPL中消失了?
Why does type knowledge disappear with Boost::MPL?
我有下面的代码,它工作得很好。
#include <boostmplvector.hpp>
#include <boostmplfold.hpp>
#include <boostmplfor_each.hpp>
#include <boostmplinherit.hpp>
#include <boostmplinherit_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 Generate'转换为'wrap &'[T = int)
为什么它在main中工作,而不是如果我把它放在一个模块中?如何将数据放入可以使用类型列表创建的数据值的位置,由类型列表驱动的一系列模板函数调用。基本上,我如何用这两个部分制作一个有用的物体?
如果您将print
中的operator()
更改为以下内容,则可能是可以编译的代码:
struct print {
...
void operator()(T) // remove const
或
static_cast<wrap<int>const&>(generated) // add const
相关文章:
- C++ :: 设置值函数在函数内设置,但后跟 Get 函数,设置值消失了
- 添加新纹理OpenGL时,模型消失了
- 当焦点丢失时,全屏GLFW窗口消失了
- 为什么当我写入文件时,结构在我的文件中消失了
- 我的地板平面的一部分消失了
- 最小化/最大化后,MFC窗口图形消失了
- 为什么我在使用矢量的 push_back() 函数后元素消失了
- 一秒钟后,OpenCV窗口消失了
- 字符串神秘地消失了,将字符串从C++带到对象C++
- Qpainter的情节消失了
- 为什么Visual Studio的控制台消失了?
- 为什么我的 cmd 消失了?我使用 cin.ignore(),但它没有效果!C++
- 尺寸手柄从MacOSX上的Qt 5.1的状态栏中消失了
- Qt Sqlite表列似乎消失了
- 为什么当我在OpenGL中启用照明时,我的颜色消失了?
- eclipse CDT 8.01 - 默认路径(libstdc,libstdc++)在"include"目录中完全消失了
- 当我将setPhysicsBody设置为sprite时,sprite消失了
- 苹果的 Using C++ with Objective-C 文章已经消失了......谁能给我一篇新文章,或者帮我找到旧文章?
- 当从c++传递缓冲区到NodeJS时,我的数据消失了
- 为什么类型知识在Boost::MPL中消失了?