public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/59603] std::random_shuffle tries to swap element with itself
Date: Sat, 08 Feb 2014 20:19:00 -0000	[thread overview]
Message-ID: <bug-59603-4-ft6Rt9xoof@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-59603-4@http.gcc.gnu.org/bugzilla/>

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59603

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This patch avoids the self-move:

index 4c6ca8a..6ce2d21 100644
--- a/libstdc++-v3/include/bits/stl_algo.h
+++ b/libstdc++-v3/include/bits/stl_algo.h
@@ -4430,7 +4430,12 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO

       if (__first != __last)
        for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
-         std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));
+         {
+           int __offset = std::rand() % ((__i - __first) + 1);
+           _RandomAccessIterator __j = __first + __offset;
+           if (__i != __j)
+             std::iter_swap(__i, __j);
+         }
     }

   /**

But I'm not sure whether this is a real bug, or types used with random_shuffle
should be safe against self-move (which means they can't use an implicit move
ctor if they have types that are not safe against self-move, which includes
most std::lib types).


  parent reply	other threads:[~2014-02-08 20:19 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-26 16:36 [Bug libstdc++/59603] New: " fab at orlen dot de
2013-12-26 16:37 ` [Bug libstdc++/59603] " fab at orlen dot de
2014-02-04  8:38 ` joerg.richter@pdv-fs.de
2014-02-04 13:10 ` redi at gcc dot gnu.org
2014-02-08 20:19 ` redi at gcc dot gnu.org [this message]
2014-02-09 17:31 ` joerg.richter@pdv-fs.de
2014-02-10 19:00 ` redi at gcc dot gnu.org
2014-09-12 11:45 ` redi at gcc dot gnu.org
2014-09-12 13:31 ` redi at gcc dot gnu.org
2014-09-12 13:35 ` redi at gcc dot gnu.org
2014-12-06 20:38 ` redi at gcc dot gnu.org
2014-12-06 22:04 ` redi at gcc dot gnu.org
2015-09-16 11:37 ` TonyELewis at hotmail dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-59603-4-ft6Rt9xoof@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).