【发布时间】:2017-02-16 13:16:47
【问题描述】:
假设我有一个 constexpr 函数指针数组,我想编写一个 constexpr 函数来查找指定函数的数组索引。
我可能有这样的代码:
void test1(){}void test2(){}void test3(){}void test4(){}
typedef void(*func)(void);
constexpr func funcs[] = { &test1, &test2, &test3 };
constexpr int FindMatchingIdx (const func work, const int idx) {
return (work == funcs[idx]) ? (idx) : (FindMatchingIdx(work, idx + 1));
}
constexpr unsigned int loc = FindMatchingIdx (&test1,0);
现在这段代码可以在 Clang 和 MSVC 上编译,但是 GCC 只有在使用数组中的第一个元素调用 FindMatchingIdx 时才会编译。如果FindMatchingIdx 用test1 调用,GCC 将编译代码,但是如果FindMatchingIdx 用test2 或test3 调用,GCC 将无法编译代码,并给出错误消息:
错误:'(test1 != test2)' 不是常量表达式。
如果FindMatchingIdx 必须递归,GCC 将无法将其视为 constexpr 函数。这是 GCC 中的错误吗?函数指针比较如何在 constexpr 函数中工作?显然它不能使用真正的指针值,因为它们是由链接器分配的。
【问题讨论】:
-
对我来说它看起来像一个错误,这可能是
operator!=(T, T)其中T是一个函数指针不是constexpr而operator==(T, T)它是。
标签: c++ c++11 c++14 language-lawyer constexpr