C++ Logo

STD-PROPOSALS

Advanced search

Subject: Re: [std-proposals] Meta types
From: Михаил Найденов (mihailnajdenov_at_[hidden])
Date: 2020-02-04 10:03:00


On Tue, Feb 4, 2020 at 5:16 PM Arthur O'Dwyer via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Tue, Feb 4, 2020 at 8:16 AM Михаил Найденов via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>> This definitely steps into both "language variant" and Pattern Matching
>> territory.
>> I am not sure what optimization we can expect from "language variant",
>> but PM should give us close to your ideal code
>>
>> void main(int argc, char const* const* argv){
>> inspect(halve(args))
>> Numeric x => std::cout << "You passed 2*(" << x << ") arguments\n";
>> return 0;
>> }
>>
>

> The current Pattern Matching proposal doesn't give the programmer any way
> to write `halve` so that it can actually return either of two alternative
> types (`unsigned` or `double`) depending on a *runtime* condition (the
> evenness of the runtime argument).
>

A "language variant" should do just that, much like current variant, but
hopefully better in terms of performance and compiler optimizations.

PM will be used to unwrap the values.
BTW this example is *great* to showcase the need of as minimum syntax as
possible, so that people will feel as they are writing normal code.
In this example we want hide completely the fact we are writing a special
block to handle two different types. This is one of the OP requests.
Not sure if we can do better then the above though, probably making the
inspect parenths optional but not much else

>
> However, I don't see anything wrong with the existing C++14 language
> solution to OP's problem. It's not worth pursuing any crazy core-language
> gymnastics unless you can provide a use-case that isn't already solvable
> idiomatically in C++14 (17, 20). Here's the '14 solution:
>
> template<class F>
> void halve(unsigned i, const F& f) {
> if (i % 2 == 0) {
> f(i / 2);
> } else {
> f(i * 0.5);
> }
> }
>
> int main(int argc, char**) {
> halve(argc, [](auto x) {
> std::cout << "You passed 2*(" << x << ") arguments\n";
> });
> return 0;
> }
>
> Short, readable, relatively easy to understand. Certainly easi*er* to
> understand than anything proposed in the OP.
>
> –Arthur
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>



STD-PROPOSALS list run by herb.sutter at gmail.com

Standard Proposals Archives on Google Groups