A Couple Of Newbie boost::phoenix::bind Questions

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

A Couple Of Newbie boost::phoenix::bind Questions

sergegers
1. Why is the following code compilation failed?

#include <boost/phoenix/core/value.hpp>
#include <boost/phoenix/bind/bind_function_object.hpp>

namespace ph = boost::phoenix;

int main()
{
    ph::bind(ph::val(9));
    return 0;
}

The compiler is MSVC++ 2015 RC.

2. More common question: I've got a phoenix function f(x) with one argument and a phoenix function g() w/o arguments. I want to make a composition like this: f(g()). Is the following code chunk correct?

ph::bind(f, ph::bind(g));
Reply | Threaded
Open this post in threaded view
|

Re: A Couple Of Newbie boost::phoenix::bind Questions

Michael Powell-2


On May 3, 2015 4:41:38 AM EDT, sergegers <[hidden email]> wrote:

>1. Why is the following code compilation failed?
>
>#include <boost/phoenix/core/value.hpp>
>#include <boost/phoenix/bind/bind_function_object.hpp>
>
>namespace ph = boost::phoenix;
>
>int main()
>{
>    ph::bind(ph::val(9));
>    return 0;
>}
>
>The compiler is MSVC++ 2015 RC.
>
>2. More common question: I've got a phoenix function f(x) with one
>argument
>and a phoenix function g() w/o arguments. I want to make a composition
>like
>this: f(g()). Is the following code chunk correct?
>
>ph::bind(f, ph::bind(g));

My first thought on that is that you should bind each one separately, using a placeholder for f(x).

How f necessarily 'sees' the g result, couldn't tell you without more of a working example.

Can you post an online code example?

>
>
>--
>View this message in context:
>http://boost.2283326.n4.nabble.com/A-Couple-Of-Newbie-boost-phoenix-bind-Questions-tp4674994.html
>Sent from the spirit-general mailing list archive at Nabble.com.
>
>------------------------------------------------------------------------------
>One dashboard for servers and applications across
>Physical-Virtual-Cloud
>Widest out-of-the-box monitoring support with 50+ applications
>Performance metrics, stats and reports that give you Actionable
>Insights
>Deep dive visibility with transaction tracing using APM Insight.
>http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
>_______________________________________________
>Spirit-general mailing list
>[hidden email]
>https://lists.sourceforge.net/lists/listinfo/spirit-general

--
Sent from my Android device with K-9 Mail. Please excuse my brevity.

------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: A Couple Of Newbie boost::phoenix::bind Questions

sergegers
#include <iostream>

#include <boost/phoenix/core/value.hpp>
#include <boost/phoenix/core/argument.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/bind/bind_function_object.hpp>

namespace ph = boost::phoenix;
namespace expr = boost::phoenix::expression;
namespace arg = boost::phoenix::arg_names;
namespace pr = boost::proto;

struct f
{
    template<class> struct result
    {
        typedef int type;
    };
    int operator()(int i) const { return i + 5; }
};

struct g
{
    template<class> struct result
    {
        typedef void type;
    };
    int operator()() const { return 42; }
};

int main()
{
    // This is compiled & worked.
    auto e = ph::bind(f {}, ph::bind(g {}));
    std::cout << e() << std::endl;

    // But this code isn't compiled despite of ph::val(42) is nullary phoenix
    // function.
    // auto e0 = ph::bind(f {}, ph::bind(ph::val(42)));
    return 0;
}
Reply | Threaded
Open this post in threaded view
|

Re: A Couple Of Newbie boost::phoenix::bind Questions

sergegers
more exactly, structure g must be defined as:

struct g
{
    template<class> struct result
    {
        typedef int type; // <- int here
    };
    int operator()() const { return 42; }
};
Reply | Threaded
Open this post in threaded view
|

Re: A Couple Of Newbie boost::phoenix::bind Questions

Joel de Guzman
In reply to this post by sergegers
On 5/3/15 7:01 PM, sergegers wrote:

> #include <iostream>
>
> #include <boost/phoenix/core/value.hpp>
> #include <boost/phoenix/core/argument.hpp>
> #include <boost/phoenix/operator.hpp>
> #include <boost/phoenix/bind/bind_function_object.hpp>
>
> namespace ph = boost::phoenix;
> namespace expr = boost::phoenix::expression;
> namespace arg = boost::phoenix::arg_names;
> namespace pr = boost::proto;
>
> struct f
> {
>      template<class> struct result
>      {
>          typedef int type;
>      };
>      int operator()(int i) const { return i + 5; }
> };
>
> struct g
> {
>      template<class> struct result
>      {
>          typedef void type;
>      };
>      int operator()() const { return 42; }
> };
>
> int main()
> {
>      // This is compiled & worked.
>      auto e = ph::bind(f {}, ph::bind(g {}));
>      std::cout << e() << std::endl;
>
>      // But this code isn't compiled despite of ph::val(42) is nullary
> phoenix
>      // function.
>      // auto e0 = ph::bind(f {}, ph::bind(ph::val(42)));
>      return 0;
> }

You do not need to bind the arguments. Here's the working code:

     auto e0 = ph::bind(f {}, ph::val(42));
     std::cout << e0() << std::endl;

val is also implied (default capture is by value, so:

     auto e0 = ph::bind(f {}, 42);

Regards,
--
Joel de Guzman
http://www.ciere.com
http://boost-spirit.com
http://www.cycfi.com/

------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: A Couple Of Newbie boost::phoenix::bind Questions

Joel de Guzman
In reply to this post by sergegers
On 5/3/15 4:41 PM, sergegers wrote:

> 1. Why is the following code compilation failed?
>
> #include <boost/phoenix/core/value.hpp>
> #include <boost/phoenix/bind/bind_function_object.hpp>
>
> namespace ph = boost::phoenix;
>
> int main()
> {
>      ph::bind(ph::val(9));
>      return 0;
> }
>
> The compiler is MSVC++ 2015 RC.
>
> 2. More common question: I've got a phoenix function f(x) with one argument
> and a phoenix function g() w/o arguments. I want to make a composition like
> this: f(g()). Is the following code chunk correct?
>
> ph::bind(f, ph::bind(g));

It's best to use phoenix functions. See tutorials in docs.

Regards,
--
Joel de Guzman
http://www.ciere.com
http://boost-spirit.com
http://www.cycfi.com/

------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general