有人有关于使用运算符“”的信息吗?
Bjarne Stroustrup 今天在 Going Native 2012 会议上发表了主题演讲。在他的演讲中,他讨论了执行正确单位的问题。他对此的优雅(恕我直言)解决方案涉及使用我以前从未听说过的运算符:operator""
。使用此运算符,他能够编写如下所示的 C++ 代码:
ratio = 100m / 1s;
其中 operator""m(...)
和 operator""s(...)
被定义。
有谁知道有关如何实际使用此运算符的任何文档(或者即使任何现代 C++ 编译器都支持它)?我尝试在网上搜索,但没有成功。任何帮助将不胜感激。
Bjarne Stroustrup gave a keynote presentation today for the Going Native 2012 conference. In his presentation, he discussed the issue of enforcing correct units. His elegant (IMHO) solution to this involved using an operator I have never heard of before: operator""
. Using this operator, he was able to write C++ code that looked like this:
ratio = 100m / 1s;
Where operator""m(...)
and operator""s(...)
were defined.
Does anyone know of any documentation regarding how to actually use this operator (or even if any modern C++ compilers support it)? I tried searching online, but had no luck. Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要的语法是“用户定义的文字”,这是 C++11 的一项功能。
g++ 4.7 及以上版本更好地支持此功能。
以下是一些描述该运算符重载的使用的文档:
http://www.open-std。 org/jtc1/sc22/wg21/docs/papers/2008/n2765.pdf
另请参阅 Xeo 在评论中提供的优秀链接 问题。
The syntax you'd be looking for is "user-defined literals" which is a feature of C++11.
g++ versions 4.7 and better support this feature.
Here is some documentation describing the use of that operator overload:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2765.pdf
Also see the excellent link Xeo provides in the comments to your question.
目前最好的文档可能就在标准本身中。您可以从委员会网站获取最新版本。根据 gcc 网站,它将出现在 gcc 的下一个版本 (gcc-4.7) 中。当从 SVN 存储库构建 gcc 时,您应该能够测试它。
Currently the best documentation is probably in the standard itself. You can get the latest version from the commitee's site. According to gcc's site it will be in the next revision of gcc (gcc-4.7). You should be able to test it when building gcc from the SVN repository.