使用Boost C++ Phoenix用户定义的参数的下标operator []错误

use*_*370 3 c++ templates boost boost-phoenix boost-proto

使用现有的Boost Phoenix(占位符)参数_1,我可以使用数组/下标运算符.例如,以下摘录将显示a 1.

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

但是,如果我定义自己的占位符参数:

phoenix::actor<phoenix::expression::argument<1>::type> const my_1 = {{}};
Run Code Online (Sandbox Code Playgroud)

虽然它工作正常朴素(以下输出7):

std::cout << my_1(7) << std::endl;
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用下标运算符(如上所述):

std::cout << my_1[0](arr) << std::endl;
Run Code Online (Sandbox Code Playgroud)

我收到很多错误; 总之,使用G ++ 4.7.2,模板参数推导失败; 使用Clang 3.2,我被告知函数不能返回数组类型.

如何让我的Phoenix占位符参数支持下标运算符?

ild*_*arn 5

只是摆脱该actor部分,它工作正常:

#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';
}
Run Code Online (Sandbox Code Playgroud)

在线演示

  • @ user2023370:对,就像你提到的那样,它是`const`.以下都可以工作:`static_assert(std :: is_same <phx :: expression :: argument <1> :: type const,decltype(phx :: arg_names :: _ 1)> :: value,"");`或`static_assert(std :: is_same <phx :: expression :: argument <1> :: type,std :: remove_const <decltype(phx :: arg_names :: _ 1)> :: type> :: value,""); `或`static_assert(std :: is_same <decltype(phx :: arg_names :: _ 1),decltype(my_1)> :: value,"");`. (2认同)