Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #13373
| From | "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: How does a lambda actually get assigned to a std::function<>? |
| Date | 2012-01-14 22:24 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <jesrr7$6ot$1@dont-email.me> (permalink) |
| References | <671a92c2-96bc-4fe7-b471-1540176eb31e@t13g2000yqg.googlegroups.com> |
On 14.01.2012 21:42, K. Frank wrote:
>
> I'm looking for some insight into how an assignment such as:
>
> std::function<int, (int, int)> f = [z](int x, int y){ return x + y
> + z; };
>
> actually works under the hood.
Well, disregarding "actually", here's one possibility for your specific
example:
<code>
#include <iostream>
#include <vector>
#include <memory> // std::shared_ptr
class WrapperBase
{
public:
virtual ~WrapperBase() {}
};
template< class Functor >
class Wrapped
: public WrapperBase
{
public:
Functor f;
Wrapped( Functor const& _f ): f( _f ) {}
};
class MyFunc
{
private:
typedef int (*InvokerFunc)( void*, int, int );
std::shared_ptr< WrapperBase > pFunc_;
InvokerFunc invoker_;
template< class Functor >
static int invoke( void* pF, int x, int y )
{
return (reinterpret_cast< Wrapped< Functor>* >( pF )->f)( x, y );
}
public:
template< class Functor >
MyFunc( Functor const& f )
: pFunc_( new Wrapped< Functor >( f ) )
, invoker_( &invoke< Functor > )
{}
int operator()( int x, int y ) const
{
return invoker_( pFunc_.get(), x, y );
}
};
int main()
{
using namespace std;
MyFunc f = []( int x, int y ) -> int { return x*y; };
cout << f( 6, 7 ) << endl;
}
</code>
The technique used for the invoker function is called "type erasure",
and I believe it was developed (by the Boost guys) for just this
purpose, namely for the implementation of boost::function.
Cheers & hth.,
- Alf
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How does a lambda actually get assigned to a std::function<>? "K. Frank" <kfrank29.c@gmail.com> - 2012-01-14 12:42 -0800
Re: How does a lambda actually get assigned to a std::function<>? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-01-14 22:24 +0100
Re: How does a lambda actually get assigned to a std::function<>? "K. Frank" <kfrank29.c@gmail.com> - 2012-01-14 19:14 -0800
Re: How does a lambda actually get assigned to a std::function<>? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-01-15 14:27 +0100
Re: How does a lambda actually get assigned to a std::function<>? Peter Remmers <p.remmers@expires-2011-04-30.arcornews.de> - 2012-01-15 12:16 +0100
Re: How does a lambda actually get assigned to a std::function<>? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-01-15 14:24 +0100
Re: How does a lambda actually get assigned to a std::function<>? Peter Remmers <p.remmers@expires-2011-04-30.arcornews.de> - 2012-01-16 01:52 +0100
Re: How does a lambda actually get assigned to a std::function<>? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-01-16 05:32 +0100
Re: How does a lambda actually get assigned to a std::function<>? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-01-14 22:27 +0100
Re: How does a lambda actually get assigned to a std::function<>? "K. Frank" <kfrank29.c@gmail.com> - 2012-01-14 13:54 -0800
csiph-web