public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/65786] New: Wrong code when using decltype to specify the return type
@ 2015-04-16  8:27 josopait at goopax dot com
  2015-04-16  8:40 ` [Bug c++/65786] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: josopait at goopax dot com @ 2015-04-16  8:27 UTC (permalink / raw)
  To: gcc-bugs

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;
}


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-04-16  9:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-16  8:27 [Bug c++/65786] New: Wrong code when using decltype to specify the return type josopait at goopax dot com
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

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).