问题描述
我会在C ++中看到关于重载逗号运算符的问题(主要与重载本身无关,但是类似序列点的概念),这让我想到:当应时,您的逗号超载了吗?它的实际用途是什么?
我只是想不到任何例子在我的头顶部,我看到或需要的东西像
foo,bar;
在现实世界的代码,所以我很好奇,
让我们将重点改为:
答案:永不。
例外:如果你正在进行模板元编程,运算符
放置在运算符优先级列表的最底部,这对于构造SFINAE-guard等可以派上用场。
我看到的只有两个实际用途运算符
都位于中:
- –它的根本在于它允许Phoenix lambdas支持多个语句
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:
The answer: Never.
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.
The only two practical uses I've seen of overloading operator,
are both in Boost:
- Boost.Assign
- Boost.Phoenix – it's fundamental here in that it allows Phoenix lambdas to support multiple statements
这篇关于什么时候超载逗号运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!