Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does boost::any forbid the forwarding of const&&?

Tags:

c++

c++11

boost

Here is the relevant code:

// Perfect forwarding of ValueType
template<typename ValueType>
any(ValueType&& value
    , typename boost::disable_if<boost::is_same<any&, ValueType> >::type* = 0 // disable if value has type `any&`
    , typename boost::disable_if<boost::is_const<ValueType> >::type* = 0) // disable if value has type `const ValueType&&`
  : content(new holder< typename decay<ValueType>::type >(static_cast<ValueType&&>(value)))
{
}

For all I know, a copy constructor could be used to construct from a const&&. I use boost 1.55.0.

like image 281
user1095108 Avatar asked Jul 23 '14 12:07

user1095108


1 Answers

This is a fix for https://svn.boost.org/trac/boost/ticket/9215:

Infinite loop when any constructor called for const any&&

#include <boost/any.hpp>
#include <string>
const boost::any getBoolVal()
{
    return false;
}
int main()
{
    boost::any vals[] = {1.0, std::string("1m"), getBoolVal()};
}

So it looks like the intent is to exclude any const&& from the forwarding constructor; the fact that it excludes all const rvalues is a side effect, harmless because those will be handled by the ValueType const& copying constructor.

Personally, I would have gone for

typename disable_if<is_same<any,
    typename remove_cv<typename remove_reference<ValueType>::type>::type>>::type

which correctly handles volatile rvalues as well - not that I've ever seen one of those in the flesh.

like image 144
ecatmur Avatar answered Sep 22 '22 02:09

ecatmur