一元+运算符有实际用途吗? [英] Does the unary + operator have any practical use?

查看:108
本文介绍了一元+运算符有实际用途吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一元运算符+仅是为了与一元运算符-对称而包括的,还是在C ++代码中找到了实际用途?

Was the unary + operator only included for symmetry with the unary - operator, or does it find some practical use in C++ code?

在这里搜索时,我遇到了 C?中一元'+'运算符的用途,但唯一有用的方案是预处理器宏.这些是很容易知道的,但是它们似乎是不太常见的情况,并且涉及宏.有没有涉及更常见的C ++代码的用例?

Searching here, I came across What is the purpose of the unary '+' operator in C?, but the only useful scenarios there involve preprocessor macros. Those are good to know, but they seem to be some less common situations, and involve macros. Are there any use cases involving more common C++ code?

推荐答案

char ch = 'a';
std::cout << ch << '\n';
std::cout << +ch << '\n';

第一次插入将字符a写入cout.第二次插入将ch的数值写入cout.但这有点晦涩难懂.它依赖于编译器为+运算符应用积分提升.

The first insertion writes the character a to cout. The second insertion writes the numeric value of ch to cout. But that's a bit obscure; it relies on the compiler applying integral promotions for the + operator.

这篇关于一元+运算符有实际用途吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