c++ - Can std::function be move-constructed from rvalue reference to a temporary functor object? -
i have untemplated functor object i'm trying store std::function
inside object. object heavyweight, it's marked uncopyable, have move constructor. however, trying construct std::function, or assign it, temporary constructor fails.
here minimal example provoke error.
// pretend heavyweight functor can't copied. struct exampletest { int x; int operator()(void) const {return x*2;} exampletest( ) :x(0){} exampletest( int ) :x(a){} // allow move exampletest( exampletest &&other ) :x(other.x) {}; private: // disallow copy, assignment exampletest( const exampletest &other ); void operator=( const exampletest &other ); }; // stores big functors , other times stores tiny lambdas. struct examplecontainer { examplecontainer( int ); std::function<int(void)> funct; }; /******** error: compiler error: 'exampletest::exampletest' : cannot access private member declared in class 'exampletest' ******************/ examplecontainer::examplecontainer( int x ) : funct( exampletest( x ) ) {} /******** error: compiler error: 'exampletest::exampletest' : cannot access private member declared in class 'exampletest' ******************/ int setexample( examplecontainer *container ) { container->funct = exampletest(); return container->funct(); }
in simpler construction, i'm making local function, error:
int contrivedexample( ) { // parens sidestep vexing parse std::function<int()> zug( (exampletest()) ); /*** error: 'exampletest::exampletest' : cannot access private member declared in class 'exampletest' */ int troz = zug( ) ; return troz; }
so far can tell, in of these cases, temporary exampletest ought passed function constructor rvalue. yet compiler wants copy them.
what gives? possible pass uncopyable (but move-copyable) functor objects std::function constructor? there workarounds pointers , on, want understand going on here.
the specific errors above visual studio 2012 ctp c++11 patch. gcc 4.8 , clang 3 fall down, own error messages.
this object heavyweight, it's marked uncopyable, have move constructor.
if functor non-copyable, not meet necessary requirements being used std::function
. paragraph 20.8.11.2.1/7 of c++11 standard specifies:
template<class f> function(f f); template <class f, class a> function(allocator_arg_t, const a& a, f f);
7 requires:
f
shallcopyconstructible
.f
shallcallable
(20.8.11.2) argument typesargtypes
, return typer
. copy constructor , destructor ofa
shall not throw exceptions.
Comments
Post a Comment