Why are type_traits implemented with specialized template structs instead of constexpr?

后端 未结 5 1799
别跟我提以往
别跟我提以往 2021-01-12 00:40

Is there any reason why the standard specifies them as template structs instead of simple boolean constexpr?

In an additional question that

5条回答
  •  暖寄归人
    2021-01-12 01:14

    I would say the mainreason is that type_traits was already part of tr1 and was therefore basically guaranteed to end up in the standard in more or less the same form, so it predates constexpr. Other possible reasons are:

    • Having the traits as types allows for overloading functions on the type of the trait
    • Many traits (like remove_pointer) define a type instead of a value, so they have to be expressed in this way. Having different interfaces for traits defining values and traits defining types seems unnessecary
    • templated structs can be partial specialized, while functions can't, so that might make the implementation of some traits easier

    For your second question: As enable_if defines a type (or not, if it is passed false) a nested typedef inside a struct is really the way to go

提交回复
热议问题