Test* get_test() { /*...*/ }
Test* test = (get_test() != nullptr) ? get_test() : new Test();
Test* test = get_test();
test = test != nullptr ? test : new Test();
Test* test = get_test() ?? new Test();
some_pointer ?? throw std::invalid_argument("some_pointer is null.");
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/8a34f78a-53ec-48d3-934f-457fd6d949d7%40isocpp.org.
"""(Although the referenced link is to a paper that has nothing to do with the operator in question).
and the shortest work-around I can concoct quickly is
Test* test = []{Test* ret{get_test()}; return ret ? ret : new Test();}();
Not all that pretty, no.
I've decided to dip my toe into the standardization process. As such, I've come up with (what I think is) a small feature I really like. I have to do a lot of programming in [other language] at work, and something I greatly miss when getting back to C++ is the null coalescing operator. It is a quality of life feature that simplifies and compacts patterns of code that are used throughout codebases.Consider the following conditional operator use case:
Test* get_test() { /*...*/ }
Test* test = (get_test() != nullptr) ? get_test() : new Test();If `get_test()` has side effects, we may have a problem. We would use the following instead.
Test* test = get_test();
test = test != nullptr ? test : new Test();We could instead use a shortcut without side effect, the null coalescing operator ??.The left hand side expression is evaluated and compared to nullptr. If it is not nullptr, the evaluation is complete. If it is nullptr, the right hand side expression is evaluated.
Test* test = get_test() ?? new Test();It can also be a concise way to throw on a null value.
some_pointer ?? throw std::invalid_argument("some_pointer is null.");
These are the most interesting aspects of the feature. I'd like to gather thoughts and comments. Maybe it has already been mentioned in a paper that didn't go through? I'm simply interested in getting the discussion started before writing a paper. If the general consensus is that this feature is desired, maybe you could point me to a committee member that could mentor me?Thank you,Philippe
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
What about a library function such astemplate <class T, class Func>T* coalesce(T* x, Func f){if (x == nullptr)return f();elsereturn x;}
Il giorno venerdì 12 maggio 2017 13:07:51 UTC+2, dgutson ha scritto:What about a library function such astemplate <class T, class Func>T* coalesce(T* x, Func f){if (x == nullptr)return f();elsereturn x;}
This is exactly the kind of applications of my recent proposal Controlled Argument Evaluation.
With my proposal, you would be able to write a function with the exact same meaning without having to dish out lambdas explicitly.
Alberto
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/c0310bc7-feb0-4c93-8bdb-5b75fe4b6156%40isocpp.org.
--
You received this message because you are subscribed to a topic in the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this topic, visit https://groups.google.com/a/isocpp.org/d/topic/std-proposals/uNYpDyi7VSA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to std-proposal...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/a17ed11b-ff92-43d2-bb1f-7c662a18c9e4%40isocpp.org.
From: Philippe Groarke Sent: Monday, May 22, 2017 8:20 PM Reply To: std-pr...@isocpp.org Subject: Re: [std-proposals] Null coalescing operator |