[英]How to solve 'right-hand operand has no effect' error?
我目前正在使用 Eclipse CDT、 ESP-IDF框架和ESP32 Wrover DevKit 。
我正在尋找C
向量實現並在 GitHub 上偶然發現了rxi/vec 。 我將vec.h
和vec.c
復制到我的項目中並嘗試編譯。 我收到以下錯誤:
Error: right-hand operand of comma expression has no effect[-Werror=unused-value]
...在vec.h
中的這行代碼:
#define vec_push(v, val)\
( vec_expand_(vec_unpack_(v)) ? -1 :\
((v)->data[(v)->length++] = (val), 0), 0 )
任何想法如何解決這個問題或解決? 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);
}
也許這個問題與這個問題無關,但由於 Eric 提到了最后一次提交的日志消息“Fixed vec_insert() and vec_push() for structs”插入了導致這里討論的問題的'fishy' ,0
,它可能仍然是相關的。
執行“更新 B”部分中的代碼時,我得到以下輸出:
TOC [toc1]:
- item[0]=cat1
但我希望以下內容:
TOC [toc1]:
- item[0]=cat1
- element[0]=elem1
- element[1]=elem2
已解決: 為什么內部向量沒有打印出來?
編譯器警告你是正確的。 那里有些可疑。 … ? -1 : (…, 0)
… ? -1 : (…, 0)
部分旨在返回成功/失敗指示,然后尾隨, 0
將其丟棄。 它是“有效代碼”,因為它不違反 C 標准的規則,但顯然存在設計或編碼錯誤。
額外的, 0
在提交 dd55e00e17d454f54b905fdcf6718ba0c1ed94b0 中引入,日志消息“已修復 vec_insert() 和 vec_push() for structs”。 它和對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 )
我看不到這些更改與日志消息的對應關系。 我認為這是一個錯誤。 請注意,預提交代碼是錯誤的; 這段代碼:
( vec_expand_(vec_unpack_(v)) ? -1 :\
((v)->data[(v)->length++] = (val)), 0 )
有模式(Test ? -1 : (Operation), 0)
。 這總是產生 0,因為逗號運算符的優先級最低。 提交將其更改為(Test ? -1 : (Operation, 0), 0)
,這具有等效的行為——在:
一側,執行操作,但隨后丟棄結果,評估並丟棄 0,然后還有另一個 0。如果提交已將其更改為(Test ? -1 : (Operation, 0))
,那將是有道理的。 提交將修復錯誤,因為宏以前總是產生 0,但現在根據操作是否成功會產生 0 或 -1。
存儲庫中的所有測試都不會檢查vec_push
的“返回”值。
刪除, 0
應該是安全的。 在正常情況下,由此產生的擴展將繼續評估為零。 如果推送失敗(因為內存分配失敗),它將更改為 -1,但這似乎是可取的。 好的代碼應該是:
#define vec_push(v, val)\
( vec_expand_(vec_unpack_(v)) ? -1 :\
((v)->data[(v)->length++] = (val), 0))
還要對vec_insert
進行相同的更改,並查找需要修復的任何類似代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.