[cfe-dev] static_assert without C++11 and with a message?
Howard Hinnant
hhinnant at apple.com
Tue Jun 19 09:06:44 PDT 2012
On Jun 19, 2012, at 3:22 AM, Stephen Kelly wrote:
>
> Hi there,
>
> I was looking into libc++, and noticed the implementation of static_assert
> for pre-c++11 compilers.
>
> Inside of the _LIBCPP_HAS_NO_STATIC_ASSERT ifdef, there is a typo (__t
> should be __m I presume?),
Nope.
> but even when that is fixed, I can't see how the
> static assert macro could work.
>
> It would expand to
>
> typedef __static_assert_check<unsigned> "some message";
>
> in both the true and false case. For the false case it fails to compile the
> __static_assert_check<unsigned> already, but for the true case it also fails
> to compile. How should that work?
For me, preprocessing this in C++03 mode:
#include <__config>
int main()
{
const int i = 0;
static_assert(i == 0, "");
}
Yields:
typedef __char16_t char16_t;
typedef __char32_t char32_t;
namespace std {
inline namespace __1 {
}
}
template <bool> struct __static_assert_test;
template <> struct __static_assert_test<true> {};
template <unsigned> struct __static_assert_check {};
int main()
{
const int i = 0;
typedef __static_assert_check<sizeof(__static_assert_test<(i == 0)>)> __t6;
}
Since i == 0 is true, __static_assert_test<true> is instantiated, which has a complete type. So __t6 is a typedef for:
__static_assert_check<sizeof(__static_assert_test<true>)>
If i != 0, an attempt would be made to form a typedef to:
__static_assert_check<sizeof(__static_assert_test<false>)>
but __static_assert_test<false> is an incomplete type, and thus a compile time error will be emitted.
Is this not the behavior you're seeing?
Howard
More information about the cfe-dev
mailing list