Subscript operator[] error with Boost C++ Phoenix user-defined argument

女生的网名这么多〃 提交于 2019-12-10 19:56:59

问题


With an existing Boost Phoenix (placeholder) argument, such as _1, I can use the array/subscript operator. For example, the following excerpt will display a 1.

int arr[4] = {1,2,3,4};
std::cout << _1[0](arr) << std::endl;

However, if I define my own placeholder argument:

phoenix::actor<phoenix::expression::argument<1>::type> const my_1 = {{}};

though it works fine unadorned (the following outputs a 7):

std::cout << my_1(7) << std::endl;

if I attempt to use the subscript operator (as above):

std::cout << my_1[0](arr) << std::endl;

I get many errors; in summary, with G++ 4.7.2, template argument deduction fails; with Clang 3.2, I'm told that a function cannot return an array type.

How can I make my Phoenix placeholder argument support the subscript operator?


回答1:


Just get rid of the actor part and it works fine:

#include <iostream>
#include <boost/phoenix.hpp>

int main()
{
    namespace phx = boost::phoenix;

    phx::expression::argument<1>::type const my_1 = {{{}}};
    int arr[4] = { 1, 2, 3, 4 };
    std::cout << my_1[0](arr) << '\n';
}

Online demo



来源:https://stackoverflow.com/questions/15397899/subscript-operator-error-with-boost-c-phoenix-user-defined-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!