mardi 10 mars 2015

Lambda with variadic template

Faced with the problem, which heard only within the gcc compiler. I have been programming in Visual Studio 2013 (update 3).


I need to create some objects in the some right thread. Also, there is a need to be able to pass a function of their creation.


This code



class Object1;
class Object2;

class MyClass
{
public:

std::function<Object1*( const std::vector<int>& )> creatorForObject1() const;
std::function<Object2*( int, double )> creatorForObject2() const;

private:

// exec 'handler' in other thread when there is a possibility
void execInRightThread( std::function<void()> handler ) const;

// need be called in right thread
Object1& craeteObject1( const std::vector<int>& param );
Object2& craeteObject2( int param1, double param2 );

template<class TObject, class ...TParams>
std::function<TObject*( TParams... )> creatorForObject( TObject&( MyClass::*creator )( TParams... ) ) const;
};


template<class TObject, class ...TParams>
std::function<TObject*( TParams... )> MyClass::creatorForObject( TObject&( MyClass::*creator )( TParams... ) ) const
{
return [ this, &creator ]( TParams... params )
{
TObject* result = nullptr;
MyClass& me = *const_cast<MyClass*>( this );
me.execInRightThread( [ &me, &result, &creator, &params... ]()
{
result = &( me.*creator )( params... );
} );
while( result == nullptr ); // ugliness
return result;
};
}

std::function<Object1*( const std::vector<int>& )> MyClass::creatorForObject1() const
{
return creatorForObject( &MyClass::craeteObject1 );
}

std::function<Object2*( int, double )> MyClass::creatorForObject2() const
{
return creatorForObject( &MyClass::craeteObject2 );
}


report an errors



error C3521: 'params' is not a parameter pack
error C3546: '...' : there are no parameter packs available to expand
error C2065: 'params' : undeclared identifier


at the line



me.execInRightThread( [ &me, &result, &creator, &params... ]()


Please, help.


Thanks in advance.


Aucun commentaire:

Enregistrer un commentaire