Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why std::function<boost::any ()> won't work in this situation?

Tags:

c++

c++11

boost

I faced a situation where I want this kind of function:

MoveOnly createMoveOnly();

Stored here:

std::function<boost::any ()> factory = &createMoveOnly;

This should work, AFAIK, because MoveOnly is convertible to boost::any

Using boost 1.55 which supports move semantics for boost.any, it does not work. It triggers an error about trying to use the deleted copy constructor for MoveOnlyinternally in the holder for boost::any. But the top-level constructor for Boost.Any is correctly chosen (It uses a templated ValueType && that forwards parameters).

Maybe the problem is in std::function.

Any hints?

like image 378
Germán Diago Avatar asked Aug 07 '14 18:08

Germán Diago


1 Answers

I think that boost::any requires a copy constructor and assignment operator. The documentation seems to indicate this:

As the emphasis of a value lies in its state not its identity, values can be copied and typically assigned one to another, requiring the explicit or implicit definition of a public copy constructor and public assignment operator. Values typically live within other scopes, i.e. within objects or blocks, rather than on the heap. Values are therefore normally passed around and manipulated directly as variables or through references, but not as pointers that emphasize identity and indirection.

http://www.boost.org/doc/libs/1_55_0/doc/html/any/reference.html

like image 142
Brad Avatar answered Sep 22 '22 08:09

Brad