Problems with Polymorphic functor

Previous Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Problems with Polymorphic functor

Martin MacRobert
Hi Spirit People,
I'm working my way slowly through the phoenix documentation making my own examples to come to terms with the concepts presented. I'm having problems getting one example to build (attached).

The problem I'm having is the enormous compile error output, as well as identifying what the real problem is.

Any feedback would be gratefully received.
Thanks,
Martin

TestPolymorphicFunctions.h (964 bytes) Download Attachment
println.h (286 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problems with Polymorphic functor

Joel de Guzman-2
Martin MacRobert wrote:
> Hi Spirit People,
> I'm working my way slowly through the phoenix documentation making my
> own examples to come to terms with the concepts presented. I'm having
> problems getting one example to build (attached).
>
> The problem I'm having is the enormous compile error output, as well as
> identifying what the real problem is.
>
> Any feedback would be gratefully received.

Try this (attached).

A couple of errors.

1) Your result type deduction is written incorrectly. There's a
    missing argument. It is a binary function. As such, it must
    have the sig: template <typename A, typename B>
2) println(add(arg1, arg2)(std::string("Hello "), "world")); will
    not compile. see "Forwarding Function Problem" here:
    http://tinyurl.com/5xj5a

Is that really an example from the docs? If so, could you point
mr to where you got it?

Cheers,
--
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net


#include <string>
#include <boost/spirit/phoenix/functions.hpp>
#include <boost/spirit/phoenix/primitives.hpp>
#include <iostream>

template <typename T>
void println(const T& t) { std::cout<<t<<std::endl; }
void println() { std::cout<<std::endl; }


// define our add function
struct add_impl
{
        template <typename ArgT, typename Arg2T>
        struct result { typedef ArgT type; };
       
        template <typename ArgT, typename Arg2T>
        ArgT operator()(const ArgT& lhs, const Arg2T& rhs)
        {
                return lhs + rhs;
        }
};

void TestPolymorphicFunctions()
{
        using namespace std;
        using namespace phoenix;
       
        // define the phoenix functional
        function<add_impl> add;
       
    std::string s("Hello ");
        println(add(arg1, arg2)(s, "world"));
}

int
main()
{
    TestPolymorphicFunctions();
}


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problems with Polymorphic functor

Martin MacRobert
In reply to this post by Martin MacRobert
Hi Joel,

Having fixed the type-deduction problem I run into the Forwarding
function problem and get inconsistent results from different
compilers, but at least I know why.

The code is my own practice attempts at creating my own phoenix
functions. It is not from the docs. However, if it may be useful to
other learners, I have no problem with it being used for illustrative
purposes in the Phoenix docs. It is inspired by the Polymorphic
Functions section at: http://tinyurl.com/3khbu

The "add" functional was not explicitly described, so I thought I'd
have a go at writing it.

I've attached my modified version of phx_test.cpp, showing the compiler results.

Regards,
Martin

>
> Martin MacRobert wrote:
> > Hi Spirit People,
> > I'm working my way slowly through the phoenix documentation making my
> > own examples to come to terms with the concepts presented. I'm having
> > problems getting one example to build (attached).
> >
> > The problem I'm having is the enormous compile error output, as well as
> > identifying what the real problem is.
> >
> > Any feedback would be gratefully received.
>
> Try this (attached).
>
> A couple of errors.
>
> 1) Your result type deduction is written incorrectly. There's a
>     missing argument. It is a binary function. As such, it must
>     have the sig: template <typename A, typename B>
> 2) println(add(arg1, arg2)(std::string("Hello "), "world")); will
>     not compile. see "Forwarding Function Problem" here:
>     http://tinyurl.com/5xj5a
>
> Is that really an example from the docs? If so, could you point
> mr to where you got it?
>
> Cheers,
> --
> Joel de Guzman
> http://www.boost-consulting.com
> http://spirit.sf.net
>

phx_test.cpp (2K) Download Attachment
Loading...