一元 + int& 上的运算符
我有以下语句并且可以编译:
static unsigned char CMD[5] = {0x10,0x03,0x04,0x05,0x06};
int Class::functionA(int *buflen)
{
...
int length = sizeof(CMD); + *buflen; // compiler should cry! why not?
...
}
为什么我没有得到编译器错误?
I have following statement and it compiles:
static unsigned char CMD[5] = {0x10,0x03,0x04,0x05,0x06};
int Class::functionA(int *buflen)
{
...
int length = sizeof(CMD); + *buflen; // compiler should cry! why not?
...
}
Why I get no compiler error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是一元
+
运算符在int&
上的有效应用,它基本上是一个 noop。这与您编写的内容相同:请参阅此处了解一元
operator+
实际执行的操作整数,以及此处您实际上可以用它做什么。Is a valid application of the unary
+
operator on anint&
, it's basically a noop. It's the same as if you wrote this:See here for what the unary
operator+
actually does to integers, and here what you can practically do with it.因为它并没有错,只是一种没有任何作用的说法。
如果你用 -Wall 标志编译(gcc/g++),你就会看到。
Because it isn't wrong, just a statement with no effect.
If you compile (gcc/g++) with the flag -Wall you'll see.
我从这个问题的标题“在分号后另一个命令并编译”中猜想,您认为每行只能有一个命令/语句?
正如您所注意到的,这是错误的。 C++ 和 C 是自由格式语言(这意味着您可以将符号排列成任意形式)您认为合适的方式)。分号只是一个语句终止符。
您可以编写
foo();bar();
或两种(以及更多)安排都完全没问题。顺便说一句,这是一个功能,而不是一个错误。有些语言(Python、早期的 Fortran)不具有该属性。
正如其他人正确指出的那样,您的具体声明是无操作的,是没有任何效果的声明。有些编译器可能会警告您这一点 - 但没有编译器会警告您一行上有多个语句。
I guess from this Question's title "After semicolon another command and it compiles" that you think that there can only be one command/statement per line?
As you noticed, this is false. C++ and C are free-form languages (which means that you can arrange the symbols in any way you see fit). The semicolon is just a statement terminator.
You may write
foo();bar();
orBoth (and more) arrangements are totally fine. By the way, that's a feature, not a bug. Some languages (Python, early Fortran) don't have that property.
As others have correctly pointed out, your specific statement is a no-op, a statement without any effect. Some compilers might warn you about that - but no compiler will warn you about multiple statements on one line.