boost :: phoenix :: static_cast_编译错误

时间:2015-07-07 09:45:36

标签: c++ boost casting compiler-errors boost-phoenix

我尝试在boost phoenix lambda中静态转换参数时遇到编译错误。错误本身太长了,所以我把它们发布到了pastebin here

我创建了一个显示我想要做的最小例子。如果我将变量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。

1 个答案:

答案 0 :(得分:0)

错误消息似乎暗示您在那里使用MenuItem logoutItem = menuNav.findItem(R.id.menu_logout);

动态强制转换需要虚拟类。将虚拟成员添加到基类(例如析构函数)

示例:

<强> Live On Coliru

dynamic_cast_

打印

#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;
}