TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

A brief guide to C++ template metaprogramming

88 点作者 Halienja超过 8 年前

6 条评论

TickleSteve超过 8 年前
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_lao超过 8 年前
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 未加载
cubaia超过 8 年前
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 未加载
lorenzhs超过 8 年前
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.
qwertyuiop924超过 8 年前
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 未加载
dukoid超过 8 年前
I expected a single page that just says &quot;Don&#x27;t&quot; in big letters :)
评论 #12574813 未加载
评论 #12576134 未加载