C++ Logo

std-proposals

Advanced search

Re: P2192R0 -- Transparent return type

From: Nikolay Mihaylov <nmmm_at_[hidden]>
Date: Tue, 28 Jul 2020 14:04:02 +0300
Is this some kind of pattern?

auto [ value, status ] = valstat_enabled_function () ;
> if ( value && status ) { /* info */ }
> if ( value && ! status ) { /* ok */ }
> if ( ! value && status ) { /* error*/ }
> if ( ! value && ! status ) { /* empty*/ }


or you just check all possibilities?

I believe more often one use it as following:

auto [ fd, status ] = modern_fopen("/tmp/abc");



switch(status){
> case FStatus::NOT_FOUND: return display_error("File not found");
> case FStatus::INCORRECT: return display_error("incorrect");
> case OK: /* ok */
> }



// use file descriptor
> close(fd);


I am unsure why I should use "std" valstat and not make my own with say
std::pair or custom struct? - *show intention? *(this is a very good reason)

about implementation, I would never do status std::optional.
is debatable if value should be std::optional too.

If you see the example with file descriptor - it is *int* - if status is
incorrect, we can put anything there (not just -1) and it will have a small
memory footprint and perform faster than std::optional.
Same with iterators - they are usually pointers or structs with a pointer
or two inside.:

class SkipList::iterator{
> public:
> constexpr iterator(const Node *node) : node_(node){}
> public:
> using difference_type = SkipList::difference_type;
> using value_type = const Pair;
> using pointer = value_type *;
> using reference = value_type &;
> using iterator_category = std::forward_iterator_tag;
> public:
> iterator &operator++();
> reference operator*() const;
> public:
> bool operator==(const iterator &other) const{
> return node_ == other.node_;
> }
> bool operator!=(const iterator &other) const{
> return ! operator==(other);
> }
> pointer operator ->() const{
> return & operator*();
> }
> private:
>
> * const Node *node_;*};


If one needs std::optional, he / she can add it.

So implementation should be more like that:

namespace std {
> template< typename T, typename S>
> struct [[nodiscard]] valstat{
> using value_type = T;
> using status_type = S;
> T value;
> S status;
> };



template< typename T, typename S>
> auto make_valstat(T t, S s){
> // to do - fix forward mess :)
> valstat<T,S>(t, s);
> }
> } // std


I also would make function similar to std::make_pair - even you can use
CTAD, make_pair still show intend;

Regards



On Tue, Jul 28, 2020 at 1:12 PM Dusan Jovanovic via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Dear All,
>
> I might be so bold to request your valuable comments on
>
> P2192R0 -- Transparent return type
>
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2192r0.pdf
>
> Kind regards ...
>
> *Mr Dusan B. J.* | https://dbj.org
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2020-07-28 06:08:02