raz*_*ebe 8 c++ cout comma-operator
在回答这个问题时,我遇到了这个代码......
#include <iostream>
int main()
{
int const income = 0;
std::cout << "I'm sorry your income is: " < income; // this is line 6
}
Run Code Online (Sandbox Code Playgroud)
...其中包含拼写错误.第<<
6行的第二个(预期)操作符被意外写为a <
.
除此之外,使用GCC 4.3.4或4.4.3 编译代码会产生警告:
prog.cpp: In function ‘int main()’:
prog.cpp:6: warning: right-hand operand of comma has no effect
Run Code Online (Sandbox Code Playgroud)
我的问题:为什么会产生特别警告?它指的是哪个逗号运算符?
注意:我并不是主张<
在cout
声明中故意使用单曲. 我只是偶然发现了这个警告,同时试图找出我已经链接到的另一个问题的答案,并且好奇为什么编译器会生成它.
我想他们只是忘了改变警告文字
int main() {
1, 2;
}
prog.cpp:2: warning: left-hand operand of comma has no effect
prog.cpp:2: warning: right-hand operand of comma has no effect
Run Code Online (Sandbox Code Playgroud)
该expr, expr
运算符将左操作数,然后计算右操作数,并产生正确的操作数的评价结果.如果右操作数没有效果且未使用其值,则可能是程序中的错误.
现在他们只是滥用上面的警告文本来警告其他二元运营商.