[Any] inconsistent return types of any_cast overloads

Hello Boost Users,
Code like the following used to work with Boost 1.53 on x86-64 Linux
using GCC 4.7 and -std=c++11:
boost::any makeVec()
{
return std::vector<int>{ 1, 2, 3 };
}
void foo()
{
const auto& vec = boost::any_cast

2013/12/2 Arne Vogel
You are absolutely right: ValueType&& any_cast(any&&) was not a good idea and it may break code that accepts result by const reference. Making it ValueType any_cast(any&&) would be more correct. I'll fix that as soon as the migration to the GIT will be finished. Great thanks for finding, investigating and reporting this issue! Created ticket #9462 : https://svn.boost.org/trac/boost/ticket/9462 -- Best regards, Antony Polukhin

On 03.12.2013 00:01, Krzysztof Czainski wrote:
Thanks a lot!
In C++11 with a moveable type, probably not much. I don't quite remember why I wrote the code like this - it could have been out of habit since in C++98 using a reference may save a copy, though I guess an optimizing compiler had already been allowed to elide it. Regards, Arne

2013/12/4 Arne Vogel
I'm just curious to know if anyone uses a compiler that doesn't actually elide the copy (or move) in this case ;-) In all discussions about this I've never seen someone actually name one, but still they try to avoid the copy just in case. I think taking values returned by value by const & to extend their lifetime should be deprecated in new C++ - compilers could then warn not to do it. Cheers, Kris

On Wed, Dec 4, 2013 at 7:07 AM, Krzysztof Czainski <1czajnik@gmail.com>wrote: I think taking values returned by value by const & to extend their lifetime
should be deprecated in new C++ - compilers could then warn not to do it.
I like that idea! One C++03 usage that has bothered me for a long time is a base class which stores one or more std::string data members, with virtual methods that return const std::string&. In an override method on one existing subclass, I decide to compute a std::string return value. But I can't return a temporary, and I can't return a stack variable! I have two bad choices: I can add a new private std::string data member to the subclass specifically to store the return value from this method; or I can refactor every declaration of this method in the entire class hierarchy to return by value. (We will loftily ignore the even worse choice of storing a module-static std::string for the method return value.) I have several times decided to refactor the method signature throughout the class hierarchy. But by the time I'm doing that much work, I typically also change the signature of every virtual method returning const std::string& to return by value -- to spare myself, or someone else, the pain of having to do it later. I've pretty much concluded that a virtual method returning const& is an anti-pattern. I like the idea of a compiler warning about it.

On 12/4/2013 7:07 AM, Krzysztof Czainski wrote:
An old article comes to mind which uses exactly this idea to implement a little API to help people write exception-safe code [1]. This is part of the Loki library [2]. I think this is still considered a legitimate technique, right? Andy 1. http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/18440375... 2. http://loki-lib.sourceforge.net/
participants (5)
-
Antony Polukhin
-
Arne Vogel
-
Krzysztof Czainski
-
Michael Chisholm
-
Nat Goodspeed