mp-units icon indicating copy to clipboard operation
mp-units copied to clipboard

refactor: specializations of standard templates may lead to UB

Open JohelEGP opened this issue 2 years ago • 3 comments

One of the provisions of https://eel.is/c++draft/namespace.std#2 is

that (a) the added declaration depends on at least one program-defined type

A template specialization of the form

template<concept_name T> struct std::something<T>;

arguably does not depend on a program-defined type; its instantiations do. This library has plenty of that form.

Another potential issue is the increase in compile times with the proliferation of this form of specialization. This is similar to the benefits of using hidden friends. When a specialization depends on a program-defined type, the compiler skips all concept-constrained specializations (IIUC, see https://godbolt.org/z/E5Yv3vYP4).

Note that changing the specializations to use quantity<...> rather than Quantity might affect users who derive from quantity<...>.

JohelEGP avatar Nov 16 '21 21:11 JohelEGP

This library has plenty of that form.

Are there any other such specializations than std::common_type?

mpusz avatar Nov 17 '21 08:11 mpusz

Not that I can remember. I exaggerated a bit. The few I know are together and for the same primary template.

JohelEGP avatar Nov 17 '21 13:11 JohelEGP

When a specialization depends on a program-defined type, the compiler skips all concept-constrained specializations

I was wrong. You can see at https://godbolt.org/z/GeqhMoEo5 that checking whether a partial specialization is viable causes the instantiation of the class' body (and possibly more, depending on what the partial specialization checks).

It seems that reachable, constrained partial specializations can have unintended effects. I personally found this out when adding one for test types to cover a wide range of unit tests. A previous test on a trait that accepts incomplete types, for std::complex<void>, was now instantiating it and resulting in hard errors.

JohelEGP avatar Jan 19 '22 14:01 JohelEGP