以下代碼被clang接受並被gcc拒絕。我想知道這是否是一個錯誤或我失去了一些東西:靜態成員函數不被接受爲constexpr參數
#include <array>
template<typename T>
static constexpr T Apply(T in, T fun(T)) {
return fun(in);
}
template <typename T, size_t N> struct Triangle {
using Ar = std::array<T, N>;
static constexpr Ar foo(Ar line) { return line; }
static constexpr Ar results = Apply<Ar>({{ 1 }}, foo); // foo({1}); is ok
};
template <typename T, size_t N> constexpr std::array<T, N> Triangle<T, N>::results;
int main() {
Triangle<unsigned long, 10>::results[0];
}
GCC錯誤消息是:
binom2.cpp: In instantiation of ‘constexpr const std::array<long unsigned int, 10ul> Triangle<long unsigned int, 10ul>::results’:
binom2.cpp:16:32: required from here
binom2.cpp:11:57: in constexpr expansion of ‘Apply<std::array<long unsigned int, 10ul> >(std::array<long unsigned int, 10ul>{std::__array_traits<long unsigned int, 10ul>::_Type{1ul}}, Triangle<T, N>::foo<long unsigned int, 10ul>)’
binom2.cpp:5:16: error: expression ‘Triangle<T, N>::foo<long unsigned int, 10ul>’ does not designate a constexpr function
return fun(in);
^
由於函數調用替換的規則,我認爲clang在這裏是正確的。 – Simple
我這麼認爲,如果我們改變Ar類型爲'int','Apply(1,foo)'按預期工作。 –
Jarod42
@ Jarod42:是的,我注意到,雖然試圖減少這個問題。對不起,我忘了提及它。 – hivert