Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class template argument deduction for array of function pointer works on clang, but not on gcc

The following code:

#include <array>

template <int i>
auto f(){}

int main () {
    std::array{f<5>};
}

compiles with clang 7.0, but fails with gcc 8.2 with the message

prog.cc: In function 'int main()':
prog.cc:7:20: error: class template argument deduction failed:
     std::array{f<5>};
                    ^
prog.cc:7:20: error: no matching function for call to 'array(<unresolved overloaded function type>)'
In file included from prog.cc:1:
/opt/wandbox/gcc-8.2.0/include/c++/8.2.0/array:244:5: note: candidate: 'template<class _Tp, class ... _Up> std::array(_Tp, _Up ...)-> std::array<typename std::enable_if<(is_same_v<_Tp, _Up> && ...), _Tp>::type, (1 + sizeof... (_Up))>'
     array(_Tp, _Up...)
     ^~~~~
/opt/wandbox/gcc-8.2.0/include/c++/8.2.0/array:244:5: note:   template argument deduction/substitution failed:
prog.cc:7:20: note:   couldn't deduce template parameter '_Tp'
     std::array{f<5>};
                    ^
In file included from prog.cc:1:
/opt/wandbox/gcc-8.2.0/include/c++/8.2.0/array:94:12: note: candidate: 'template<class _Tp, long unsigned int _Nm> array(std::array<_Tp, _Nm>)-> std::array<_Tp, _Nm>'
     struct array
            ^~~~~
/opt/wandbox/gcc-8.2.0/include/c++/8.2.0/array:94:12: note:   template argument deduction/substitution failed:
prog.cc:7:20: note:   couldn't deduce template parameter '_Tp'
     std::array{f<5>};
                ^

Is this code legal? If not, how do I fix it?

like image 368
Baum mit Augen Avatar asked Jan 17 '19 12:01

Baum mit Augen


1 Answers

I believe the program is well formed. I faced a similar problem myself recently. GCC seems to have issues when it needs to deduce the placehoder return type while in the process of deducing the array arguments. Specifying the return type explicitly as void would make GCC accept your code, for instance.

Ultimately, the workaround is to split the declarations.

auto *p = f<5>;
std::array{p};
like image 185
StoryTeller - Unslander Monica Avatar answered Sep 23 '22 07:09

StoryTeller - Unslander Monica