I've noticed strange behavior of static_assert
:
#include <iostream>
template <typename T, unsigned int D> struct Vec
{
static_assert(D && 0, "Invalid dimension for vector!");
};
template <typename T> struct Vec<T, 1> {union {T x, r;};};
template <typename T> struct Vec<T, 2> : Vec<T, 1> {union {T y, g;};};
template <typename T> struct Vec<T, 3> : Vec<T, 2> {union {T z, b;};};
template <typename T> struct Vec<T, 4> : Vec<T, 3> {union {T w, a;};};
int main()
{
Vec<float, 3> v;
v.x = 1;
v.y = 2;
v.z = 3;
return 0;
}
It compiles fine: http://ideone.com/wHbJYP . I would expect
static_assert(0, "Invalid dimension for vector!");
to give me same result, but it causes static assertion failure: http://ideone.com/UEu9Kv .
Is gcc correct in both cases? If so, why? Or is it a gcc bug? Then, in which case gcc is correct?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…