Opened 14 years ago

Closed 13 years ago

#2887 closed Bugs (fixed)

mersenne_twister generated copy ctor overloaded by a template ctor

Reported by: corlay@… Owned by: No-Maintainer
Milestone: Boost 1.39.0 Component: random
Version: Boost 1.38.0 Severity: Problem
Keywords: Copy ctor Bug Cc:

Description

Compiled-generated copy constructor and assignment operator are fine, but the copy constructor is overloaded by mersenne_twister(Generator & gen) { seed(gen); } even is the Generator type is mersenne_twister;

  template<class Generator>
  explicit mersenne_twister(Generator & gen) { seed(gen); }

  // compiler-generated copy ctor and assignment operator are fine

Hence calling

	boost::mt19937 rng(0);
	boost::mt19937 rngcopy(rng);
        std::cout << (rng==rngcopy) << std::endl;

yields "false".

Maybe a boost::disable_if<boost::is_same<mersenne_twister,Generator> > solves the problem.

See also the workaround made in default constructor of boost::pass_through_engine to avoid matching Generator & constructor

S.

Attachments (1)

patchfile.patch (1.7 KB ) - added by corlay@… 14 years ago.
unified diff for the proposed correction

Download all attachments as: .zip

Change History (4)

comment:1 by corlay@…, 14 years ago

In mersenne_twister.hpp, replacing

template<class Generator>
  explicit mersenne_twister(Generator & gen) { seed(gen); }

by

  template<class Generator>
	explicit mersenne_twister(Generator & gen, typename disable_if< is_same<Generator,mersenne_twister>,int >::type = 0) { seed(gen); }

and add includes

#include <boost/utility/enable_if.hpp>
#include  <boost/type_traits/is_same.hpp>

seems to solve it. No need to handle with the const case.

Then, one has to do it for other generators and, in pass_through_engine.hpp, one can then replace

  explicit pass_through_engine(UniformRandomNumberGenerator rng)
    // make argument an rvalue to avoid matching Generator& constructor
    : _rng(static_cast<typename helper_type::rvalue_type>(rng))
  { }

by

  explicit pass_through_engine(UniformRandomNumberGenerator rng) : _rng(rng)
  { }

.

comment:2 by anonymous, 14 years ago

Keywords: Copy ctor Bug added; boost::random removed
Summary: boost::mersenne_twister copy constructormersenne_twister generated copy ctor overloaded by a template ctor

by corlay@…, 14 years ago

Attachment: patchfile.patch added

unified diff for the proposed correction

comment:3 by Steven Watanabe, 13 years ago

Resolution: fixed
Status: newclosed

(In [53699]) Fix overload resolution for generator constructors/seed. Fixes #351. Fixes #2424. Fixes #2887

Note: See TracTickets for help on using tickets.