TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

A brief guide to C++ template metaprogramming

88 pointsby Halienjaover 8 years ago

6 comments

TickleSteveover 8 years ago
In general I like templates.... but that example is just twisted....<p>Handle errors appropriately, don&#x27;t wrap a C API like that for gods sake, thats just wasted code trying to be clever.<p>In 5 years time, someone will look at that and have to try and figure out what the <i></i><i></i> you were trying to do.
shin_laoover 8 years ago
I am absolutely unconvinced by the added value of wrapping the C functions. Using TMP would be useful to adapt the C functions to a different interface, but from my understanding all it does is catching the error.<p>Additionally, the author implements count in the worst way possible, it&#x27;s highly inefficient and it will result in very long compilation time. You don&#x27;t need to do tail recursion, you can just write:<p>template &lt;class... T&gt; using count = std::integral_constant&lt;std::size_t, sizeof...(T)&gt;;<p>Such errors make me uncomfortable as it seems the author isn&#x27;t up to date on the topic. A+ for intent though.
评论 #12575672 未加载
cubaiaover 8 years ago
I think templates get a bad reputation because in practice many uses of templates are not appropriate.<p>Too many clever people saw 3 repetitions of a pattern and decided to show case every template trick to create a &quot;generic library&quot; that is compile-time optimized, usually to solve a problem that doesn&#x27;t exist.<p>Multiply that many times through the course of a project and you end up with code that seems to try to maximizes job security.
评论 #12575508 未加载
lorenzhsover 8 years ago
This is great reading, and helped me clear up a few misconceptions about how SFINAE works. I really don&#x27;t understand the negativity here.
qwertyuiop924over 8 years ago
Implementing generics was a good idea. However, templates were not. They should have been dismissed as a bad job years ago, and replaced by a better metaprogramming facility with less complexity, if at all possible (given the complexity of C++, it may not be). Kind of like what Rust did, by pretty much just stealing syntax-rules wholesale from Scheme.
评论 #12575314 未加载
dukoidover 8 years ago
I expected a single page that just says &quot;Don&#x27;t&quot; in big letters :)
评论 #12574813 未加载
评论 #12576134 未加载