【问题标题】:Subscript operator[] error with Boost C++ Phoenix user-defined argumentBoost C++ Phoenix 用户定义参数的下标运算符 [] 错误
【发布时间】:2013-03-13 22:39:29
【问题描述】:

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

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

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

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

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

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

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

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

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

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

【问题讨论】:

    标签: c++ templates boost boost-phoenix boost-proto


    【解决方案1】:

    只需去掉 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';
    }
    

    Online demo

    【讨论】:

    • 我看到decltype(phx::arg_names::_1) 也可以工作,尽管类型显然是const type。奇怪的是,decltype(phx::arg_names::_2) 也是如此。
    • @user2023370 :phx::arg_names::_1 的声明与 my_1 的声明相同,除了它所在的范围之外,所以我当然希望这会起作用。 ;-]
    • 好的。只是我发现这个失败了:static_assert(std::is_same&lt;phx::expression::argument&lt;1&gt;::type,decltype(phx::arg_names::_1)&gt;::value,"");
    • @user2023370 :对,正如你提到的,它是const。以下都可以工作:static_assert(std::is_same&lt;phx::expression::argument&lt;1&gt;::type const, decltype(phx::arg_names::_1)&gt;::value, "");static_assert(std::is_same&lt;phx::expression::argument&lt;1&gt;::type, std::remove_const&lt;decltype(phx::arg_names::_1)&gt;::type&gt;::value, "");static_assert(std::is_same&lt;decltype(phx::arg_names::_1), decltype(my_1)&gt;::value, "");
    猜你喜欢
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    相关资源
    最近更新 更多