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 via email to