Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't perform a range based loop directly over a dynamically allocated array because all you have is a pointer to the first element. There is no information concerning its size that the compiler can use to perform the loop. The idiomatic C++ solution would be to replace the dynamically allocated array by an <code>std::vector</code>:</p> <pre><code>std::vector&lt;int&gt; arr(size); for(const auto&amp; i : arr) std::cout&lt;&lt; i &lt;&lt; std::endl; </code></pre> <p>Alternatively, you could use a range type that provides a begin and end iterator based on a pointer and an offset. Have a look at some of the types in the <a href="https://www.boost.org/doc/libs/1_67_0/libs/range/doc/html/index.html" rel="nofollow noreferrer">boost.range</a> library, or at the <a href="https://github.com/isocpp/CppCoreGuidelines/blob/master/docs/gsl-intro.md" rel="nofollow noreferrer">GSL</a> span proposal (example implementation <a href="https://github.com/Microsoft/GSL" rel="nofollow noreferrer">here</a>, reference for C++20 proposed type <a href="https://en.cppreference.com/w/cpp/container/span" rel="nofollow noreferrer">here</a>).</p> <hr> <p>Note that a range based for loop does work for <code>std::array</code> objects of fixes size plain arrays:</p> <pre><code>std::array&lt;int,10&gt; arr; for(const auto&amp; i : arr) std::cout&lt;&lt; i &lt;&lt; std::endl; int arr[10] = .... ; for(const auto&amp; i : arr) std::cout&lt;&lt; i &lt;&lt; std::endl; </code></pre> <p>but in both cases the size needs to be a compile-time constant.</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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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