<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- saved from url=(0047)file:///home/sdowney/src/papers/view-maybe.html -->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- 2018-10-08 Mon 09:43 -->

<meta name="viewport" content="width=device-width, initial-scale=1">
<title>A view of 0 or 1 elements: &lt;code&gt;view::maybe&lt;/code&gt;</title>
<meta name="generator" content="Org mode">
<style type="text/css">
 <!--/*--><![CDATA[/*><!--*/
  .title  { text-align: center;
             margin-bottom: .2em; }
  .subtitle { text-align: center;
              font-size: medium;
              font-weight: bold;
              margin-top:0; }
  .todo   { font-family: monospace; color: red; }
  .done   { font-family: monospace; color: green; }
  .priority { font-family: monospace; color: orange; }
  .tag    { background-color: #eee; font-family: monospace;
            padding: 2px; font-size: 80%; font-weight: normal; }
  .timestamp { color: #bebebe; }
  .timestamp-kwd { color: #5f9ea0; }
  .org-right  { margin-left: auto; margin-right: 0px;  text-align: right; }
  .org-left   { margin-left: 0px;  margin-right: auto; text-align: left; }
  .org-center { margin-left: auto; margin-right: auto; text-align: center; }
  .underline { text-decoration: underline; }
  #postamble p, #preamble p { font-size: 90%; margin: .2em; }
  p.verse { margin-left: 3%; }
  pre {
    border: 1px solid #ccc;
    box-shadow: 3px 3px 3px #eee;
    padding: 8pt;
    font-family: monospace;
    overflow: auto;
    margin: 1.2em;
  }
  pre.src {
    position: relative;
    overflow: visible;
    padding-top: 1.2em;
  }
  pre.src:before {
    display: none;
    position: absolute;
    background-color: white;
    top: -10px;
    right: 10px;
    padding: 3px;
    border: 1px solid black;
  }
  pre.src:hover:before { display: inline;}
  /* Languages per Org manual */
  pre.src-asymptote:before { content: 'Asymptote'; }
  pre.src-awk:before { content: 'Awk'; }
  pre.src-C:before { content: 'C'; }
  /* pre.src-C++ doesn't work in CSS */
  pre.src-clojure:before { content: 'Clojure'; }
  pre.src-css:before { content: 'CSS'; }
  pre.src-D:before { content: 'D'; }
  pre.src-ditaa:before { content: 'ditaa'; }
  pre.src-dot:before { content: 'Graphviz'; }
  pre.src-calc:before { content: 'Emacs Calc'; }
  pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
  pre.src-fortran:before { content: 'Fortran'; }
  pre.src-gnuplot:before { content: 'gnuplot'; }
  pre.src-haskell:before { content: 'Haskell'; }
  pre.src-hledger:before { content: 'hledger'; }
  pre.src-java:before { content: 'Java'; }
  pre.src-js:before { content: 'Javascript'; }
  pre.src-latex:before { content: 'LaTeX'; }
  pre.src-ledger:before { content: 'Ledger'; }
  pre.src-lisp:before { content: 'Lisp'; }
  pre.src-lilypond:before { content: 'Lilypond'; }
  pre.src-lua:before { content: 'Lua'; }
  pre.src-matlab:before { content: 'MATLAB'; }
  pre.src-mscgen:before { content: 'Mscgen'; }
  pre.src-ocaml:before { content: 'Objective Caml'; }
  pre.src-octave:before { content: 'Octave'; }
  pre.src-org:before { content: 'Org mode'; }
  pre.src-oz:before { content: 'OZ'; }
  pre.src-plantuml:before { content: 'Plantuml'; }
  pre.src-processing:before { content: 'Processing.js'; }
  pre.src-python:before { content: 'Python'; }
  pre.src-R:before { content: 'R'; }
  pre.src-ruby:before { content: 'Ruby'; }
  pre.src-sass:before { content: 'Sass'; }
  pre.src-scheme:before { content: 'Scheme'; }
  pre.src-screen:before { content: 'Gnu Screen'; }
  pre.src-sed:before { content: 'Sed'; }
  pre.src-sh:before { content: 'shell'; }
  pre.src-sql:before { content: 'SQL'; }
  pre.src-sqlite:before { content: 'SQLite'; }
  /* additional languages in org.el's org-babel-load-languages alist */
  pre.src-forth:before { content: 'Forth'; }
  pre.src-io:before { content: 'IO'; }
  pre.src-J:before { content: 'J'; }
  pre.src-makefile:before { content: 'Makefile'; }
  pre.src-maxima:before { content: 'Maxima'; }
  pre.src-perl:before { content: 'Perl'; }
  pre.src-picolisp:before { content: 'Pico Lisp'; }
  pre.src-scala:before { content: 'Scala'; }
  pre.src-shell:before { content: 'Shell Script'; }
  pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
  /* additional language identifiers per "defun org-babel-execute"
       in ob-*.el */
  pre.src-cpp:before  { content: 'C++'; }
  pre.src-abc:before  { content: 'ABC'; }
  pre.src-coq:before  { content: 'Coq'; }
  pre.src-groovy:before  { content: 'Groovy'; }
  /* additional language identifiers from org-babel-shell-names in
     ob-shell.el: ob-shell is the only babel language using a lambda to put
     the execution function name together. */
  pre.src-bash:before  { content: 'bash'; }
  pre.src-csh:before  { content: 'csh'; }
  pre.src-ash:before  { content: 'ash'; }
  pre.src-dash:before  { content: 'dash'; }
  pre.src-ksh:before  { content: 'ksh'; }
  pre.src-mksh:before  { content: 'mksh'; }
  pre.src-posh:before  { content: 'posh'; }
  /* Additional Emacs modes also supported by the LaTeX listings package */
  pre.src-ada:before { content: 'Ada'; }
  pre.src-asm:before { content: 'Assembler'; }
  pre.src-caml:before { content: 'Caml'; }
  pre.src-delphi:before { content: 'Delphi'; }
  pre.src-html:before { content: 'HTML'; }
  pre.src-idl:before { content: 'IDL'; }
  pre.src-mercury:before { content: 'Mercury'; }
  pre.src-metapost:before { content: 'MetaPost'; }
  pre.src-modula-2:before { content: 'Modula-2'; }
  pre.src-pascal:before { content: 'Pascal'; }
  pre.src-ps:before { content: 'PostScript'; }
  pre.src-prolog:before { content: 'Prolog'; }
  pre.src-simula:before { content: 'Simula'; }
  pre.src-tcl:before { content: 'tcl'; }
  pre.src-tex:before { content: 'TeX'; }
  pre.src-plain-tex:before { content: 'Plain TeX'; }
  pre.src-verilog:before { content: 'Verilog'; }
  pre.src-vhdl:before { content: 'VHDL'; }
  pre.src-xml:before { content: 'XML'; }
  pre.src-nxml:before { content: 'XML'; }
  /* add a generic configuration mode; LaTeX export needs an additional
     (add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
  pre.src-conf:before { content: 'Configuration File'; }

  table { border-collapse:collapse; }
  caption.t-above { caption-side: top; }
  caption.t-bottom { caption-side: bottom; }
  td, th { vertical-align:top;  }
  th.org-right  { text-align: center;  }
  th.org-left   { text-align: center;   }
  th.org-center { text-align: center; }
  td.org-right  { text-align: right;  }
  td.org-left   { text-align: left;   }
  td.org-center { text-align: center; }
  dt { font-weight: bold; }
  .footpara { display: inline; }
  .footdef  { margin-bottom: 1em; }
  .figure { padding: 1em; }
  .figure p { text-align: center; }
  .inlinetask {
    padding: 10px;
    border: 2px solid gray;
    margin: 10px;
    background: #ffffcc;
  }
  #org-div-home-and-up
   { text-align: right; font-size: 70%; white-space: nowrap; }
  textarea { overflow-x: auto; }
  .linenr { font-size: smaller }
  .code-highlighted { background-color: #ffff00; }
  .org-info-js_info-navigation { border-style: none; }
  #org-info-js_console-label
    { font-size: 10px; font-weight: bold; white-space: nowrap; }
  .org-info-js_search-highlight
    { background-color: #ffff00; color: #000000; font-weight: bold; }
  .org-svg { width: 90%; }
  /*]]>*/-->
</style>
<link rel="stylesheet" type="text/css" href="./P1255R0_files/smd-zenburn.css">
<script type="text/javascript">
/*
@licstart  The following is the entire license notice for the
JavaScript code in this tag.

Copyright (C) 2012-2018 Free Software Foundation, Inc.

The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version.  The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU GPL for more details.

As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.


@licend  The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
 function CodeHighlightOn(elem, id)
 {
   var target = document.getElementById(id);
   if(null != target) {
     elem.cacheClassElem = elem.className;
     elem.cacheClassTarget = target.className;
     target.className = "code-highlighted";
     elem.className   = "code-highlighted";
   }
 }
 function CodeHighlightOff(elem, id)
 {
   var target = document.getElementById(id);
   if(elem.cacheClassElem)
     elem.className = elem.cacheClassElem;
   if(elem.cacheClassTarget)
     target.className = elem.cacheClassTarget;
 }
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<h1 class="title">A view of 0 or 1 elements: <code>view::maybe</code></h1>
<ul class="org-ul">
<li>Document number: P1255R0</li>
<li>Date:  2018-10-07</li>
<li>Author: Steve Downey &lt;sdowney2@bloomberg.net&gt;</li>
<li>Audience: LEWG</li>
</ul>

<div class="ABSTRACT">
<p>
Abstract: This paper proposes <code>view::maybe</code> a range adaptor that produces a view with cardinality 0 or 1 which acts as an adaptor for nullable types such as <code>std::optional</code> and pointer types.
</p>

</div>

<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="file:///home/sdowney/src/papers/view-maybe.html#org9ce06bd">1. Motivation</a></li>
<li><a href="file:///home/sdowney/src/papers/view-maybe.html#org7c14da0">2. Proposal</a></li>
<li><a href="file:///home/sdowney/src/papers/view-maybe.html#orgd75e4e5">3. Design</a></li>
<li><a href="file:///home/sdowney/src/papers/view-maybe.html#orge6e684c">4. Very very preliminary wording</a></li>
<li><a href="file:///home/sdowney/src/papers/view-maybe.html#orgcdbcbaa">5. Impact on the standard</a></li>
<li><a href="file:///home/sdowney/src/papers/view-maybe.html#org276417d">6. Bikeshed</a></li>
<li><a href="file:///home/sdowney/src/papers/view-maybe.html#org014c630">7. References</a></li>
</ul>
</div>
</div>

<div id="outline-container-org9ce06bd" class="outline-2">
<h2 id="org9ce06bd"><span class="section-number-2">1</span> Motivation</h2>
<div class="outline-text-2" id="text-1">
<p>
In writing range transformation pipelines it is useful to be able to lift a nullable value into a view that is either empty or contains the value held by the nullable. The adapter <code>view::single</code> fills a similar purpose for non-nullable values, lifting a single value into a view, and <code>view::empty</code> provides a range of no values of a given type. A <code>view::maybe</code> adaptor also allows nullable values to be treated as ranges when it is otherwise undesirable to make them containers, for example <code>std::optional</code>.
</p>

<div class="org-src-container">
<pre class="src src-C++"><span style="color: #2aa198;">std</span>::<span style="color: #b58900;">vector</span>&lt;<span style="color: #2aa198;">std</span>::<span style="color: #b58900;">optional</span>&lt;<span style="color: #b58900;">int</span>&gt;&gt; <span style="color: #268bd2;">v</span>{
  <span style="color: #2aa198;">std</span>::<span style="color: #b58900;">optional</span>&lt;<span style="color: #b58900;">int</span>&gt;{42},
  <span style="color: #2aa198;">std</span>::<span style="color: #b58900;">optional</span>&lt;<span style="color: #b58900;">int</span>&gt;{},
  <span style="color: #2aa198;">std</span>::<span style="color: #b58900;">optional</span>&lt;<span style="color: #b58900;">int</span>&gt;{6 * 9}};

<span style="color: #859900;">auto</span> <span style="color: #268bd2;">r</span> = <span style="color: #2aa198;">view</span>::join(<span style="color: #2aa198;">view</span>::transform(v, <span style="color: #2aa198;">view</span>::maybe));

<span style="color: #859900;">for</span> (<span style="color: #859900;">auto</span> <span style="color: #268bd2;">i</span> : r) {
    <span style="color: #2aa198;">std</span>::cout &lt;&lt; i; <span style="color: #586e75; font-style: italic;">// </span><span style="color: #586e75; font-style: italic;">prints 42 and 42</span>
}
</pre>
</div>

<p>
In addition to range transformation pipelines, <code>view::maybe</code> can be used in range based for loops, allowing the nullable value to not be dereferenced within the body. This is of small value in small examples in contrast to testing the nullable in an if statement, but with longer bodies the dereference is often far away from the test. Often the first line in the body of the <code>if</code> is naming the dereferenced nullable, and lifting the dereference into the for loop eliminates some boilerplate code, the same way that range based for loops do.
</p>

<div class="org-src-container">
<pre class="src src-C++">{
<span style="color: #859900;">auto</span>&amp;&amp; <span style="color: #268bd2;">opt</span> = possible_value();
<span style="color: #859900;">if</span> (*opt) {
<span style="color: #586e75; font-style: italic;">// </span><span style="color: #586e75; font-style: italic;">a few dozen lines ...</span>
use(*opt); <span style="color: #586e75; font-style: italic;">// </span><span style="color: #586e75; font-style: italic;">is *opt OK</span>
}
}

<span style="color: #586e75; font-style: italic;">// </span><span style="color: #586e75; font-style: italic;">vs ...</span>

<span style="color: #859900;">for</span> (<span style="color: #859900;">auto</span>&amp;&amp; <span style="color: #268bd2;">v</span> : <span style="color: #2aa198;">view</span>::maybe(possible_value())) {
<span style="color: #586e75; font-style: italic;">// </span><span style="color: #586e75; font-style: italic;">a few dozen lines ...</span>
use(v);
}

</pre>
</div>
</div>
</div>


<div id="outline-container-org7c14da0" class="outline-2">
<h2 id="org7c14da0"><span class="section-number-2">2</span> Proposal</h2>
<div class="outline-text-2" id="text-2">
<p>
Add class template <code>maybe_view</code>, a closure object <code>view::maybe</code>, and customization point objects <code>view::maybe_has_value</code> and <code>view::maybe_value</code>, used to determine if there is a value in the underlying object and to produce a View that contains that value.
</p>
</div>
</div>

<div id="outline-container-orgd75e4e5" class="outline-2">
<h2 id="orgd75e4e5"><span class="section-number-2">3</span> Design</h2>
<div class="outline-text-2" id="text-3">
<p>
The basis of the design is to hybridize <code>view::single</code> and <code>view::empty</code>. If the underlying object holds a value, as determined by <code>maybe_has_value</code> on the object, <code>begin</code> and <code>end</code> of the view are equivalent to <code>return maybe_value(value_).operator-&gt;();</code>, and <code>return begin()+1</code>.  Otherwise, if the underlying object does not have a value, <code>begin</code> and <code>end</code> return <code>nullptr</code>.
</p>

<p>
The names <code>has_value</code> and <code>value</code> are chosen to mirror the member functions of <code>std::optional</code>, and can pass through to those. The proposed <code>std::expected</code> uses the same names, establishing a pattern for nullable types. If the type being ranged over is pointer-like then <code>maybe_has_value</code> is equivalent to <code>!= nullptr</code>, and <code>maybe_value</code> dereferences the underlying object.
</p>
</div>
</div>

<div id="outline-container-orge6e684c" class="outline-2">
<h2 id="orge6e684c"><span class="section-number-2">4</span> Very very preliminary wording</h2>
<div class="outline-text-2" id="text-4">
</div>
<div id="outline-container-org7825d41" class="outline-3">
<h3 id="org7825d41"><span class="section-number-3">4.1</span> Class template <code>maybe_view</code></h3>
<div class="outline-text-3" id="text-4-1">
<p>
<code>maybe_view</code> produces a View that contains exactly zero or one element contained within a specified nullable value.
</p>

<p>
[Example:
</p>
<div class="org-src-container">
<pre class="src src-C++">   <span style="color: #b58900;">maybe_view</span> <span style="color: #268bd2;">s</span>{<span style="color: #2aa198;">std</span>::optional{4}};
   <span style="color: #859900;">for</span> (<span style="color: #b58900;">int</span> <span style="color: #268bd2;">i</span> : s)
     cout &lt;&lt; i; <span style="color: #586e75; font-style: italic;">// </span><span style="color: #586e75; font-style: italic;">prints 4</span>
   <span style="color: #b58900;">maybe_view</span> <span style="color: #268bd2;">e</span>{<span style="color: #2aa198;">std</span>::optional{}};
   <span style="color: #859900;">for</span> (<span style="color: #b58900;">int</span> <span style="color: #268bd2;">i</span> : e)
     cout &lt;&lt; i; <span style="color: #586e75; font-style: italic;">// </span><span style="color: #586e75; font-style: italic;">does not print</span>
</pre>
</div>

<p>
— end example ]
</p>

<div class="org-src-container">
<pre class="src src-C++">   <span style="color: #859900;">namespace</span> <span style="color: #2aa198;">std</span>::<span style="color: #2aa198;">ranges</span> {
   <span style="color: #859900;">template</span>&lt;<span style="color: #b58900;">CopyConstructible</span> <span style="color: #268bd2;">T</span>&gt;
   <span style="color: #b58900;">requires</span> <span style="color: #b58900;">is_object_v</span>&lt;T&gt;
   <span style="color: #859900;">class</span> maybe_view : <span style="color: #859900;">public</span> <span style="color: #b58900;">view_interface</span>&lt;<span style="color: #b58900;">maybe_view</span>&lt;T&gt;&gt; {
     <span style="color: #859900;">private</span>:
       <span style="color: #b58900;">T</span>* <span style="color: #268bd2;">value_</span>; <span style="color: #586e75; font-style: italic;">// </span><span style="color: #586e75; font-style: italic;">exposition only</span>
     <span style="color: #859900;">public</span>:
       maybe_view() = <span style="color: #859900;">default</span>;

       <span style="color: #859900;">template</span>&lt;<span style="color: #859900;">typename</span> <span style="color: #b58900;">U</span>&gt;
       <span style="color: #b58900;">requires</span> <span style="color: #b58900;">Constructible</span>&lt;<span style="color: #b58900;">T</span>, <span style="color: #b58900;">remove_pointer</span>&lt;<span style="color: #b58900;">U</span>&gt;&gt;
       <span style="color: #859900;">constexpr</span> <span style="color: #859900;">explicit</span> maybe_view(<span style="color: #859900;">const</span> <span style="color: #b58900;">U</span>&amp; <span style="color: #268bd2;">u</span>);

       <span style="color: #859900;">constexpr</span> <span style="color: #b58900;">T</span>* <span style="color: #268bd2; font-weight: bold;">begin</span>() <span style="color: #859900;">const</span> <span style="color: #859900;">noexcept</span>;

       <span style="color: #859900;">constexpr</span> <span style="color: #b58900;">T</span>* <span style="color: #268bd2; font-weight: bold;">end</span>() <span style="color: #859900;">const</span> <span style="color: #859900;">noexcept</span>;

       <span style="color: #859900;">constexpr</span> <span style="color: #859900;">static</span> <span style="color: #b58900;">ptrdiff_t</span> <span style="color: #268bd2; font-weight: bold;">size</span>() <span style="color: #859900;">noexcept</span>;

       <span style="color: #859900;">constexpr</span> <span style="color: #859900;">const</span> <span style="color: #b58900;">T</span>* <span style="color: #268bd2; font-weight: bold;">data</span>() <span style="color: #859900;">const</span> <span style="color: #859900;">noexcept</span>;
   };
   <span style="color: #859900;">template</span>&lt;<span style="color: #859900;">class</span> <span style="color: #b58900;">U</span>&gt;
   <span style="color: #859900;">explicit</span> <span style="color: #268bd2; font-weight: bold;">maybe_view</span>(<span style="color: #859900;">const</span> <span style="color: #b58900;">U</span>&amp;) -&gt; <span style="color: #b58900;">maybe_view</span>&lt;<span style="color: #b58900;">invoke_result</span>&lt;<span style="color: #859900;">decltype</span>(maybe_value), <span style="color: #b58900;">U</span>&gt;&gt;;
   }
</pre>
</div>
</div>
</div>

<div id="outline-container-org0c9cfec" class="outline-3">
<h3 id="org0c9cfec"><span class="section-number-3">4.2</span> <code>maybe_view</code> operations</h3>
<div class="outline-text-3" id="text-4-2">
<div class="org-src-container">
<pre class="src src-C++">   <span style="color: #859900;">template</span>&lt;<span style="color: #859900;">typename</span> <span style="color: #b58900;">U</span>&gt;
   <span style="color: #859900;">constexpr</span> <span style="color: #859900;">explicit</span> <span style="color: #268bd2; font-weight: bold;">maybe_view</span>(<span style="color: #859900;">const</span> <span style="color: #b58900;">U</span>&amp; <span style="color: #268bd2;">u</span>);
</pre>
</div>
<p>
Effects: Initializes value_ with addressof(maybe_value(u)).
</p>

<div class="org-src-container">
<pre class="src src-C++">   <span style="color: #859900;">constexpr</span> <span style="color: #b58900;">T</span>* <span style="color: #268bd2; font-weight: bold;">begin</span>() <span style="color: #859900;">const</span> <span style="color: #859900;">noexcept</span>;
</pre>
</div>
<p>
Effects: Equivalent to: return value_;.
</p>

<div class="org-src-container">
<pre class="src src-C++">   <span style="color: #859900;">constexpr</span>  <span style="color: #b58900;">T</span>* <span style="color: #268bd2; font-weight: bold;">end</span>() <span style="color: #859900;">const</span> <span style="color: #859900;">noexcept</span>;
</pre>
</div>
<p>
Effects: Equivalent to: if (value_ != nullptr) return value_.operator-&gt;() + 1; else return nullptr;
</p>

<div class="org-src-container">
<pre class="src src-C++">   <span style="color: #859900;">constexpr</span> <span style="color: #859900;">static</span> <span style="color: #b58900;">ptrdiff_t</span> <span style="color: #268bd2; font-weight: bold;">size</span>() <span style="color: #859900;">noexcept</span>;
</pre>
</div>
<p>
Effects: Equivalent to return !value_;
</p>

<div class="org-src-container">
<pre class="src src-C++">   <span style="color: #859900;">constexpr</span> <span style="color: #859900;">const</span> <span style="color: #b58900;">T</span>* <span style="color: #268bd2; font-weight: bold;">data</span>() <span style="color: #859900;">const</span> <span style="color: #859900;">noexcept</span>;
</pre>
</div>
<p>
Effects: Equivalent to: return begin();.
</p>
</div>
</div>

<div id="outline-container-orge1bb281" class="outline-3">
<h3 id="orge1bb281"><span class="section-number-3">4.3</span> <code>view::maybe</code></h3>
<div class="outline-text-3" id="text-4-3">
<p>
The name view::maybe denotes a customization point object ([customization.point.object]). The expression view::maybe(E) for some subexpression E is expression-equivalent to maybe_view{E}.
</p>
</div>
</div>

<div id="outline-container-org289e52c" class="outline-3">
<h3 id="org289e52c"><span class="section-number-3">4.4</span> <code>view::maybe_has_value</code></h3>
<div class="outline-text-3" id="text-4-4">
<p>
The name view::maybe_has_value denotes a customization point object ([customization.point.object]). The expression view::maybe_has_value(E) for some subexpression E is expression-equivalent to E.has_value() or (E != nullptr) if E.has_value() is not well formed.
</p>

<p>
[Example:
</p>
<div class="org-src-container">
<pre class="src src-C++">    <span style="color: #859900;">constexpr</span> <span style="color: #2aa198;">std</span>::<span style="color: #b58900;">optional</span> <span style="color: #268bd2;">s</span>{7};
    <span style="color: #859900;">constexpr</span> <span style="color: #2aa198;">std</span>::<span style="color: #b58900;">optional</span>&lt;<span style="color: #b58900;">int</span>&gt; <span style="color: #268bd2;">e</span>{};
    <span style="color: #859900;">static_assert</span>(<span style="color: #2aa198;">view</span>::maybe_has_value(s));
    <span style="color: #859900;">static_assert</span>(<span style="color: #dc322f; font-weight: bold;">!</span><span style="color: #2aa198;">view</span>::maybe_has_value(e));
</pre>
</div>
<p>
— end example ]
</p>
</div>
</div>


<div id="outline-container-orge7113ab" class="outline-3">
<h3 id="orge7113ab"><span class="section-number-3">4.5</span> <code>view::maybe_value</code></h3>
<div class="outline-text-3" id="text-4-5">
<p>
The name view::maybe_value denotes a customization point object ([customization.point.object]). The expression view::maybe_value(E) for some subexpression E is expression-equivalent to *(E.operator-&gt;()).
</p>

<p>
[Example:
</p>
<div class="org-src-container">
<pre class="src src-C++">    <span style="color: #2aa198;">std</span>::<span style="color: #b58900;">optional</span> <span style="color: #268bd2;">s</span>{42};
    assert(<span style="color: #2aa198;">view</span>::maybe_value(s) == 42);

    <span style="color: #b58900;">int</span> <span style="color: #268bd2;">k</span> = 42;
    <span style="color: #b58900;">int</span> *<span style="color: #268bd2;">p</span> = &amp;k;
    assert(<span style="color: #2aa198;">view</span>::maybe_value(p) == 42);

</pre>
</div>
<p>
— end example ]
</p>
</div>
</div>
</div>

<div id="outline-container-orgcdbcbaa" class="outline-2">
<h2 id="orgcdbcbaa"><span class="section-number-2">5</span> Impact on the standard</h2>
<div class="outline-text-2" id="text-5">
<p>
Dependent on The One Ranges Proposal, P0896, but otherwise a pure library extension.
</p>
</div>
</div>

<div id="outline-container-org276417d" class="outline-2">
<h2 id="org276417d"><span class="section-number-2">6</span> Bikeshed</h2>
<div class="outline-text-2" id="text-6">
<p>
The name <code>maybe</code>.
</p>

<p>
That <code>maybe_view</code> is templated only on the held type of the nullable and not the nullable type. This allows mixing of nullable types in the conversion to view, at the cost of not being able to recover the base.
</p>
</div>
</div>

<div id="outline-container-org014c630" class="outline-2">
<h2 id="org014c630"><span class="section-number-2">7</span> References</h2>
<div class="outline-text-2" id="text-7">
<p>
[P0896R2] Eric Niebler, Casey Carter, Christopher Di Bella. The One Ranges Proposal URL: <a href="https://wg21.link/p0896r2">https://wg21.link/p0896r2</a>
</p>

<p>
[P0323R7] Vicente Botet, JF Bastien. std::expected URL: <a href="https://wg21.link/p0323r7">https://wg21.link/p0323r7</a>
</p>
</div>
</div>
</div>


</body></html>