Live data from Hacker News

A quick primer on type traits in modern C++

internalpointers.com

21–30 of 38 posts

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

#21
post #18
post #11

The amount of background knowledge you need to understand what’s happening here... I don’t envy people who have to catch up becoming more and more convinced that when sfinae opened the Turing hatch it doomed c++

traits + "constexpr if" replace SFINAE.

You often need sfinae to implement the trait itself, but concepts (and the 'requires' keyword itself) will finally get rid of sfinae.

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

#22

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

Enable if is fine if you have mutually exclusive choices (although the way it work is pure magic), but it quickly becomes very complex for overlapping cases as you have to somehow hack the partial ordering in. A chain of constexpr if is significantly easier to write and understand.

The right solution are of course concepts.

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

#23
post #11

The amount of background knowledge you need to understand what’s happening here... I don’t envy people who have to catch up becoming more and more convinced that when sfinae opened the Turing hatch it doomed c++

> The amount of background knowledge you need to understand what’s happening here...

Not a whole lot, actually. You can go from C to this in about a week if you’re focused. (I did this a few years ago when learning C++. Now if you want to talk about initialization, that’s a whole ‘nother story…)

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

#24
post #11

The amount of background knowledge you need to understand what’s happening here... I don’t envy people who have to catch up becoming more and more convinced that when sfinae opened the Turing hatch it doomed c++

You don’t need sfinae to get Turing completeness of templates. Template specialization (a crude form of pattern matching) is the main driver for Turing completeness. For example, here’s a toy project to implement a subset of scheme lisp in templates which needs no sfinae: https://github.com/tdp2110/TmpLisp

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

#25
post #11

The amount of background knowledge you need to understand what’s happening here... I don’t envy people who have to catch up becoming more and more convinced that when sfinae opened the Turing hatch it doomed c++

I seriously don't understand this. I am a developer learning Modern C++ and this article was perfectly clear to me. I was using `enable_if` before, but the new `if constexpr` does make things clear and idiomatic.

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

#26
post #11

The amount of background knowledge you need to understand what’s happening here... I don’t envy people who have to catch up becoming more and more convinced that when sfinae opened the Turing hatch it doomed c++

> …when sfinae opened the Turing hatch it doomed c++…

I think that’s unfair. While “sfinae’ itself is a mouthful, what it says in practice is that template expansion observes the principle of “least surprise” which is inherently user friendly.

You certainly don’t need to know the acronym, or even of the idea, to benefit from it. I stead, the developer won’t be presented with a confusing error message when the thing they want to happen is supported but not the first possibility examined.

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

#27
post #17
post #11

The amount of background knowledge you need to understand what’s happening here... I don’t envy people who have to catch up becoming more and more convinced that when sfinae opened the Turing hatch it doomed c++

It just looks kinda wrong to me - If your generic template has to switch on what type it's been passed, surely you've made it too generic somewhere? Adding back conditional compilation or execution afterwards strikes me as the same sort of code-smell as checking if something is an instance of a given class before then casting it and calling a member - you've lost information and using some sort of reflection to get i…

Think of it the other way around: there’s a generic method/template, but the developer has a special implementation for a specific case. The compiler already does this for things like memmove() that is properly aligned and sized; this allows user code to cleanly do the same thing.

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

#28
post #11

The amount of background knowledge you need to understand what’s happening here... I don’t envy people who have to catch up becoming more and more convinced that when sfinae opened the Turing hatch it doomed c++

I'm not sure, I last coded C++ about 8 years ago and that was all C++03, yet I had no trouble to follow along.

I mean, even if you never saw a "constexpr" before, isn't it pretty clear from the example and the name, well, "constexpr" want's going on? Same for the other newish C++ features.

I thought it was a nice little primer.

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

#29
post #26
post #11

The amount of background knowledge you need to understand what’s happening here... I don’t envy people who have to catch up becoming more and more convinced that when sfinae opened the Turing hatch it doomed c++

> …when sfinae opened the Turing hatch it doomed c++… I think that’s unfair. While “sfinae’ itself is a mouthful, what it says in practice is that template expansion observes the principle of “least surprise” which is inherently user friendly. You certainly don’t need to know the acronym, or even of the idea, to benefit from it. I stead, the developer won’t be presented with a confusing error message when the thing t…

I just learned about sfinae by googling it, so correct me if I'm wrong, but I think you're misreading the GP's comment.

The argument is I think, that because sfinae effectively added an "if" statement to C++ templates, it made templates turing complete. It made it possible to write all kinds insane, undebuggable programs purely at compile-time, in the most terrible purely functional language imaginable. People did exactly that, which went fine until their colleagues (or users of open source libraries) did something unexpected and got screenfuls of errors.

Wikipedia's sfinae page has a nice story about how it added compile-time introspection and conditionals to the language: https://en.wikipedia.org/wiki/Substitution_failure_is_not_an...

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

#30

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

Most of this is pretty new to me. Why is it nasty? It looks neat to me!
Post reply on HN