简体   繁体   中英

Variadic template deduction using type alias

I have a program like this:

template<typename ...Args>
using Function = void(*)(Args *...);

template<typename ...Args>
void DoThing(Function<Args...> func) { }

void IntFunction(int *i) { }

int main(int argc, char *argv[]) {
  DoThing(IntFunction);
}

When I run the program I get this error

$ clang++ -std=c++14 template.cpp
template.cpp:12:3: error: no matching function for call to 'DoThing'
  DoThing(IntFunction);
  ^~~~~~~
template.cpp:7:6: note: candidate template ignored: substitution failure [with Args = int]
void DoThing(Function<Args...> func) { }
     ^
1 error generated.

But if I compile it using g++ I don't get any errors.

It appears that clang is having trouble deducing the variadic template parameters when used in a type alias. If I replace the variadic parameters with standard ones then I don't get the error anymore.

Which compiler is giving me the proper result? And why would I not be allowed to do this?

Can be reduced to

template <typename... T>
using funptr = void(*)(T...);

template <typename... T>
void f(funptr<T...>) {}

template void f(void(*)());

This is valid code; if we substitute funptr<T...> by the corresponding pack expansion, Clang suddenly doesn't complain anymore.

Reported as #25250 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM