[cfe-dev] static_assert without C++11 and with a message?

Richard Smith richard at metafoo.co.uk
Tue Jun 19 15:30:37 PDT 2012


On Tue, Jun 19, 2012 at 9:06 AM, Howard Hinnant <hhinnant at apple.com> wrote:
> 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;

This isn't quite right: the first argument to static_assert is
contextually converted to bool. So a more correct emulation would
produce:

   typedef __static_assert_check<sizeof(__static_assert_test<bool(i ==
0)>)> __t6;

Testcase:

  int n;
  static_assert(&n, "");




More information about the cfe-dev mailing list