相关疑难解决方法(0)

static constexpr指向函数的指针,编译器之间的区别

在回答这个问题时,我尝试使用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)
  1. 哪个编译器是对的?
  2. 如果clang是对的,为什么,这个错误究竟意味着什么?

编辑:对于"为什么",请参阅DyP的问题.

c++ static function-pointers constexpr c++11

9
推荐指数
1
解决办法
2399
查看次数

标签 统计

c++ ×1

c++11 ×1

constexpr ×1

function-pointers ×1

static ×1