Boost::phoenix::static_cast_编译错误
boost::phoenix::static_cast_ compile errors
我遇到一个编译错误时,试图静态转换一个参数内的boost phoenix lambda。这些错误本身太长了,所以我把它们贴在了这里。
我已经创建了一个最小的例子,显示我正在尝试做什么。如果我把变量b变成A指针,因此不强制转换,那么一切都可以正确编译和运行。有人知道为什么会这样吗?
最小示例(使用"clang++ -lboost_thread phoenix_test.cpp"编译):
#include <boost/phoenix.hpp>
#include <iostream>
using namespace boost;
using namespace phoenix;
using namespace arg_names;
using namespace local_names;
class A
{
public:
A(int a)
: mA(a)
{};
int GetX() const {return mA;};
protected:
int mA;
};
class B : public A
{
public:
B(int a)
: A(a)
{};
int GetX() const { return mA + 1; }
};
int main (void)
{
const A* a = new A(3);
const A* b = new B(2);
BOOST_AUTO(test, (_1->*&A::GetX)() + (static_cast_<const B*>(_2)->*&B::GetX)());
std::cout << test(a, b) << std::endl;
delete a;
delete b;
return 0;
}
使用的编译器是clang 3.4, gcc 4.6.3也试过了
错误消息似乎暗示您在那里使用了dynamic_cast_
。
动态强制转换需要虚类。向基类添加虚成员(例如析构函数)
示例:Live On Coliru
#include <boost/phoenix.hpp>
#include <iostream>
namespace px = boost::phoenix;
using namespace px::arg_names;
using namespace px::local_names;
class A {
public:
virtual ~A() {}
A(int a) : mA(a){};
int GetX() const { return mA; };
protected:
int mA;
};
class B : public A {
public:
B(int a) : A(a){};
int GetX() const { return mA + 1; }
};
int main()
{
const A* a = new A(3);
const A* b = new B(2);
BOOST_AUTO(test, (_1->*&A::GetX)() + (px::dynamic_cast_<const B*>(_2)->*&B::GetX)());
std::cout << test(a, b) << std::endl;
delete a;
delete b;
return 0;
}
打印
6
相关文章:
- std::is_base_of表示ctor编译错误
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- 如何修复sfml c++代码编译错误
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- vscode下的Arduino代码出现意外编译错误
- 第三方 API 中的编译错误 - Visual Studio
- std::cout输出int时出现编译错误
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 提升图广度优先搜索前置编译错误
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 我在C++中遇到了这个奇怪的编译错误
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 现代OpenGL和GLEW Libraray的编译错误
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- C ++程序编译错误,找不到/访问文件
- 使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"
- 为什么传递非静态成员函数会导致编译错误?