11

Discussion

According to the standard §20.10.2/1 Header <type_traits> synopsis [meta.type.synop]:

1 The behavior of a program that adds specializations for any of the class templates defined in this subclause is undefined unless otherwise specified.

This specific clause contradicts to the general notion that STL should be expandible and prevents us from expanding type traits as in the example below:

namespace std {
template< class T >
struct is_floating_point<std::complex<T>> : std::integral_constant
         <
         bool,
         std::is_same<float, typename std::remove_cv<T>::type>::value  ||
         std::is_same<double, typename std::remove_cv<T>::type>::value ||
         std::is_same<long double, typename std::remove_cv<T>::type>::value
         > {};
}

LIVE DEMO

where std::is_floating_point is expanded to handle complex number with underlying floating point type as well.

Questions

  1. What are the reasons that made the standardization committee decide that type-traits should not be specialized.
  2. Are there any future plans for this restriction to be retracted.
4
  • @billz what about it? The program posted exhibits UB.
    – 101010
    Aug 17, 2014 at 1:29
  • Why not struct is_floating_point<std::complex<T>> : public std::is_floating::point<T> {};?
    – Manu343726
    Aug 17, 2014 at 1:39
  • @Manu343726 yes is sorter and nicer but also irrelevant, since your version also exhibits undefined behaviour.
    – 101010
    Aug 17, 2014 at 1:47
  • Even if it were allowed, specializing std::is_floating_point and making your entire translation unit consider std::complex to be a floating point type just so you can do a check of "is std::complex or floating point" in one of your functions is like blowing up your house to get some fresh air inside. The type trait classes query fundamental properties of types and are the basic building blocks for template metaprograms. It makes little sense to allow you to add specializations for them.
    – T.C.
    Aug 17, 2014 at 1:58

2 Answers 2

14

For the primary type categories, which is_floating_point is one, there is a design invariant:

For any given type T, exactly one of the primary type categories has a value member that evaluates to true.

Reference: (20.10.4.1 Primary type categories [meta.unary.cat])

Programmers can rely on this invariant in generic code when inspecting some unknown generic type T: I.e. if is_class<T>::value is true, then we don't need to check is_floating_point<T>::value. We are guaranteed the latter is false.

Here is a diagram representing the primary and composite type traits (the leaves at the top of this diagram are the primary categories).

http://howardhinnant.github.io/TypeHiearchy.pdf

If it was allowed to have (for example) std::complex<double> answer true to both is_class and is_floating_point, this useful invariant would be broken. Programmers would no longer be able to rely on the fact that if is_floating_point<T>::value == true, then T must be one of float, double, or long double.

Now there are some traits, where the standard does "say otherwise", and specializations on user-defined types are allowed. common_type<T, U> is such a trait.

For the primary and composite type traits, there are no plans to relax the restriction of specializing these traits. Doing so would compromise the ability of these traits to precisely and uniquely classify every single type that can be generated in C++.

3
  • 2
    There is also an ‘invariant’ of sorts in that the primary type category traits also reflect the truth. That is, a category such as ‘floating-point type’ is concretely described in the Standard—and they are very much not extensible or user-customizable. Thus a user-defined specialization would either re-state the obvious, or lie.
    – Luc Danton
    Aug 17, 2014 at 7:01
  • @Deduplicator: Ok, I've removed the embedding !. Aug 17, 2014 at 17:39
  • What about amending mistakes in the standard? Like making std::complex trivially_default_constructible (as we know should be) godbolt.org/z/lQlMTy .
    – alfC
    Mar 31, 2020 at 23:36
4

Adding to Howard's answer (with an example).

If users were allowed to specialize type traits they could lie (intentionally or by mistake) and the Standard Library could no longer assure that its behavior is correct.

For instance, when an object of type std::vector<T> is copied an optimization that popular implementations do is calling std::memcpy to copy all elements provided that T is trivially copy constructible. They might use std::is_trivially_copy_constructible<T> to detect whether the optimization is safe or not. If not, then the implementation falls back to the safe but slower method which is looping through the elements and call T's copy constructor.

Now, if one specializes std::is_trivially_copy_constructible for T = std::shared_ptr<my_type> like this:

namespace std {
    template <>
    class is_trivially_copy_constructible<std::shared_ptr<my_type>> : std::true_type {
    };
}

Then copying a std::vector<std::shared_ptr<my_type>> would be disastrous.

This would not be the Standard Library implementation's fault but rather the specialization writer's. To some extend, that's what the quote provided by the OP says: "It's your fault, not mine."

2
  • The example is very tutoring, clears thing up nicely.
    – 101010
    Aug 17, 2014 at 18:37
  • I don't think this example properly illustrates the issue. This example produces broken program not because we're specializing a type that we aren't supposed to specialize, but because we're providing false information about a type (shared pointer). The behavior of the program would be equally undefined if we inherited from false_type, while practically it would work fine, because the specialization would provide correct information. As a side note, copy ctor must also be implemented properly for library to guarantee correct vector copy, yet it's not illegal to implement it.
    – S. Kaczor
    Mar 17, 2021 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.