public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "josopait at goopax dot com" <gcc-bugzilla@gcc.gnu.org>
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	[thread overview]
Message-ID: <bug-65786-4@http.gcc.gnu.org/bugzilla/> (raw)

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 <iostream>
using namespace std;


struct testclass
{
  int data;
  inline operator const int&() const
  {
    return data; 
  }

  testclass& operator = (const int& in)
  {
    data = in;
    return *this;
  }
};


template <typename A, typename B> auto mymax14(const A& a, const B& b)
{
  return std::max((int)a, (int)b);
}

template <typename A, typename B> 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;
}


             reply	other threads:[~2015-04-16  8:27 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-16  8:27 josopait at goopax dot com [this message]
2015-04-16  8:40 ` [Bug c++/65786] " pinskia at gcc dot gnu.org
2015-04-16  9:31 ` redi at gcc dot gnu.org
2015-04-16  9:35 ` josopait at goopax dot com
2015-04-16  9:53 ` glisse 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-65786-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).