Boost logo

Boost :

Subject: Re: [boost] [local] Any "active Boost library author" in favor of Boost.Local?
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2011-11-25 12:44:25


On Fri, Nov 25, 2011 at 12:32 PM, Vicente J. Botet Escriba
<vicente.botet_at_[hidden]> wrote:
> Le 25/11/11 18:18, Lorenzo Caminiti a écrit :
>>
>> On Fri, Nov 25, 2011 at 11:06 AM, Vicente Botet
>> <vicente.botet_at_[hidden]>  wrote:
>>>
>>> * access to the non public functions of the embedding class (in case of a
>>> local function of a member function).
>>
>> Can you explain this point better? This example shows that you can
>> access public, protected, and private members of a bound object (from
>> within a member of the same class):
>>
>> #include<boost/local/function.hpp>
>> #include<iostream>
>>
>> struct x {
>>     void f() {
>>         void BOOST_LOCAL_FUNCTION_PARAMS(bind this) {
>>             this_->priv();
>>             this_->prot();
>>             this_->publ();
>>         } BOOST_LOCAL_FUNCTION_NAME(l)
>>         l();
>>     }
>>
>> 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;
>> }
>
> Sorry, I miss Boost.Local was able to do that. Where in the documentation
> can I find the local function can access the non-public interface?

It's nowhere in the docs... my bad :)

On the contrary such a feature is not supported by the Phoenix
solution *AFAIK* precisely because the functor is not local to the
outer class (whereas the Local functor is local to the enclosing
member function and therefore to the outer class). For example:

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

namespace impl
{
    template<typename This>
    void f(This this_)
    {
        this_->publ();
        // this_->prot(); // error :(
        // this_->priv(); // error :(
    }
}

BOOST_PHOENIX_ADAPT_FUNCTION(void, globalf, impl::f, 1)

struct x {
    void f() {
        globalf(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; }
};

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

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 :)

Thanks.
--Lorenzo


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