From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 32558 invoked by alias); 16 Apr 2015 08:27:20 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 32526 invoked by uid 48); 16 Apr 2015 08:27:16 -0000 From: "josopait at goopax dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/65786] New: Wrong code when using decltype to specify the return type Date: Thu, 16 Apr 2015 08:27:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 4.9.2 X-Bugzilla-Keywords: X-Bugzilla-Severity: critical X-Bugzilla-Who: josopait at goopax dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2015-04/txt/msg01306.txt.bz2 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65786 Bug ID: 65786 Summary: Wrong code when using decltype to specify the return type Product: gcc Version: 4.9.2 Status: UNCONFIRMED Severity: critical Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: josopait at goopax dot com In the program below, the assignment of the return value is messed up. While the C++-14 style of fully automatic type deduction works fine, the mymax11 function call produces some random numbers. The output is: 1 2 -2100190336 or similar. The last line is different for every program execution. I am using gcc 4.9.2 on x86_64 Linux. I don't get this bug if I use -m32. #include using namespace std; struct testclass { int data; inline operator const int&() const { return data; } testclass& operator = (const int& in) { data = in; return *this; } }; template auto mymax14(const A& a, const B& b) { return std::max((int)a, (int)b); } template auto mymax11(const A& a, const B& b) -> decltype(std::max((int)a, (int)b)) { return std::max((int)a, (int)b); } int main() { testclass d; d = 1; cout << d.data << endl; // ok, d.data==1 d = mymax14(d, 2); cout << d.data << endl; // ok, d.data==2 d = mymax11(d, 2); cout << d.data << endl; // bad: d.data == some random number. return 0; }