23

Similar question: Why are type_traits implemented with specialized template structs instead of constexpr? – but with a different answer.

I realise that alias templates cannot be specialised and hence can’t currently be used to implement type traits directly1. However, this is a conscious decision of the committee, and as far as I see there is no technical reason to forbid this.

So wouldn’t it have made more sense to implement type traits as alias templates, streamlining their syntax?

Consider

 typename enable_if<is_pointer<T>::value, size_t>::type
 address(T p);

versus

 enable_if<is_pointer<T>, size_t> address(T p);

Of course, this introduces a breaking interface change when moving from Boost.TypeTraits – but is this really such a big problem?

After all, the code will need to be modified anyway since the types reside in different namespace and, as many modern C++ programmers are reluctant to open namespaces, will be qualified explicitly (if it would be changed at all).

On the other hand, it vastly simplifies the code. And given that template metaprogramming often gets deeply nested, convoluted and complex, it seems obvious that a clearer interface is beneficial.

Am I missing something? If not, I’d appreciate an answer that is not mere guesswork but relies on (and can cite) knowledge of the committee’s decision rationale.


1 But very well indirectly! Consider:

template <typename T> using is_pointer = typename meta::is_pointer<T>::type;

Where meta::is_pointer<T> corresponds to the current std::is_pointer<T> type.

