Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r84229 - in trunk: boost libs/any
From: antoshkka_at_[hidden]
Date: 2013-05-10 15:23:34


Author: apolukhin
Date: 2013-05-10 15:23:34 EDT (Fri, 10 May 2013)
New Revision: 84229
URL: http://svn.boost.org/trac/boost/changeset/84229

Log:
Make Boost.Any more conformant to draft:
* tune boost::any_cast to work with temporaries and add some more test (partially refs #6999)
Text files modified:
   trunk/boost/any.hpp | 15 +++++++++++++++
   trunk/libs/any/any_test.cpp | 36 +++++++++++++++++++++++++++++++++++-
   2 files changed, 50 insertions(+), 1 deletions(-)

Modified: trunk/boost/any.hpp
==============================================================================
--- trunk/boost/any.hpp (original)
+++ trunk/boost/any.hpp 2013-05-10 15:23:34 EDT (Fri, 10 May 2013)
@@ -21,6 +21,7 @@
 #include <boost/type_traits/remove_reference.hpp>
 #include <boost/type_traits/decay.hpp>
 #include <boost/type_traits/is_reference.hpp>
+#include <boost/type_traits/is_const.hpp>
 #include <boost/throw_exception.hpp>
 #include <boost/static_assert.hpp>
 #include <boost/utility/enable_if.hpp>
@@ -307,6 +308,20 @@
         return any_cast<const nonref &>(const_cast<any &>(operand));
     }
 
+#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
+ template<typename ValueType>
+ inline ValueType&& any_cast(any&& operand)
+ {
+ BOOST_STATIC_ASSERT_MSG(
+ boost::is_rvalue_reference<ValueType&&>::value
+ || boost::is_const< typename boost::remove_reference<ValueType>::type >::value,
+ "boost::any_cast shall not be used for getting nonconst references to temporary objects"
+ );
+ return any_cast<ValueType&&>(operand);
+ }
+#endif
+
+
     // Note: The "unsafe" versions of any_cast are not part of the
     // public interface and may be removed at any time. They are
     // required where we know what type is stored in the any and can't

Modified: trunk/libs/any/any_test.cpp
==============================================================================
--- trunk/libs/any/any_test.cpp (original)
+++ trunk/libs/any/any_test.cpp 2013-05-10 15:23:34 EDT (Fri, 10 May 2013)
@@ -37,6 +37,7 @@
     void test_null_copying();
     void test_cast_to_reference();
     void test_with_array();
+ void test_with_func();
 
     const test_case test_cases[] =
     {
@@ -49,7 +50,8 @@
         { "swap member function", test_swap },
         { "copying operations on a null", test_null_copying },
         { "cast to reference types", test_cast_to_reference },
- { "storing an array inside", test_with_array }
+ { "storing an array inside", test_with_array },
+ { "implicit cast of returned value",test_with_func }
     };
 
     const test_case_iterator begin = test_cases;
@@ -270,6 +272,38 @@
         check_null(any_cast<const char[1]>(&value2), "any_cast<const char[1]>");
     }
 
+ const std::string& returning_string1()
+ {
+ static const std::string ret("foo");
+ return ret;
+ }
+
+ std::string returning_string2()
+ {
+ static const std::string ret("foo");
+ return ret;
+ }
+
+ void test_with_func()
+ {
+ std::string s;
+ s = any_cast<std::string>(returning_string1());
+ s = any_cast<const std::string&>(returning_string1());
+ //s = any_cast<std::string&>(returning_string1());
+
+ s = any_cast<std::string>(returning_string2());
+ s = any_cast<const std::string&>(returning_string2());
+ //s = any_cast<std::string&>(returning_string2());
+
+#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
+ //s = static_cast<std::string&&>(any_cast<std::string&>(returning_string1()));
+ s = any_cast<std::string&&>(returning_string1());
+
+ //s = static_cast<std::string&&>(any_cast<std::string&>(returning_string2()));
+ s = any_cast<std::string&&>(returning_string2());
+#endif
+ }
+
 }
 
 // Copyright Kevlin Henney, 2000, 2001. All rights reserved.


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk