C++ Logo


Advanced search

Re: Function proposal: generic value_or

From: Roberto Romani <roberto.romani_at_[hidden]>
Date: Mon, 5 Jul 2021 22:34:08 +0200
If the word “value” in the name of the function may create confusion then the function may be called “coalesce”.


The goal is to have a function that makes cleaner and less error prone things like v = p1 ? *p1 : p2 ? *p2 : defaultValue;




From: Std-Proposals <std-proposals-bounces_at_[hidden]> On Behalf Of ?????? ???????? via Std-Proposals
Sent: Monday 5 July 2021 17:32
To: sotrdg sotrdg via Std-Proposals <std-proposals_at_[hidden]>
Cc: Михаил Найденов <mihailnajdenov_at_[hidden]>
Subject: Re: [std-proposals] Function proposal: generic value_or


Note that optional is a value type and all pointers are reference types.
"The value" of the pointer is the pointer itself, the same way a copy of the pointer is a copy of the pointer itself.
For optional the value is the value stored inside it and copy is copy of the value as well.
Nuances like this might get in the way of standardizing this.


On Sun, Jul 4, 2021 at 10:33 PM Roberto Romani via Std-Proposals <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]> > wrote:

Dear All


I have trivial proposal: a function similar to the std::optional.value_or: it returns the first parameter not null, if it does not find it, then it returns a default value. It is very similar to a coalesce SQL function.

The input parameters can be raw pointers, std::unique_ptr, std::shared_ptr, std::weak_ptr or std::optional.


My idea is something like:



     * Concept that define a pointer of type PointerType that points to

     * ValueType.

     * It covers: raw pointers, std::unique_ptr, std:shared_ptr,

     * std::weak_ptr, std::optional, std::null_ptr, NULL, 0 and any other

     * class that has the operators * and bool.


    template<typename PointerType, typename ValueType>

    concept PointerTo = requires(PointerType pointer, ValueType) // pointer must behave like a raw pointer that points to a ValueType


        {*pointer} -> std::convertible_to<ValueType>;

        {!pointer} noexcept;


    || requires(PointerType pointer, ValueType) // pointer must behave like a weak_ptr that points to a ValueType


        {*pointer.lock()} noexcept -> std::convertible_to<ValueType>;


    || std::same_as<std::nullptr_t, PointerType> // to cover the case of null_ptr

    || std::same_as<int, PointerType>; // for NULL (in VS) and 0




    * It looks for a not null value in to_test_v. If it does not find it,

    * then value_or returns a default value.


    * \param default_value Value to return if all the values of to_test_v are null

    * \param ...to_test_v The values to test for not null

    * \return The first value not null of to_test_v, if no value of to_test_v is not null then default_value


    template<typename T, PointerTo<T>... Args>

    constexpr decltype(auto) value_or(T&& default_value, Args&&... to_test_v) noexcept;



There is the concept PointerTo that defines a class that behave like a raw pointer, and then there is the function value_or that tales a default value and then a list of “pointers” to check.


In PointerType I have included also the possibility the pointer is an int in order to support the values 0 and NULL, I am not sure that it is needed.


I have declared value_or noexcept, I think it makes sense but I do not think it is possible to guarantee it in a formal way.


I think a function like this may help to manage object that can be null in structured form.


Here you can find a possible implementation: https://github.com/roroberto/cpp_small_simple_stupid_stuff/blob/main/value_or/value_or.h



Best Regards



Std-Proposals mailing list
Std-Proposals_at_[hidden] <mailto:Std-Proposals_at_[hidden]> 

Received on 2021-07-05 15:34:12