Boost C++ Libraries: Ticket #2428: [random] missing member initialization of generator_iterator ctor https://svn.boost.org/trac10/ticket/2428 <p> I am trying to use boost::generator_iterator with a random number generator as follows: </p> <p> --- #include &lt;boost/generator_iterator.hpp&gt; #include &lt;boost/random/mersenne_twister.hpp&gt; </p> <p> class my_generator {...}; </p> <p> my_generator gen; </p> <p> boost::generator_iterator&lt;my_generator&gt; first(&amp;gen); boost::generator_iterator&lt;my_generator&gt; last; </p> <p> boost::random::mt19937 rng; rng.seed(first, last); </p> <p> --- </p> <p> Sadly the default constructor of generator_iterator leaves m_gen uninitialized and the seed function is trying to compare first with last. </p> <p> This yield undefined behavior. </p> <p> Solution: default ctor member initialization: </p> <p> generator_iterator.hpp : line 38 </p> <p> generator_iterator() : m_g(0), m_value(0) {} </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2428 Trac 1.4.3 Johannes Brunen <JBrunen@…> Wed, 22 Oct 2008 11:19:52 GMT attachment set https://svn.boost.org/trac10/ticket/2428 https://svn.boost.org/trac10/ticket/2428 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">generator_iterator.hpp</span> </li> </ul> Ticket Daryle Walker Mon, 08 Dec 2008 23:44:21 GMT cc changed https://svn.boost.org/trac10/ticket/2428#comment:1 https://svn.boost.org/trac10/ticket/2428#comment:1 <ul> <li><strong>cc</strong> <span class="trac-author">dwalker07@…</span> added </li> </ul> <p> The author of the UUID library recently under review noticed this too; and made a copy of <code>generator_iterator</code> that adds explicit default-initialization for the generator pointer member. I realized that the cached value member also needs default initialization (which that author skipped). You mentioned comparisons with the generator pointer, but both members need it, as your code states, because both members will get compared when checking two end iterators. BTW, do the initializations need to be filled in with "0," or can nothing between the parentheses be used? </p> Ticket anonymous Tue, 09 Dec 2008 09:15:44 GMT <link>https://svn.boost.org/trac10/ticket/2428#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2428#comment:2</guid> <description> <p> In case of the mersenne_twister generator the Generator::result_type is UIntType. Probably more correct would be initialization with Generator::result_type(), i.e. </p> <p> generator_iterator() : m_g(0), m_value(Generator::result_type()) {} </p> <p> Default construction of integral types are guaranteed to be 0. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Wed, 09 Jun 2010 02:18:48 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/2428#comment:3 https://svn.boost.org/trac10/ticket/2428#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">wontfix</span> </li> </ul> <p> Use the new function_input_iterator in Boost.Iterator instead. (See <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2893" title="#2893: Library Submissions: Function Input Iterator (closed: fixed)">#2893</a>). I'm going to deprecate generator_iterator as it's behavior can be somewhat odd. </p> Ticket