Live data from Hacker News

A quick primer on type traits in modern C++

internalpointers.com

11–20 of 38 posts

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

#12
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 think that it's pretty easy if you just consider that what's happening between `template` is a simple LISP whose values are types.

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

#13

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

> I found the use of 'if constexpr' in the example pretty nasty.

For new code it is to be preferred - it generates less symbols than enable_if, compiles faster (by a noticeable margin from my own experience) and makes for more readable code as you don't have to go look everywhere for the various enable_if cases.

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

#14
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++

Scott Meyers' next Effective C++ is going to be a doozy. Is it even possible to usefully categorize all the edge cases anymore?

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

#15
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 think that it's pretty easy if you just consider that what's happening between `template ` is a simple LISP whose values are types.

more like a prolog with obtuse syntax and complicated semantics

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

#16
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++

Scott Meyers' next Effective C++ is going to be a doozy. Is it even possible to usefully categorize all the edge cases anymore?

Scott Meyers has retired from C++ (his own blog post: http://scottmeyers.blogspot.com/2015/12/good-to-go.html?m=1) so the next C++ bible has to come from somewhere else. Maybe he saw it coming :)

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

#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 it back looks like poorly thought out code. Sure, there are times when you more or less have to, but you ought to try to avoid them.

But what do I know, I haven't done C++ for about 5 years and was never that into it.

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

#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.

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

#19
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…

I have an example for you. A serialization library that supports container classes. It has a generic way to fill lists (vectors), but not all list containers support resize and default-initialize new elements. So somewhere inside a generic template function there is a switch to use or replicate that function.

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

#20
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…

You do not switch on types, you switch on groups of tuoes having certain properties. It is not a new thing this sort of thing has been doable for the last 20 years in a form or another.
Post reply on HN