STL icon indicating copy to clipboard operation
STL copied to clipboard

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

Open CaseyCarter opened this issue 1 year ago • 2 comments

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.

CaseyCarter avatar Dec 11 '24 00:12 CaseyCarter

MSVC-PR-599993 implemented [[msvc::no_specializations]] for VS 2022 17.14 Preview 1.

StephanTLavavej avatar Jan 06 '25 07:01 StephanTLavavej

It seems that LWG-2129 was expected to be handled by compilers, and Clang (clang-cl) hasn't implemented it. [[clang::no_specializations]] might be necessary for Clang.

frederick-vs-ja avatar Feb 21 '25 02:02 frederick-vs-ja