我有以下代码:
struct Processor
{
template <typename... ARGS>
void OnMsg(ARGS... args, int) {}
// but makine ARGS after int is fine:
// void OnMsg(int, ARGS... args) {}
};
struct Dispatcher
{
void Register(void (Processor::*memfun)(int)) {}
};
void Register(Dispatcher& dispatcher)
{
dispatcher.Register(&Processor::OnMsg<>);
}
GCC&gt; 4.7.1编译就好了。 Clang(any)没有,因为它无法实例化&#34; no-args&#34; OnMsg函数的版本。
17 : <source>:17:26: error: address of overloaded function 'OnMsg' does not match required type 'void (int)'
dispatcher.Register(&Processor::OnMsg<>);
^~~~~~~~~~~~~~~~~~
5 : <source>:5:10: note: candidate template ignored: failed template argument deduction
void OnMsg(ARGS... args, int) {}
^
12 : <source>:12:37: note: passing argument to parameter 'memfun' here
void Register(void (Processor::*memfun)(int)) {}
^
1 error generated.
Compiler exited with result code 1
奇怪的是,如果你将OnMsg函数中的ARGS ...移动到参数的末尾(在int的右边),它就可以在clang中运行。
Godbolt链接:https://godbolt.org/g/cJwXC7
哪种编译器是对的?