https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66717

            Bug ID: 66717
           Summary: In variable declaration, decltype incorrectly deduces
                    return type of function returning const reference to
                    int
           Product: gcc
           Version: 5.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: charlie at charliedyson dot net
  Target Milestone: ---

Applying decltype to a function returning const int & appears to deduce int
rather than const int &. This only seems to happen when declaring a variable.
Here's an example showing this using error messages:

#include <iostream>
#include <string>

const int &const_ref_to_int ()
{
  static int i;
  return i;
}

const std::string &const_ref_to_string ()
{
  static std::string s;
  return s;
}

int main ()
{
  // error: invalid conversion from 'const char*' to 'int' [-fpermissive]
  decltype (const_ref_to_int ()) x = "foo";

  // error: invalid initialization of reference of type 'const string&
  // {aka const std::__cxx11::basic_string<char>&}' from expression
  // of type 'double'
  decltype (const_ref_to_string ()) y = 1.23;

  // But this is fine:
  static_assert (
    std::is_same<decltype (const_ref_to_int ()), const int&>(), "");
}

Above example on Godbolt: https://goo.gl/sWgdc9

Reply via email to