ArrayCount가 템플릿 일 때 clang과 gcc가 아래 코드를 컴파일하지 못합니다. 이것은 특히 ArrayCount 솔루션의 sizeof 작업에 비추어 잘못된 것 같습니다. ArrayCount의 템플릿 버전은 일반적으로 더 나은 솔루션입니다.하지만 여기까지가는 중입니다. constexpr은 겉으로보기에는 약속의 정신에 부합하지 않습니다.constexpr 템플릿 함수가 구성원 배열 크기를 const 식으로 표시하지 않습니다.
#if 1
template<typename T, size_t N>
constexpr size_t ArrayCount(T (&)[N])
{
return N;
}
// Results in this (clang): error : static_assert expression is not an integral constant expression
// Results in this (gcc): error: non-constant condition for static assertion, 'this' is not a constant expression
#else
#define ArrayCount(t) (sizeof(t)/sizeof(t[0]))
// Succeeds
#endif
struct X
{
int x[4];
X() { static_assert(ArrayCount(x) == 4, "should never fail"); }
};
연타/GCC 버전? 어느 stdlib? – Xeo