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.

C++'s SFINAE concept: compile-time introspection of a class

72 pointsby jguegantover 9 years ago

2 comments

welandover 9 years ago
I&#x27;m a little... confused is maybe too strong a word for this, I just don&#x27;t really grok this:<p>&gt; As you can see, during serialization, it comes pretty handy to be able to check if an object has an attribute and to query the type of this attribute. In our case, it permits us to use the serialize method if available and fall back to the more generic method str otherwise.<p>I would actually prefer if, during serialization, the compiler would yell at me that object C doesn&#x27;t have a serialize method. For one thing, it&#x27;s very likely that, while it does have a toString() method, it doesn&#x27;t have the corresponding fromString() method, making the serialized output useless. For the other, in a sane architecture, if an object does not have a serialize() method, then it&#x27;s either: a) because it shouldn&#x27;t be serialized in the first place, whatever the reason, or b) because <i>it should</i> have a serialize() method, but it has not yet been implemented (and it should!)<p>I understand that this is &quot;didactic&quot; example. Does anyone know of a more real-life&#x2F;relevant example where this can be used? SFINAE (Substitution Failure Is Not An Error) looks neat as a language feature but I don&#x27;t really see where it&#x27;s useful (in fact, it looks like a bug that was repurposed as a feature).
评论 #10486464 未加载
评论 #10487690 未加载
评论 #10486639 未加载
评论 #10487919 未加载
oldmanjayover 9 years ago
It&#x27;s clever. I admire and fear cleverness.
评论 #10490116 未加载