私は関数ポインタのconstexpr配列を持っていて、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);
さて、このコードは、クランとMSVCでコンパイルをFindMatchingIdx
が配列の最初の要素で呼び出されたとき、しかし、GCCはコンパイルされます。
error: '(test1 != test2)' is not a constant expression.
FindMatchingIdx
が再帰している場合は、GCCは以下となります。FindMatchingIdx
がtest1
で呼び出された場合、GCCはFindMatchingIdx
がtest2
またはtest3
GCCは、エラーメッセージを与えて、コードをコンパイルに失敗すると呼び出されたが場合、コードをコンパイルします。それをconstexpr関数として扱いません。これはGCCのバグですか?関数ポインタの比較は、constexpr関数の内部でもどのように機能しますか?明らかに、実際のポインタ値をリンカによって割り当てられた値として使用することはできません。
の作業例:https://godbolt.org/g/xfv1PM
これはおそらく 'operator!=(T、T)'です。 'T'は関数ポインタが' constexpr'ではなく、 'operator ==(T、T)'です。 。 – 101010