Boost::phoenix::static_cast_编译错误

boost::phoenix::static_cast_ compile errors

本文关键字:编译 错误 cast static phoenix Boost      更新时间:2023-10-16

我遇到一个编译错误时,试图静态转换一个参数内的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