什么时候超载逗号运算符

什么时候超载逗号运算符

本文介绍了什么时候超载逗号运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会在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:

这篇关于什么时候超载逗号运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 00:34