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++11 user-defined literals and some constructions

30 pointsby peroneover 13 years ago

5 comments

Game_Enderover 13 years ago
As a programmer who has seen bad C++ code this just seems scary, an already complicated language gaining more complexity. Although I am sure it can be used to come up with some cool syntax for various different kinds of operations, hopefully it's not too opaque in the end.
albertzeyerover 13 years ago
Some interesting examples / possible use cases / discussion about user-defined literals in C++11: <a href="http://stackoverflow.com/questions/237804/user-defined-literals-in-c11-a-much-needed-addition-or-making-c-even-more-b" rel="nofollow">http://stackoverflow.com/questions/237804/user-defined-liter...</a>
prophetjohnover 13 years ago
This seems to be much in the same vein as operator overloading. As long as the programmer defines these with intuitive names, I think they'll serve to make C++ a more expressive and readable language.
geoffhillover 13 years ago
I think the primary disadvantage of an addition like this is the extra complexity is shifted almost completely into parsing. This means that accidental suffixes on the end of literals always have to be treated as custom literals by the parser, until they can be confirmed as invalid by a much later stage in the compiler.<p>In essence, an enormous class of syntactically invalid code is now syntactically valid and now just semantically invalid. A much harder bug to classify.
makecheckover 13 years ago
Like many language features, it can and will be abused; and like overloaded operators it may be very difficult to determine where a custom suffix is even defined.<p>It will definitely be nice in a few places. For instance I can imagine that writing 01011011_b (after defining "_b") would be very handy.<p>I think examples that attach units instead of just metric prefixes are missing the point because the result is always a <i>number</i> (or a string), not a particular kind of object. If you say "assert(1_kg == 2.2_lb)" that is <i>completely</i> misguided because you haven't specified the context; again, these are <i>just numbers</i> so what do you end up with, kilograms or pounds or something else? Far better to name suffixes only in terms of scaling factors, e.g. "_kilo" or "_K" defined as 1000x would be applicable in more situations.
评论 #3263937 未加载