4 ms·
Why do you really need to do template metaprogramming? Generally that is not required even in performance sensitive code. Perhaps a few conditionals a'la enabl
by AstralStorm 10y ago
Why do you really need to do template metaprogramming?
Generally that is not required even in performance sensitive code. Perhaps a few conditionals a'la enable_if or direct use of SFINAE... but most everything else, not really.
- hughw 10y agoI needed to create a custom iterator for Thrust, a STL/Boost-like library for CUDA.
- hughw 10y agoAdditionally: I needed to make a program to join two tables by a common subset of their columns. It's desirable in CUDA, to enhance caching locality, to work with structs of arrays rather than arrays of structs. So a pattern in Thrust is to work with tuples. You zip up your columns and then you can use them in transformations. That's nice enough. The trouble arises when you need to write code to join (and transform) table A having NA columns, by table B having NB columns, sharing some number of shared columns NC. To manipulate the tuples you end up specializing cases and you build some awful large switch statements. That is, if you're a naive rookie, like me. Then you back off, and try to learn a little meta programming. You actually can dynamically treat tuples, sort of like lists, a little bit. But finally I decided that too is a kind of madness, and I'm on my third try, this time using a custom iterator over good old-fashioned dynamic vectors of arrays, so I can sidestep tuples altogether, and still use the structure of arrays idiom.
- hughw 10y agoOh -- and enable_if, and SFINAE -- these are terms I've learned only in the last three weeks, but yes, I would count having to learn even those concepts, to successfully use the language, as an obstacle and not for the faint of heart, right?
- int_19h 10y agoGenerally speaking, you don't have to know them unless you're writing generic libraries in the vein of STL and Boost. If you're just using those, then SFINAE etc is the magic under the hood that makes it all "just work".
- mannykannot 10y agoSooner or later you will likely be faced with having to understand code that was written by someone who had just learned about template metaprogramming and was determined to put his new-found knowledge to use.
- hughw 10y agothat's gonna be the next guy who looks at this code i'm writing... Edit: Clarification, I am not actually having myself to do original metaprogramming, but in order to diagnose problems implementing the custom iterators, I have needed to delve into, and understand, the meaning of the Thrust library code.
- mannykannot 10y agoYou ask way too many pertinent questions to be the sort of person who does what I am thinking of.