https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79167

            Bug ID: 79167
           Summary: LHS of assignment operator side-effect sequenced
                    before RHS computation
           Product: gcc
           Version: 5.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ronen at barzel dot org
  Target Milestone: ---

Created attachment 40556
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=40556&action=edit
g++ --save-temps output

According to http://en.cppreference.com/w/cpp/language/eval_order, 

    20) In every simple assignment expression E1=E2 and every compound
assignment expression E1@=E2, every value computation and side-effect of E2 is
sequenced before every value computation and side effect of E1

But in this case, the size of the map gets incremented by the LHS before
m.size() gets evaluated on the RHS:   

    #include <map>
    #include <string>
    #include <cassert>

    int main(int argc, char *argv[])
    {
        std::map<std::string, int> m;
        m["bug"] = m.size();
        assert(m["bug"] == 0);
        return 0;
    }

i.e. m["bug"] should be equal to 0 but it is getting set to 1.

version:
    $ g++ --version
    g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
    Copyright (C) 2015 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    $

To reproduce:
    $ g++ bug.cpp
    $ ./a.out
    a.out: bug.cpp:9: int main(int, char**): Assertion `m["bug"] == 0' failed.
    Aborted
    $

Reply via email to