C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::any ehancement

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Mon, 13 May 2024 10:05:04 -0400
On Mon, May 13, 2024 at 3:45 AM Ahmed Elbadawy via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Dear C++ Committee,
>
> I hope this message finds you well. I am writing to propose an enhancement to the template class std::any in the C++ standard library. The proposed enhancement aims to simplify the usage of std::any by adding a conversion function, thereby eliminating the need for explicit std::any_cast operations.
>
> Currently, using std::any requires explicit casting using std::any_cast but adding a conversion function to std::any will allow users to convert the stored value to a specified type directly, without the need for std::any_cast which will
>
> Improve Readability: The proposed conversion function makes code more readable by eliminating the need for explicit casts.
> Simplified Usage: Developers can use the conversion function directly, reducing the complexity of working with std::any.
>
> Considering the scenario where std::any is used as an argument to an overloaded function. With the proposed conversion function, we can use the builtin static_cast which is more convenient to be used than the template function std::any_cast.
> The attached file is a simple implementation of the std::any with the conversion function and simple usage of it, I would appreciate it if you could review the attached file and provide feedback.

Since casting with `any` can throw (unless you cast from a pointer,
which your API doesn't support), that makes it an inherently dangerous
operation. You shouldn't have implicit operations which are dangerous.
And personally, I would say that a `static_cast` shouldn't throw
either.

Received on 2024-05-13 14:05:17