public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "roman at binarylife dot net" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/51866] New: [c++0x][4.7 Regression] unordered_set compares moved-out values
Date: Mon, 16 Jan 2012 01:44:00 -0000	[thread overview]
Message-ID: <bug-51866-4@http.gcc.gnu.org/bugzilla/> (raw)

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

             Bug #: 51866
           Summary: [c++0x][4.7 Regression] unordered_set compares
                    moved-out values
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: roman@binarylife.net


$ cat test.cpp 
#include <unordered_set>
#include <iostream>
#include <cstdlib>

struct num {
  int value;
  num(int n)                 : value(n) {}
  num(num const&)            = default;
  num& operator=(num const&) = default;
  num(num&& o)               : value(o.value) { o.value=-1; }
  num& operator=(num&& o)    { if (this!=&o) { value=o.value; o.value=-1; }
return *this; }
};

struct num_hash {
  size_t operator()(num const& a)const {
    if (a.value<0) std::cout<<"hash of moved value"<<std::endl;
    return 0;
  }
};

struct num_equal {
  bool operator()(num const& a,num const& b)const {
    if (a.value<0 || b.value<0) {
      std::cout<<"compare of moved value: a="<<a.value<<",
b="<<b.value<<std::endl;
      return false;
    }
    return a.value==b.value;
  }
};

int main() {
  std::unordered_multiset<num,num_hash,num_equal> mset;
  mset.insert(num(1));
  mset.insert(num(2));
  mset.insert(num(1));
  mset.insert(num(2));
  if (mset.size()!=4) abort();
  auto iter=mset.cbegin();
  int x0=(iter++)->value;
  int x1=(iter++)->value;
  int x2=(iter++)->value;
  int x3=(iter++)->value;
  if ((x0==1 && x1==1 && x2==2 && x3==2) ||
      (x0==2 && x1==2 && x2==1 && x3==1)) return 0;
  std::cout<<x0<<' '<<x1<<' '<<x2<<' '<<x3<<std::endl;
  abort();
}

$ g++ -std=c++0x test.cpp && ./a.out 
compare of moved value: a=-1, b=1
compare of moved value: a=-1, b=2
compare of moved value: a=-1, b=1
compare of moved value: a=-1, b=1
compare of moved value: a=-1, b=2
compare of moved value: a=-1, b=1
2 1 2 1
Aborted


             reply	other threads:[~2012-01-16  1:34 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-16  1:44 roman at binarylife dot net [this message]
2012-01-16  1:51 ` [Bug libstdc++/51866] " paolo.carlini at oracle dot com
2012-01-16  1:53 ` [Bug libstdc++/51866] [c++0x][4.7 Regression] unordered_multiset " roman at binarylife dot net
2012-01-16  2:53 ` paolo.carlini at oracle dot com
2012-01-16  3:31 ` paolo.carlini at oracle dot com
2012-01-16 12:21 ` paolo.carlini at oracle dot com
2012-01-16 12:40 ` redi at gcc dot gnu.org
2012-01-16 12:54 ` redi at gcc dot gnu.org
2012-01-16 12:56 ` redi at gcc dot gnu.org
2012-01-18 20:31 ` fdumont at gcc dot gnu.org
2012-01-18 20:53 ` fdumont at gcc dot gnu.org

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-51866-4@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).