On 07/30/2013 11:27 AM, Ehsan Akhgari wrote:
> I just landed bug 895322, which removes the MOZ_STATIC_ASSERT macro in C++
> code, and replaces it with the C++11 static_assert keyword. You should use
> static_assert in C++ code from now on.
A belated note: MOZ_STATIC_ASSERT was valid at top level, and inside method
bodies. C++11 static_assert is considered a declaration in the spec, so it's
valid anywhere a declaration should go. In particular, it's valid inside class
bodies, and you no longer need class methods specifically to hold static
asserts:
accessible/src/generic/Accessible.cpp
3352-Accessible::StaticAsserts() const
3353-{
3354: static_assert(eLastChildrenFlag <= (2 << kChildrenFlagsBits) - 1,
3355- "Accessible::mChildrenFlags was oversized by
eLastChildrenFlag!");
3356: static_assert(eLastStateFlag <= (2 << kStateFlagsBits) - 1,
3357- "Accessible::mStateFlags was oversized by
eLastStateFlag!");
3358: static_assert(eLastAccType <= (2 << kTypeBits) - 1,
3359- "Accessible::mType was oversized by eLastAccType!");
3360: static_assert(eLastAccGenericType <= (2 << kGenericTypesBits) - 1,
3361- "Accessible::mGenericType was oversized by
eLastAccGenericType!");
3362-}
Usually it doesn't *hurt* to do it this way, except in noise. But it *does*
become problematic if the method's in a templated class, because the
static_asserts only happen if the method is instantiated (i.e. if it's called):
[jwalden@find-waldo-now tmp]$ cat i.cpp
#include <stddef.h>
template<size_t i>
struct Test
{
void foo() { static_assert(i != 0, "fail"); }
};
int main()
{
Test<0> t;
#if FAIL
t.foo();
#endif
}
[jwalden@find-waldo-now tmp]$ clang++-tip -std=c++11 i.cpp
[jwalden@find-waldo-now tmp]$ clang++-tip -std=c++11 i.cpp -DFAIL
i.cpp:6:16: error: static_assert failed "fail"
void foo() { static_assert(i != 0, "fail"); }
^ ~~~~~~
i.cpp:13:5: note: in instantiation of member function 'Test<0>::foo' requested
here
t.foo();
^
1 error generated.
So to avoid this little bit of surprise, you should put your static_asserts
inside the class you're asserting stuff about, whenever possible.
Jeff
_______________________________________________
dev-platform mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-platform