Community
  • 1
  • 1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • "`is_pointer`" - does your `enable_if` alias take a type as the first argument? :) – Xeo Feb 29 '12 at 11:42
  • @Xeo: Nope, but the traits classes have a `constexpr` implicit conversion operator. :) – GManNickG Feb 29 '12 at 11:46
  • @Xeo Yes. This is the logical implementation once you have type traits as alias templates. – Konrad Rudolph Feb 29 '12 at 11:48
  • 1
    @GMan: No implicit conversion for template arguments. :P Also, they're types. Types can't be implicitly converted to anything! – Xeo Feb 29 '12 at 11:48
  • Btw, your `is_pointer` alias doesn't make sense currently. You either want a `typename` in there, or leave the `::type` out. – Xeo Feb 29 '12 at 11:52
  • @Xeo: It takes a type with `::value` member, i.e., a normal type trait. – R. Martinho Fernandes Feb 29 '12 at 12:25
  • I don't understand how this sort of change to standard signatures would simplify your code. :-S – wilhelmtell Feb 29 '12 at 12:37
  • 2
    @wilhelmtell Seriously!? Apart from the example clearly showing that it’s briefer, less boilerplate? And this is a *simple* example. I’ve seen, and used, much (!) more complexly nested template metaprogramming constructs. You cut down the amount of cruft almost by half. That’s a *huge* improvement. – Konrad Rudolph Feb 29 '12 at 12:45
  • @KonradRudolph well what I meant is that if we're talking about a change to a standard signature that doesn't affect the caller then it doesn't make our code simpler. it's always been like that in the stl, with tricks like tags in std::distance() and traits and so on. although i might have completely missed something here. i definitely agree though that making the stl simpler to read is a huge gain. – wilhelmtell Feb 29 '12 at 17:13
  • 1
    Sidenote: C++14 will add `enable_if_t` and other similar aliases. See http://en.cppreference.com/w/cpp/types/enable_if. Some compilers already support this. However, these have a different name (_t), so they won't break existing code. – Excelcius Jan 14 '14 at 10:44
  • @Excelcius Ah thanks, but no thanks. I’ll stick with [Wheels](https://bitbucket.org/martinhofernandes/wheels) by [Martinho](http://stackoverflow.com/users/46642/r-martinho-fernandes), it’s much more powerful. Still, good to know. – Konrad Rudolph Jan 14 '14 at 12:31
  • @KonradRudolph It's ok, just wanted to add the comment for completeness, in case someone is looking for a different solution. Since the question focusses on C++11, a C++14 alternative is no real answer anyway. – Excelcius Jan 14 '14 at 14:06
  • @Excelcius Actually now that I’ve had time to think about it, I hate how this has been handled. It adds yet more boilerplate (`_t`). It’s too late now of course (it would introduce a breaking change) but it should have been the other way round. The concise `using` aliases should have the short, usable names and the more complex (to use) structs should have longer names (or be in another namespace). There is literally no reason (other than backwards compatibility, now) to do it the other way round. – Konrad Rudolph Aug 21 '15 at 10:33

3 Answers3

17

The most concrete answer to your question is: No one ever proposed doing it that way.

The C++ standards committee is a multi-national, multi-corporation collection of volunteers. You're thinking of it as a design committee within a single organization. The C++ standards committee literally can not do anything without a proposal to put words into the draft standard.

I imagine that the reason there was no proposal is that type traits was an early proposal, with the boost implementation dating back to around 2000. And template aliases were late in getting implemented. Many of the committee members are reluctant to propose something that they have not implemented. And there was simply little opportunity to implement your proposal.

There was a lot of pressure to ship C++11. It really was intended to ship in 2009 and when that ship date slipped, it was very tough to do anything to the working paper besides fix the features already under consideration. At some point you've got to put great new ideas on the back burner, lest you will never ship.

Update

As of C++14, the TransformationTraits (those which result in a type) now have template alias spellings, for example:

template <bool b, class T = void>
  using enable_if_t = typename enable_if<b,T>::type;

And the C++1z working draft now has template variable spellings for the traits resulting in values:

template <class T>
  constexpr bool is_pointer_v = is_pointer<T>::value;

Also, even in C++11 one could do:

typename enable_if<is_pointer<T>{}, size_t>::type
address(T p);

I.e. you can use {} in place of ::value (assuming your compiler has constexpr support). In C++14 that becomes:

enable_if_t<is_pointer<T>{}, size_t>
address(T p);

And in C++1z:

enable_if_t<is_pointer_v<T>, size_t>
address(T p);

Note that the difference between C++1z and C++14 is so minimal that it doesn't even save characters, just changes {} to _v and changes where you put these two characters.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • “No one ever proposed …” – seriously? I find that hard to believe. I haven’t really followed the process that closely but there was an *immense* number of proposals. But it still makes sense, in particular in light of release pressure and the fact that alias templates came late to the game. – Konrad Rudolph Feb 29 '12 at 15:02
  • 7
    Let me rephrase: **You** did not propose it. Therefore we never reviewed your proposal. You have only yourself to blame. ;-) – Howard Hinnant Feb 29 '12 at 15:11
  • Aaah, screw me. Somebody should really roll up their sleeves and write a nice `using` wrapper. And before everybody looks at me: I don’t have the time. :-( – Konrad Rudolph Feb 29 '12 at 15:24
  • Martinho has written a wrapper, and also shows that the wrapper syntax can't completely replace the existing `::type` version. http://flamingdangerzone.com/cxx11/2012/05/29/type-traits-galore.html – Ben Voigt Dec 24 '12 at 18:25
  • 1
    Some of the standard type traits are scheduled for addition in the next standard revision via [N3655: Transformation Traits redux, v2](http://isocpp.org/files/papers/N3655.pdf) (excluding section 4). – boycy Aug 21 '13 at 09:25
2

Type traits, like several other libraries including <memory> and <functional>, were inherited from C++ TR1. Although that was a less formal document, it was more formal than Boost, and compatibility is worthwhile.

Also, note that type traits are all derived from std::integral_constant<bool>, which does implement a constexpr conversion function to bool. So that at least saves the ::value parts, if you so choose.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • Nitpick: There is no such thing as C++03 TR1. TR1 is a technical report, independent of C++03. TR1 is not a less formal standard. It is not a standard at all. A technical report does nothing but show the world that the committee is interested in a specific area. TR1 is an experimental interface. – Howard Hinnant Feb 29 '12 at 14:58
  • @HowardHinnant: Fixed (?). My point is that the committee voted on TR1, so it definitely had a foot in the door. When drafted it may have been mere interest, but when C++11 came it was all but wholly adopted. – Potatoswatter Feb 29 '12 at 15:04
  • I don’t believe that the implicit conversion helps in the context of my question. I agree that it might make the code simpler in some situations but for instance it does’t allow to write `typename enable_if >::type` (or does it?) since this would require that a *type* (rather than a value) be converted to a `bool`. I’d be surprised if that were legal. – Konrad Rudolph Feb 29 '12 at 17:19
  • 1
    @KonradRudolph: You do need to replace `::value` with `()`, come to think of it. I still prefer `::value`. – Potatoswatter Feb 29 '12 at 17:21
1

As a complete side-note since there seems to be confusion on how aliases may or may not help a trait like std::is_pointer:

You can go the Boost.MPL route and decide that you'll use Boost.MPL-style integral constants, which means types

template<typename Cond, typename Then = void>
using enable_if = typename std::enable_if<Cond::value, Then>::type;

// usage:
template<
    typename T
    , typename = enable_if<std::is_pointer<T>>
>
size_t address(T p);

or you can decide to use values instead

template<bool Cond, typename Then>
using enable_if = typename std::enable_if<Cond, Then>::type;

// can use ::value
template<
    typename T
    , typename = enable_if<std::is_pointer<T>::value>>
>
size_t address(T p);

// or constexpr conversion operator
template<
    typename T
    , typename = enable_if<std::is_pointer<T> {}>
>
size_t address(T p);

Note that in the latter case it's not possible to use enable_if<std::is_pointer<T>()>: std::is_pointer<T>() is a function type (taking void and returning std::is_pointer<T>) and is invalid since our alias takes a value and not a type in this case. The braces ensure that it is a constant expression instead.

As you may have noticed, std::is_pointer doesn't benefit from template aliases at all. This isn't surprising at it's a trait where the interesting part is accessing ::value, not ::type: template aliases can only help with member types. The type member of std::is_pointer isn't interesting since it's an Boost.MPL-style integral constant (in this case either std::true_type or std::false_type), so this doesn't help us. Sorry!

Luc Danton
  • 34,649
  • 6
  • 70
  • 114
  • True, this makes the implementation of such a library much shorter. I actually hadn’t thought of that since defining a `struct` to get a *trait* seems fundamentally a crutch. But of course it’s not actually shorter to define them as structs than to define them as alias templates, even if the latter could be specialised (and we’d discussed in the chat that this isn’t possible for good reasons). So yes, redefining `enable_if` in terms of an alias template and *not* the type traits makes a good deal of sense. – Konrad Rudolph Feb 29 '12 at 22:08
  • @KonradRudolph The Standard divides its traits into *UnaryTypeTrait*, *BinaryTypeTrait*, and *TransformationTrait*. The first two both involve `std::integral_constant` while the last one implies that there's an 'interesting' `type` member. Those traits are: `remove_const` (+ `add_const`, `remove_volatile` etc.), `remove_reference` (+ `add_lvalue_reference` etc.), `make_[un]signed`, `remove_extent` (+ `remove_all_extents`), `add_pointer` (+ `remove_pointer`). There's also a few more handful metafunctions that can also be turned into aliases, not least of which are `enable_if` and `decay`. – Luc Danton Feb 29 '12 at 22:21