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

            Bug ID: 60583
           Summary: Garbled data, "temporary bound ... only persists until
                    the constructor exits", fine with clang++
           Product: gcc
           Version: 4.8.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andreaskem at web dot de

Created attachment 32389
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=32389&action=edit
Problematic code

Hello,

I have a problem with the following code (boiled down from a larger source file
exhibiting the problem):

//------------------------
#include <iostream>

using namespace std;

struct Vector {
  double x;
  double y;
  double z;
};

class Baz {
  private:
    Vector r;
    Vector u;

  public:
    Baz(const Vector& p, const Vector& v) : r{p.x,p.y,p.z}, u{v.x,v.y,v.z} {}

    const Vector& get_u() const {
      return(u);
    };

    void write() const {
      cout << "r: " << r.x << "," << r.y << "," << r.z << "; u: " << u.x << ","
<< u.y << "," << u.z << endl;
    };
};

class Foo {
  private:
    const Baz& b;

  public:
    Foo(const Baz& b_) : b{b_} {}
    void bar();
};

void Foo::bar() {
  b.write();
  int iters{10};
  b.write();
}

int main(void) {
  Baz b{Vector{0.5, 0.5, 0.5}, Vector{1e6, 0.1, 0.1}};
  Foo f{b};
  f.bar();
}
//------------------------

Compiling this with
---
$ g++ -Wall -Wextra -std=c++11 ~/test.cpp
---
leads to the following warnings:
---
***test.cpp: In constructor ‘Foo::Foo(const Baz&)’:
***test.cpp:33:27: warning: a temporary bound to ‘Foo::b’ only persists until
the constructor exits [-Wextra]
***test.cpp: In member function ‘void Foo::bar()’:
***test.cpp:41:6: warning: unused variable ‘iters’ [-Wunused-variable]
---
and running the program shows that the data is garbled:
---
$ ./a.out
r: 6.95315e-310,2.07351e-317,0.5; u: 6.95315e-310,0.1,0.1
r: 6.95315e-310,2.07352e-317,0.5; u: 6.95315e-310,0.1,2.24932e-313
---

Using b(b_) instead of b{b_} seems to fix the first warning and the output. I
don't quite understand what is happening here. Moreover, clang++ (3.3 or 3.4)
does not print such a warning (it only complains about the unused iters
variable) and the output is not garbled:
---
$ clang++ -Wall -Wextra -std=c++11 ~/test.cpp; ./a.out
***test.cpp:41:6: warning: unused variable 'iters' [-Wunused-variable]
        int iters{10};
            ^
1 warning generated.
r: 0.5,0.5,0.5; u: 1e+06,0.1,0.1
r: 0.5,0.5,0.5; u: 1e+06,0.1,0.1
---

What is going on here?

I tried two gcc versions, 4.7.2 and 4.8.2.
gcc version 4.7.2 (Debian 4.7.2-5)
(I am currently at work and have to use gcc 4.7.2 to create the .ii file
attached to my report)
I first noticed the problem on my home computer with
gcc version 4.8.2 20140206 (prerelease) (GCC)

--------
Details:
gcc version 4.8.2 20140206 (prerelease) (GCC)
gcc version 4.7.2 (Debian 4.7.2-5)
clang version 3.4 (tags/RELEASE_34/final)
clang version 3.3 (tags/RELEASE_33/final)
Linux 3.13.6-1-ARCH #1 SMP PREEMPT Fri Mar 7 22:47:48 CET 2014 x86_64 GNU/Linux

Reply via email to