在回答这个问题时,我尝试使用gcc(代码编译)和clang(拒绝代码)的以下代码:
typedef long (*func)(int);
long function(int) { return 42; }
struct Test
{
static constexpr func f = &function;
};
template<func c>
struct Call
{
static void f()
{
c(0);
}
};
int main()
{
Call<Test::f>::f();
}
Run Code Online (Sandbox Code Playgroud)
我不确定哪个编译器是正确的,虽然我认为constexpr初始化Test::f是好的.错误clang输出是:
error: non-type template argument for template parameter of pointer type 'func'
(aka 'long (*)(int)') must have its address taken
Run Code Online (Sandbox Code Playgroud)
编辑:对于"为什么",请参阅DyP的问题.