[英]How to solve 'right-hand operand has no effect' error?
I am currently working with Eclipse CDT, the ESP-IDF framework and the ESP32 Wrover DevKit .我目前正在使用 Eclipse CDT、 ESP-IDF框架和ESP32 Wrover DevKit 。
I was looking for a C
vector implementation and stumbled over rxi/vec on GitHub .我正在寻找
C
向量实现并在 GitHub 上偶然发现了rxi/vec 。 I copied vec.h
and vec.c
to my project and tried to compile.我将
vec.h
和vec.c
复制到我的项目中并尝试编译。 I am getting the following error:我收到以下错误:
Error: right-hand operand of comma expression has no effect[-Werror=unused-value]
... at this line of code in vec.h
: ...在
vec.h
中的这行代码:
#define vec_push(v, val)\
( vec_expand_(vec_unpack_(v)) ? -1 :\
((v)->data[(v)->length++] = (val), 0), 0 )
Any ideas how solve this issue or work around?任何想法如何解决这个问题或解决? Any alternative vector implementation for
C
? C
任何替代向量实现?
../main/tools/inc/vec.h:35:42: error: right-hand operand of comma expression has no effect [-Werror=unused-value]
( vec_expand_(vec_unpack_(v)) ? -1 :\
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
((v)->data[(v)->length++] = (val), 0), 0 )
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
../main/main.c:44:2: note: in expansion of macro 'vec_push'
vec_push(&toc.items, item1);
^~~~~~~~
../main/tools/inc/vec.h:35:42: error: right-hand operand of comma expression has no effect [-Werror=unused-value]
( vec_expand_(vec_unpack_(v)) ? -1 :\
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
((v)->data[(v)->length++] = (val), 0), 0 )
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
../main/main.c:50:2: note: in expansion of macro 'vec_push'
vec_push(&item1.elements, elem1);
^~~~~~~~
../main/tools/inc/vec.h:35:42: error: right-hand operand of comma expression has no effect [-Werror=unused-value]
( vec_expand_(vec_unpack_(v)) ? -1 :\
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
((v)->data[(v)->length++] = (val), 0), 0 )
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
../main/main.c:56:2: note: in expansion of macro 'vec_push'
vec_push(&item1.elements, elem2);
^~~~~~~~
cc1: some warnings being treated as errors
#include <stdio.h>
#include <stdlib.h>
#include "vec.h"
typedef struct toc_element_t {
uint16_t uid;
char element[8];
char type[8];
} toc_element_t;
typedef vec_t(toc_element_t) vec_toc_element_t;
typedef struct toc_item_t {
char category[8];
vec_toc_element_t elements;
} toc_item_t;
typedef vec_t(toc_item_t) vec_toc_item_t;
typedef struct toc_t {
char description[8];
vec_toc_item_t items;
} toc_t;
void app_main(void) {
toc_t toc;
strcpy(toc.description, "toc1");
vec_init(&toc.items);
toc_item_t item1;
strcpy(item1.category, "cat1");
vec_init(&item1.elements);
vec_push(&toc.items, item1);
toc_element_t elem1;
elem1.uid=0;
strcpy(elem1.element, "elem1");
strcpy(elem1.type, "float");
vec_push(&item1.elements, elem1);
toc_element_t elem2;
elem2.uid=1;
strcpy(elem2.element, "elem2");
strcpy(elem2.type, "float");
vec_push(&item1.elements, elem2);
printf("TOC [%s]:\n", toc.description);
toc_item_t item; int index1;
vec_foreach(&toc.items, item, index1) {
printf(" - item[%d]=%s\n", index1, item.category);
toc_element_t element; int index2;
vec_foreach(&item.elements, element, index2) {
printf(" - element[%d]=%s [%s]\n", index2, element.element, element.type);
}
}
while(1);
}
Perhaps this issue is not related to this question, but since Eric mentioned the last commit with the log message “Fixed vec_insert() and vec_push() for structs” that inserted the 'fishy' ,0
that caused the problem discussed here, it may nonetheless be related.也许这个问题与这个问题无关,但由于 Eric 提到了最后一次提交的日志消息“Fixed vec_insert() and vec_push() for structs”插入了导致这里讨论的问题的'fishy'
,0
,它可能仍然是相关的。
When executing the code in section "Update B", I get the following output:执行“更新 B”部分中的代码时,我得到以下输出:
TOC [toc1]:
- item[0]=cat1
But I would expect the following:但我希望以下内容:
TOC [toc1]:
- item[0]=cat1
- element[0]=elem1
- element[1]=elem2
Solved: Why does the inner vector not get printed out?已解决: 为什么内部向量没有打印出来?
The compiler is right to warn you.编译器警告你是正确的。 Something is fishy there.
那里有些可疑。 The
… ? -1 : (…, 0)
… ? -1 : (…, 0)
… ? -1 : (…, 0)
part is designed to return a success/failure indication, and then the trailing , 0
throws that away. … ? -1 : (…, 0)
部分旨在返回成功/失败指示,然后尾随, 0
将其丢弃。 It is “valid code” in that it does not violate a rule of the C standard, but there is clearly a design or coding error there.它是“有效代码”,因为它不违反 C 标准的规则,但显然存在设计或编码错误。
The extra , 0
was introduced in commit dd55e00e17d454f54b905fdcf6718ba0c1ed94b0, with the log message “Fixed vec_insert() and vec_push() for structs”.额外的
, 0
在提交 dd55e00e17d454f54b905fdcf6718ba0c1ed94b0 中引入,日志消息“已修复 vec_insert() 和 vec_push() for structs”。 It and the same change to vec_insert
are the only changes in that commit:它和对
vec_insert
的相同更改是该提交中唯一的更改:
--- a/src/vec.h
+++ b/src/vec.h
@@ -33,7 +33,7 @@
#define vec_push(v, val)\
( vec_expand_(vec_unpack_(v)) ? -1 :\
- ((v)->data[(v)->length++] = (val)), 0 )
+ ((v)->data[(v)->length++] = (val), 0), 0 )
#define vec_pop(v)\
@@ -52,7 +52,7 @@
#define vec_insert(v, idx, val)\
( vec_insert_(vec_unpack_(v), idx) ? -1 :\
- ((v)->data[idx] = (val)), (v)->length++, 0 )
+ ((v)->data[idx] = (val), 0), (v)->length++, 0 )
I do not see how those changes comport with the log message.我看不到这些更改与日志消息的对应关系。 I think it is a mistake.
我认为这是一个错误。 Note that the pre-commit code was wrong;
请注意,预提交代码是错误的; this code:
这段代码:
( vec_expand_(vec_unpack_(v)) ? -1 :\
((v)->data[(v)->length++] = (val)), 0 )
has the pattern (Test ? -1 : (Operation), 0)
.有模式
(Test ? -1 : (Operation), 0)
。 That always produces 0, since the comma operator is the lowest precedence.这总是产生 0,因为逗号运算符的优先级最低。 The commit changes it to
(Test ? -1 : (Operation, 0), 0)
, which has equivalent behavior—on the :
side, the operation is performed, but then the result is discarded, 0 is evaluated and discarded, and then there is another 0. If the commit had changed it to (Test ? -1 : (Operation, 0))
, that would make sense.提交将其更改为
(Test ? -1 : (Operation, 0), 0)
,这具有等效的行为——在:
一侧,执行操作,但随后丢弃结果,评估并丢弃 0,然后还有另一个 0。如果提交已将其更改为(Test ? -1 : (Operation, 0))
,那将是有道理的。 The commit would be fixing an error, in that the macro previously always produced 0 but would now produce 0 or −1 according to whether the operation was successful or not.提交将修复错误,因为宏以前总是产生 0,但现在根据操作是否成功会产生 0 或 -1。
None of the tests in the repository check the “return” value of vec_push
.存储库中的所有测试都不会检查
vec_push
的“返回”值。
It should be safe to remove the , 0
.删除
, 0
应该是安全的。 The resulting expansion will continue to evaluate to zero in normal situations.在正常情况下,由此产生的扩展将继续评估为零。 It will change to evaluate to −1 if the push fails (because memory allocation fails), but that would seem to be desirable.
如果推送失败(因为内存分配失败),它将更改为 -1,但这似乎是可取的。 Good code should be:
好的代码应该是:
#define vec_push(v, val)\
( vec_expand_(vec_unpack_(v)) ? -1 :\
((v)->data[(v)->length++] = (val), 0))
Also make the same change to vec_insert
and look for any similar code that needs to be fixed.还要对
vec_insert
进行相同的更改,并查找需要修复的任何类似代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.