Live data from Hacker News

A quick primer on type traits in modern C++

internalpointers.com

1–10 of 38 posts

Re: A quick primer on type traits in modern C++

#5
post #3

algorithm_signed() and algorithm_unsigned() in the conditional compilation example need to be templated functions for the example to make (more) sense.

Is algorithm being templated not enough?

Two points. 1) Currently, the code could be written with normal overloading (one for int and one for unsigned). No traits required. 2) The current code relies on an implicit cast to int or unsigned int. I’m not sure that’s good.

Re: A quick primer on type traits in modern C++

#7
post #5

Earlier quoted context omitted.

Is algorithm being templated not enough?

Two points. 1) Currently, the code could be written with normal overloading (one for int and one for unsigned). No traits required. 2) The current code relies on an implicit cast to int or unsigned int. I’m not sure that’s good.

> Currently, the code could be written with normal overloading (one for int and one for unsigned). No traits required

f( (uint8_t)0 ) will dispatch to the overload f(int) with overloads. The type-trait code will dispatch to f_unsigned(unsigned).

Re: A quick primer on type traits in modern C++

#10
I found the use of 'if constexpr' in the example pretty nasty. I would have thought it more idiomatic to use std::enable_if to enable different implementations of 'algorithm' as in the example of 'construct' and 'destroy' given here:

https://en.cppreference.com/w/cpp/types/enable_if

Post reply on HN