KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have an untemplated functor object that I'm trying to store as a std::function inside another object. This object is really heavyweight, so it's marked as uncopyable, but it does have a move constructor. However, trying to construct a std::function, or assign it, from a temporary constructor fails. Here is a minimal example to provoke the error. // pretend this is a really heavyweight functor that can't be copied. struct ExampleTest { int x; int operator()(void) const {return x*2;} ExampleTest( ) :x(0){} ExampleTest( int a ) :x(a){} // allow move ExampleTest( ExampleTest &&other ) :x(other.x) {}; private: // disallow copy, assignment ExampleTest( const ExampleTest &other ); void operator=( const ExampleTest &other ); }; // this sometimes stores really big functors and 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 an even simpler construction, where I'm just making a local function, I also get the error: int ContrivedExample( ) { // extra parens to sidestep most 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 as I can tell, in all of these cases, a tempo
Tags (comma-separated)
Save Edits
Cancel