Topic: Automatic type deduction, the conditional


Author: Chris Jefferson <chris@bubblescope.net>
Date: Sun, 30 Dec 2012 11:32:36 +0000
Raw View
This is a multi-part message in MIME format.
--------------020806060802090903050104
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

On 29/12/12 18:04, Scott Prager wrote:
> This concerns Jason Merrill's proposal for "return type deduction for
> normal functions
> <http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2012/n3386.html>".
>
> |
> autofact(intx ){returnnotx ?1:x*fact(x-1);}
> |
>
> This code is rejected because it attempts to use /fact/ before its
> type is known. I think it makes sense to disallow recursion in
> general, but I see this as a special case because the return type is
> knowable. The base case is of type /decltype(1)/ and the general case
> might have the type /decltype(x*1)/.

This seems a bit clever. Why is the 'base case', decltype(1)? There are
fairly complicated rules to do with how the return type of the ?:
operator are decided, based on the operands. For example, it would be
valid for the return type of fact to be anything an int can be converted
to. This is even more complex, because who knows that type 'x*fact(x-1)'
is going to be?

We could obviously introduce a special rule that says that ?: returns
the known type, if one side is unknown, but that seems subtle.

Chris

--




--------------020806060802090903050104
Content-Type: text/html; charset=ISO-8859-1

<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">On 29/12/12 18:04, Scott Prager wrote:<br>
    </div>
    <blockquote
      cite="mid:2f463bed-f075-4e14-9409-cf96c8f6097c@isocpp.org"
      type="cite">This concerns Jason Merrill's proposal for "<a
        moz-do-not-send="true"
href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2012/n3386.html">return
        type deduction for normal functions</a>".<br>
      <br>
      <div class="prettyprint" style="background-color: rgb(250, 250,
        250); border-color: rgb(187, 187, 187); border-style: solid;
        border-width: 1px; word-wrap: break-word;"><code
          class="prettyprint">
          <div class="subprettyprint"><span style="color: #008;"
              class="styled-by-prettify">auto</span><span style="color:
              #000;" class="styled-by-prettify"> fact</span><span
              style="color: #660;" class="styled-by-prettify">(</span><span
              style="color: #000;" class="styled-by-prettify"> </span><span
              style="color: #008;" class="styled-by-prettify">int</span><span
              style="color: #000;" class="styled-by-prettify"> x </span><span
              style="color: #660;" class="styled-by-prettify">)</span><span
              style="color: #000;" class="styled-by-prettify"> </span><span
              style="color: #660;" class="styled-by-prettify">{</span><span
              style="color: #000;" class="styled-by-prettify"> </span><span
              style="color: #008;" class="styled-by-prettify">return</span><span
              style="color: #000;" class="styled-by-prettify"> </span><span
              style="color: #008;" class="styled-by-prettify">not</span><span
              style="color: #000;" class="styled-by-prettify"> x </span><span
              style="color: #660;" class="styled-by-prettify">?</span><span
              style="color: #000;" class="styled-by-prettify"> </span><span
              style="color: #066;" class="styled-by-prettify">1</span><span
              style="color: #000;" class="styled-by-prettify"> </span><span
              style="color: #660;" class="styled-by-prettify">:</span><span
              style="color: #000;" class="styled-by-prettify"> x</span><span
              style="color: #660;" class="styled-by-prettify">*</span><span
              style="color: #000;" class="styled-by-prettify">fact</span><span
              style="color: #660;" class="styled-by-prettify">(</span><span
              style="color: #000;" class="styled-by-prettify">x</span><span
              style="color: #660;" class="styled-by-prettify">-</span><span
              style="color: #066;" class="styled-by-prettify">1</span><span
              style="color: #660;" class="styled-by-prettify">);</span><span
              style="color: #000;" class="styled-by-prettify"> </span><span
              style="color: #660;" class="styled-by-prettify">}</span><span
              style="color: #000;" class="styled-by-prettify"><br>
            </span></div>
        </code></div>
      <br>
      This code is rejected because it attempts to use <i>fact</i>
      before its type is known. I think it makes sense to disallow
      recursion in general, but I see this as a special case because the
      return type is knowable. The base case is of type <i>decltype(1)</i>
      and the general case might have the type <i>decltype(x*1)</i>.<br>
    </blockquote>
    <br>
    This seems a bit clever. Why is the 'base case', decltype(1)? There
    are fairly complicated rules to do with how the return type of the
    ?: operator are decided, based on the operands. For example, it
    would be valid for the return type of fact to be anything an int can
    be converted to. This is even more complex, because who knows that
    type 'x*fact(x-1)' is going to be?<br>
    <br>
    We could obviously introduce a special rule that says that ?:
    returns the known type, if one side is unknown, but that seems
    subtle.<br>
    <br>
    Chris<br>
  </body>
</html>

<p></p>

-- <br />
&nbsp;<br />
&nbsp;<br />
&nbsp;<br />

--------------020806060802090903050104--

.