我经常看到关于在 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:
这篇关于什么时候重载逗号运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何在 C++ 中读取和操作 CSV 文件数据?How can I read and manipulate CSV file data in C++?(如何在 C++ 中读取和操作 CSV 文件数据?)
在 C++ 中,为什么我不能像这样编写 for() 循环:In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,为什么我不能像这样编写 for() 循环: for(
OpenMP 如何处理嵌套循环?How does OpenMP handle nested loops?(OpenMP 如何处理嵌套循环?)
在循环 C++ 中重用线程Reusing thread in loop c++(在循环 C++ 中重用线程)
需要精确的线程睡眠.最大 1ms 误差Precise thread sleep needed. Max 1ms error(需要精确的线程睡眠.最大 1ms 误差)
是否需要“do {...} while ()"?环形?Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?环形?)