如何使 boost-proto 函数表达式可流化?

发布于 2024-12-17 09:22:41 字数 2250 浏览 3 评论 0原文

我正在查看 boost-proto 教程,并在使用惰性 pow 函数示例时遇到了这个问题。这是示例代码:

// Define a pow_fun function object
template<int Exp> // , typename Func>
struct pow_fun
{
    typedef double result_type;
    double operator()(double d) const
    {
        return pow(d, Exp);
    }
};

// Define a lazy pow() function for the calculator DSEL.
// Can be used as: pow< 2 >(_1)
template<int Exp, typename Arg>
typename proto::result_of::make_expr<
    proto::tag::function  // Tag type
  , pow_fun<Exp>          // First child (by value)
  , Arg const &           // Second child (by reference)
>::type const
mypow(Arg const &arg)
{
    return proto::make_expr<proto::tag::function>(
        pow_fun<Exp>()    // First child (by value)
      , boost::ref(arg)   // Second child (by reference)
    );    
}

现在,如果我尝试

proto::display_expr( mypow<2>(_1) );

编译器会抱怨它没有运算符<<为 函数表达式。我如何定义一个?

谢谢。

编译器错误为:

/usr/include/boost/proto/debug.hpp:146: error: no match for 'operator<<'在 'std::operator<< 中[with _Traits = std::char_traits](((std::basic_ostream >&)((std::basic_ostream >*)std::operator<< [with _Traits = std::char_traits]((( std::basic_ostream >&)((std::basic_ostream >*)std::operator<< [with _Traits = std::char_traits](((std::basic_ostream >&)((std::basic_ostream >*)std::operator<< [与 _CharT = char,_Traits = std::char_traits](((std::basic_ostream >&)((std::ostream*)((const boost::proto::function::display_expr*)this)->boost::proto::function::display_expr::sout_)), std ::setw(((常量boost::proto::function::display_expr*)this)->boost::proto::function::display_expr::深度_)))), (((const boost::proto::function::display_expr* )this)->boost::proto::function::display_expr::first_ ? ((const char*)"") : ((const char*)", "))))), boost::proto::tag::proto_tag_name((boost::proto::tag::terminal(), boost::proto::tag::terminal()))))), ((const char*)"(")) << boost::proto::value [with Expr = boost::proto::exprns_::expr >, 0l>](((const boost::proto::exprns_::expr >, 0l>&)((const boost::proto::exprns_::expr >, 0l>*)expr)))'

I'm going over the boost-proto tutorial, and ran into this problem with the lazy pow function example. This is the example code:

// Define a pow_fun function object
template<int Exp> // , typename Func>
struct pow_fun
{
    typedef double result_type;
    double operator()(double d) const
    {
        return pow(d, Exp);
    }
};

// Define a lazy pow() function for the calculator DSEL.
// Can be used as: pow< 2 >(_1)
template<int Exp, typename Arg>
typename proto::result_of::make_expr<
    proto::tag::function  // Tag type
  , pow_fun<Exp>          // First child (by value)
  , Arg const &           // Second child (by reference)
>::type const
mypow(Arg const &arg)
{
    return proto::make_expr<proto::tag::function>(
        pow_fun<Exp>()    // First child (by value)
      , boost::ref(arg)   // Second child (by reference)
    );    
}

Now, if I try to

proto::display_expr( mypow<2>(_1) );

the compiler complains that it doesn't have operator<< for the
function expression. How do I define one?

Thanks.

The compiler error is:

/usr/include/boost/proto/debug.hpp:146: error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits](((std::basic_ostream >&)((std::basic_ostream >*)std::operator<< [with _Traits = std::char_traits](((std::basic_ostream >&)((std::basic_ostream >*)std::operator<< [with _Traits = std::char_traits](((std::basic_ostream >&)((std::basic_ostream >*)std::operator<< [with _CharT = char, _Traits = std::char_traits](((std::basic_ostream >&)((std::ostream*)((const boost::proto::functional::display_expr*)this)->boost::proto::functional::display_expr::sout_)), std::setw(((const boost::proto::functional::display_expr*)this)->boost::proto::functional::display_expr::depth_)))), (((const boost::proto::functional::display_expr*)this)->boost::proto::functional::display_expr::first_ ? ((const char*)"") : ((const char*)", "))))), boost::proto::tag::proto_tag_name((boost::proto::tag::terminal(), boost::proto::tag::terminal()))))), ((const char*)"(")) << boost::proto::value [with Expr = boost::proto::exprns_::expr >, 0l>](((const boost::proto::exprns_::expr >, 0l>&)((const boost::proto::exprns_::expr >, 0l>*)expr)))’

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫烠⑼条掵仅有一顆心 2024-12-24 09:22:41

这是哪个原型版本?最新的不需要 <<不再重载,如果需要,默认使用 typeid 来显示名称。您能发布实际的错误消息吗?

Which proto version is this ? The latest don't require the << overload anymore and default to typeid to display name if needed. Could you post the actual error message ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文