Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

boost::any and move constructible objects

14 views
Skip to first unread message

bitrex

unread,
Nov 24, 2016, 11:14:07 AM11/24/16
to
From this it looks like boost::any/std::any currently only supports
objects which are copy-constructible, and not simply move-constructible.

https://www.reddit.com/r/cpp/comments/4fyt3v/why_doesnt_stdany_support_move_only_types/

I noticed from the boost/any.hpp that if I'm using C++11 or greater, the
code first checks to see if the class is copy-constructible, and if it
is it then checks the C++ version to see if it's C++11 or greater
(#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES) and it looks like it then
uses the move constructor anyway.

That's a bit annoying. Is writing a wrapper class the only workaround?

Öö Tiib

unread,
Nov 24, 2016, 12:07:45 PM11/24/16
to
Why it is annoying?

Things that are not copyable are usually passed around by additional
indirection (like smart pointer or handle) anyway. So one workaround
is to put that thing into that 'any' instead of object itself.

Other workaround is not to use 'any'. It is indeed safer than things
like void* or ellipsis function argument but so what? I never need
these either.

bitrex

unread,
Nov 24, 2016, 12:35:33 PM11/24/16
to
On 11/24/2016 12:07 PM, Öö Tiib wrote:
> On Thursday, 24 November 2016 18:14:07 UTC+2, bitrex wrote:
>> From this it looks like boost::any/std::any currently only supports
>> objects which are copy-constructible, and not simply move-constructible.
>>
>> https://www.reddit.com/r/cpp/comments/4fyt3v/why_doesnt_stdany_support_move_only_types/
>>
>> I noticed from the boost/any.hpp that if I'm using C++11 or greater, the
>> code first checks to see if the class is copy-constructible, and if it
>> is it then checks the C++ version to see if it's C++11 or greater
>> (#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES) and it looks like it then
>> uses the move constructor anyway.
>>
>> That's a bit annoying. Is writing a wrapper class the only workaround?
>
> Why it is annoying?
>
> Things that are not copyable are usually passed around by additional
> indirection (like smart pointer or handle) anyway. So one workaround
> is to put that thing into that 'any' instead of object itself.

Hmm, yes. I guess that would make sense, wouldn't it ;-)

> Other workaround is not to use 'any'. It is indeed safer than things
> like void* or ellipsis function argument but so what? I never need
> these either.

It looks like it's about 1000 times slower in practice than say,
boost::variant too.

I was mostly hoping to use it as an argument to a constructor so I could
just pass in a ref to (any) object and extract a particular method from
that object into a boost::function. Should work just fine passing in the
smart pointer I guess


0 new messages