deduced return type with function template

Viewed 134

Consider the following program:

#include<functional>

template<typename T>
T f() { return T{}; }

template<typename T>
auto g() { return T{}; }

int main() { 
    std::function<int()> a = f<int>;  // ok, clang and gcc
    std::function<int()> b = g<int>;  // ok, clang, error gcc
}

The error in gcc is

error: conversion from '<unresolved overloaded function type>' to non-scalar type 'std::function<int()>' requested

Here's the code to experiment with.

I don't understand why the program compiles only if the return type is T, and fails on gcc when the return type is deduced.

Edit: The comments suggest this is a gcc bug, but could I get a confirmation that the program is indeed well formed?

1 Answers

Indeed, f() and g() are similar functions, having the same result type.

It was just a bug in GCC, which is fixed in GCC 10.3. Clang and MSVC also accept the program, demo: https://gcc.godbolt.org/z/qPvar6vTh

So the program is well formed.

Related