Skip to content

Use [[clang::no_specializations]] to indicate templates which users are forbidden to specialize #5179

@CaseyCarter

Description

@CaseyCarter

Clang has recently added support for a new attribute [[clang::no_specializations]]. (With an ugly equivalent [[_Clang::__no_specializations__]].) This is very similar in effect to the warning C5278 "adding a specialization for 'type trait' has undefined behavior" that MSVC added even more recently. MSVC's approach has some shortcomings:

  • MSVC's diagnostics are restricted to just the subset of type traits that the compiler implements directly, whereas we could apply an attribute to every pertinent template in the Standard Library.
  • MSVC's diagnostics are triggered by existing Standard Library code that doesn't have suppression. (This is perhaps surprisingly a real issue, since Windows carries several old STL implementations for reasons.)

We should add attribute annotations to our headers to use the Clang attribute. I assume we can detect support with __has_attribute(_Clang::__no_specializations__). This work will need to be verified with Clang 20. MSVC will likely implement a similar attribute in the near future, so these header annotations will do double duty.

The attribute takes an optional string literal argument (a la [[nodiscard("Who would discard a kitten, you monster!")]]) that is included in the error message, which we should bear in mind while doing this work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementSomething can be improved

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions