[cfe-dev] The state of clang type traits

Howard Hinnant hhinnant at apple.com
Wed May 11 08:36:56 PDT 2011


On May 9, 2011, at 3:27 PM, Howard Hinnant wrote:

> Good work has been done on the clang type traits since the last time I did a survey.  I wanted to re-do the survey based on the good clang documentation at:
> 
> http://clang.llvm.org/docs/LanguageExtensions.html#checking_type_traits
> 
> I've put the updated results here:
> 
> http://libcxx.llvm.org/type_traits_design.html
> 
> The big needs are shown in the boxes with the red background.  The "nothrow" traits are especially needed in light of the recent work done for noexcept.  From the client's point of view, the only behavior change a noexcept spec gives to a member function is whether or not the associated is_nothrow_ trait returns true or not.  We currently can not detect nothrow destructors, nothrow constructors other than the default and copy constructors, nor nothrow assignment operators other than the copy assignment operator.
> 
> Three of the traits I can do in the library if CWG 1170 is implemented:
> 
> http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1170
> 
> This is the one where access checks should be done as part of the substitution process.  However I'd be happier with compiler type traits for those three.

I've learned that if CWG 1170 is implemented, I can also do library implementations of all of the nothrow traits by putting expressions into noexcept.  For example:

// is_nothrow_destructible

template <bool, class _Tp> struct __is_nothrow_destructible;

template <class _Tp>
struct __is_nothrow_destructible<false, _Tp>
    : public false_type
{
};

template <class _Tp>
struct __is_nothrow_destructible<true, _Tp>
    : public integral_constant<bool, noexcept(_STD::declval<_Tp>().~_Tp()) >
{
};

template <class _Tp>
struct is_nothrow_destructible
    : public __is_nothrow_destructible<is_destructible<_Tp>::value, _Tp>
{
};

This reduces the highest priority needs for clang type trait support to:

1.  Implement CWG 1170
2. __is_trivially_constructible(T, Args...)
3. __is_trivially_assignable(T, U)
4. __is_trivial(T)
5. __is_trivially_copyable(T)
6. __is_standard_layout(T)
7. __underlying_type(T)

An alternative to 2-5 is to implement a __trivial(expression) that can be tested similar to noexcept(expression).  This would allow me to use techniques as shown above for is_nothrow_destructible.

I'm looking for volunteers to help out in this area.

And I'd like to thank Eli Friedman for helping out so quickly on http://llvm.org/bugs/show_bug.cgi?id=9882 .

Howard




More information about the cfe-dev mailing list