[Bug c++/47828] New: GCC instantiates function template with "auto" type

schaub.johannes at googlemail dot com gcc-bugzilla@gcc.gnu.org
Mon Feb 21 05:30:00 GMT 2011


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47828

           Summary: GCC instantiates function template with "auto" type
           Product: gcc
           Version: 4.5.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: schaub.johannes@googlemail.com


The following causes GCC to instantiate a function template using "auto" as a
template argument:

template<typename T> T *f() { 
  // funny: T is auto here
  return 0; 
}

int main() {
  auto *(*g)() = f;
  g();
}

No error is generated: The compiled code runs. My best guess is, when taking
the address of an overloaded function, GCC deduces the overloaded function
against the target type, which apparently still is "auto*()" at that point, and
therefor deduces "T" to auto, and then when it declares variable "g" deduces
its type to "auto*(*)()". 

Frankly, I'm not sure what the Standard says how such cases need to be handled.
A reasonable action is to not do deduction against the target type "auto*()"
and keep 'f' being a set of overloaded functions until initialization. Then,
when it deduces the type of 'g', it would deduce it against a set of overloaded
functions containing function templates, thus creating a non-deduced context
and error out at that point. This matches clang's behavior, which accepts the
following:

void f();
void f(int);

int main() {
  auto (*g)() = f;
}

GCC rejects that currently, because it tries to deduce 'f' against the target
type early on, when "g"'s function type is still "auto()", while clang waits
and first deduces g's type using a set of overloaded functions, deducing the
"auto" to 'void'.



More information about the Gcc-bugs mailing list