|
Boost Users : |
Subject: Re: [Boost-users] [Any] inconsistent return types of any_cast overloads
From: Michael Chisholm (chisholm_at_[hidden])
Date: 2013-12-04 14:16:16
On 12/4/2013 7:07 AM, Krzysztof Czainski wrote:
> 2013/12/4 Arne Vogel <avogel_at_[hidden]>
>
>> On 03.12.2013 00:01, Krzysztof Czainski wrote:
>> [...] what do you gain by doing:
>>
>> const auto& vec = boost::any_cast<std::vector<int>>(makeVec());
>> instead of
>> const auto vec = boost::any_cast<std::vector<int>>(makeVec());
>> ?
>>
>> Regards,
>> Kris
>>
>>
>> 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.
>>
>
> 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
>
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/184403758
2. http://loki-lib.sourceforge.net/
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net