Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can an array work with the conditional operator?
    primarykey
    data
    text
    <p>This is a retelling of <a href="https://stackoverflow.com/q/16163456/1010226" title="Can you throw within a conditional expression? (was: How can bounds-checking be extended to multiple dimensions?)">my previous post</a>, since I changed the question (so it probably didn't get flagged as a new question and was missed). I'll hopefully trim it down too.</p> <p>I had functions like:</p> <pre><code>#include &lt;cstddef&gt; #include &lt;type_traits&gt; template &lt; typename E, typename T &gt; inline constexpr auto checked_slice( E &amp;&amp;, T &amp;&amp;t ) noexcept -&gt; T &amp;&amp; { return static_cast&lt;T &amp;&amp;&gt;(t); } template &lt; typename E, typename T, std::size_t N, typename U, typename ...V &gt; inline constexpr auto checked_slice( E &amp;&amp;e, T (&amp;t)[N], U &amp;&amp;u, V &amp;&amp;...v ) -&gt; typename remove_some_extents&lt;T, sizeof...(V)&gt;::type &amp; { typedef typename std::remove_reference&lt;U&gt;::type u_type; typedef typename std::common_type&lt;u_type, std::size_t&gt;::type cmp_type; return ( u &lt; u_type{} ) || ( static_cast&lt;cmp_type&gt;(u) &gt;= static_cast&lt;cmp_type&gt;(N) ) ? throw e : checked_slice( static_cast&lt;E &amp;&amp;&gt;(e), t[static_cast&lt;U &amp;&amp;&gt;( u )], static_cast&lt;V &amp;&amp;&gt;(v)... ); } </code></pre> <p>where <code>remove_some_extents</code> is a custom class template that's like calling the <code>std::remove_extent</code> meta-function a given number of times.</p> <p>When I tried running the program, I got a bunch of errors like: "invalid initialization of reference of type <code>Whatever(&amp;)[X][Y]</code> from expression of type <code>Whatever(*)[Y]</code>" (or <code>Whatever(&amp;)[Z]</code> from <code>Whatever*</code>). My workaround was to convert the conditional expression to an <code>if</code>-<code>else</code> pair (and removing the <code>constexpr</code>).</p> <p>I'm trying to figure out what's wrong, so I'm poking around the section about the conditional operator in the C++ (2011) standard. That's section 5.16. When one of the two possible actions is a throw command (or is otherwise a <code>void</code> expression), then the conditional has the type of the other expression, but the standard conversions, including array-to-pointer, is applied to that other expression. (This is in paragraph 2.) I think that's what's messing me up. Is there any way around it? I thought returning an array reference suppresses the a-to-p conversion. Why does it work when made into an <code>if/else</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload