3
最近我回答了一些question問如何實現一個結構成員的複雜const正確性。這樣做我使用通用表達式並遇到奇怪的行爲。下面是示例代碼:爲什麼這個通用表達式會被竊聽?
struct A
{
union {
char *m_ptrChar;
const char *m_cptrChar;
} ;
};
#define ptrChar_m(a) _Generic(a, struct A *: a->m_ptrChar, \
const struct A *: a->m_cptrChar, \
struct A: a.m_ptrChar, \
const struct A: a.m_cptrChar)
void f(const struct A *ptrA)
{
ptrChar_m(ptrA) = 'A'; // NOT DESIRED!!
}
在GCC和鏘通用表達選擇時的a
類型必須是struct A
不使任何意義的情況。當我評論最後兩個案例,雖然它工作正常。這是爲什麼 - 它是否有一些錯誤?
上鐺確切的錯誤信息是:
test.c:17:5: error: member reference type 'const struct A *' is a pointer; did you mean to use '->'?
ptrChar_m(ptrA) = 'A'; // NOT DESIRED!!
^
test.c:12:45: note: expanded from macro 'ptrChar_m'
struct A: a.m_ptrCHar, \
^
test.c:17:5: error: member reference type 'const struct A *' is a pointer; did you mean to use '->'?
ptrChar_m(ptrA) = 'A'; // NOT DESIRED!!
^
test.c:13:51: note: expanded from macro 'ptrChar_m'
const struct A: a.m_cptrChar)
^
test.c:17:21: error: cannot assign to variable 'ptrA' with const-qualified type 'const struct A *'
ptrChar_m(ptrA) = 'A'; // NOT DESIRED!!
^
test.c:15:24: note: variable 'ptrA' declared const here
void f(const struct A *ptrA)
^
test.c:23:18: error: invalid application of 'sizeof' to a function type [-Werror,-Wpointer-arith]
return sizeof(main);
4 errors generated.
這是一個很好的問題,但包括編譯器輸出作爲截圖並不是那麼好。難道你不能將文本複製粘貼到問題中嗎? – unwind
[THIS](http://stackoverflow.com/a/24746034/3436922)可能會回答你的問題 – LPs