Boost logo

Boost :

Subject: Re: [boost] [local] Any "active Boost library author" in favor of Boost.Local?
From: Thomas Heller (thom.heller_at_[hidden])
Date: 2011-11-25 14:42:27


On 11/25/2011 01:39 PM, Lorenzo Caminiti wrote:
> On Fri, Nov 25, 2011 at 2:17 PM, Thomas Klimpel
> <Thomas.Klimpel_at_[hidden]> wrote:
>> Mathias Gaunard wrote:
>>>> DISCLAIMER: I apologize in advance if there's a way to do access priv
>>>> and prot from the Phoenix global functor which I was not able to
>>> find.
>>>> I'm sure Phoenix experts will be able to correct my example if such a
>>>> possibility exists :)
>>>
>>> It's not possible with Phoenix, though I believe there are hacks to
>>> make it possible somewhat.
>>
>> I guess I would create a static member function and adapt it with Phoenix. Is this one of the hacks you imagined? Or were you more thinking in the direction of "friend" declarations?
>
> Yes, friendship will work as in the example below. However, please
> note that now the functor is not only non-local but it also has to
> explicitly override the class access level being declared as friend.
> In addition please also note that in order to declare the friendship
> you have to explicitly specify the bound type `int` (and you'll have
> to change the friendship declaration if you change the type of the
> bound variable a declared locally within the function).
>
> #include<boost/phoenix/core.hpp>
> #include<boost/phoenix/function.hpp>
> #include<iostream>
>
> namespace impl
> {
> template<typename This, typename A>
> void f(This this_, A a)
> {
> this_->publ();
> this_->prot();
> this_->priv();
> }
> }
>
> BOOST_PHOENIX_ADAPT_FUNCTION(void, globalf, impl::f, 2)
>
> struct x {
> friend void impl::f<x*, int>(x*, int);
>
> void f() {
> int a;
> globalf(this, a)();
> }
>
> public:
> void publ() { std::cout<< "public"<< std::endl; }
> protected:
> void prot() { std::cout<< "protected"<< std::endl; }
> private:
> void priv() { std::cout<< "private"<< std::endl; }
> };
>
> int main ( ) {
> x xx;
> xx.f();
> return 0;
> }

Observe:

#include <boost/phoenix/core.hpp>
#include <boost/phoenix/function.hpp>
#include <iostream>

struct x {
     void f() {
         // call it regularly:
         f_impl()(this);
         // turn it into a phoenix function:
         boost::phoenix::function<f_impl> localf = f_impl();
         localf(this)();
     }

public:
     void publ() { std::cout << "public" << std::endl; }
protected:
     void prot() { std::cout << "protected" << std::endl; }
private:
     void priv() { std::cout << "private" << std::endl; }

     struct f_impl
     {
         typedef void result_type;
         template<typename This>
         void operator()(This this_) const
         {
             this_->publ();
             this_->prot(); // error
             this_->priv(); // error
         }
     };
};

int main ( ) {
     x xx;
     xx.f();
     return 0;
}

>
> That's to Vicente 2nd feature. To Vicente 1st feature instead:
>
> On Fri, Nov 25, 2011 at 11:06 AM, Vicente Botet
> <vicente.botet_at_[hidden]> wrote:
>> I think that most of us would admit that local functions are useful, but as
>> discussed during the review, Boost.Local doesn't provides local functions.
>> The missing features been:
>> * implicit access to the accessible variables
>
> Please note that while it is true that with Boost.Local you have to
> explicitly say `bind this` (as in the local function declaration) with
> the Phoenix example you still have to explicitly say `this` (and in
> the call `globalf(this)();`). So in a sense both approaches fail to
> provide "implicit access to the accessible variables"... Did I
> understand the meaning of Vicente's 1st feature correctly?
>
> Thanks.
> --Lorenzo
>
> _______________________________________________
> Unsubscribe& other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk