In the following example, template struct A has a constructor from a function type T(), and then objects of A are constructed: one with explicit type specification A<int> x and the other with template argument auto-deduction A y:
template<typename T>
struct A{
A(T f()){ f(); }
};
int foo() { return 1; }
int main() {
[[maybe_unused]] A<int> x = foo; // ok everywhere
[[maybe_unused]] A y = foo; // Clang error
}
This program is accepted as a whole by GCC, however Clang cannot auto-deduce the template argument:
error: no viable constructor or deduction guide for deduction of template arguments of 'A'
note: candidate template ignored: could not match 'A<T>' against 'int (*)()'
note: candidate template ignored: could not match 'T ()' against 'int (*)()'
Demo: https://gcc.godbolt.org/z/e9aGqoT1s
Is the program not well-formed, or Clang misses some feature required for auto-deduction?
<functional>has deduction guides that make your code valid since c++17, seems like a clang bug?explicit A( T (*f)() ){ f(); }and call like:A y(foo);(or some other more c++ scheme like std::function or template the function type)?std::initializer_list.