When to Overload the Comma Operator?(什么时候重载逗号运算符?)
问题描述
我经常看到关于在 C++ 中重载逗号运算符的问题(主要与重载本身无关,但与序列点的概念有关),这让我感到疑惑:
I see questions on SO every so often about overloading the comma operator in C++ (mainly unrelated to the overloading itself, but things like the notion of sequence points), and it makes me wonder:
什么时候应该重载逗号?它的实际用途有哪些例子?
When should you overload the comma? What are some examples of its practical uses?
我只是想不出任何我见过或需要的例子
I just can't think of any examples off the top of my head where I've seen or needed to something like
foo, bar;
在现实世界的代码中,所以我很好奇何时(如果有的话)实际使用它.
in real-world code, so I'm curious as to when (if ever) this is actually used.
推荐答案
让我们稍微改变一下重点:
Let's change the emphasis a bit to:
什么时候你应该重载逗号?
When should you overload the comma?
答案:从不.
例外:如果你在做模板元编程,operator, 在运算符优先级列表的最底部有一个特殊的位置,它可以派上用场,用于构建 SFINAE-guards 等.
The exception: If you're doing template metaprogramming, operator, has a special place at the very bottom of the operator precedence list, which can come in handy for constructing SFINAE-guards, etc.
我见过的重载 operator, 的仅有的两个实际用途都在 Boost:
The only two practical uses I've seen of overloading operator, are both in Boost:
- Boost.Assign
- Boost.Phoenix这里的基础在于它允许 Phoenix lambdas 支持多个语句
- Boost.Assign
- Boost.Phoenix – it's fundamental here in that it allows Phoenix lambdas to support multiple statements
这篇关于什么时候重载逗号运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么时候重载逗号运算符?
基础教程推荐
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 我有静态或动态 boost 库吗? 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
