为什么这个通用表达式会漏掉?

时间:2016-02-26 09:27:48

标签: c generic-programming c11

最近我回答了一些question,询问如何实现结构成员的复杂const正确性。这样做我使用了Generic表达式并遇到了奇怪的行为。以下是示例代码:

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和Clang上,通用表达式选择a的类型必须为struct A时没有任何意义的情况。当我评论最后两个案例虽然它工作正常。为什么 - 这是一些错误?

clang上的确切错误消息是:

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.

1 个答案:

答案 0 :(得分:1)

虽然未评估不匹配的选择,但编译器仍在编译代码。因此,不允许在指向结构的指针上使用.运算符。

如果输入表达式始终是变量,则可以获取表示结构实例的变量的地址。

#define AptrChar_m(a) _Generic(a, struct A *: a->m_ptrChar, \
                                  const struct A *: a->m_cptrChar)
#define ptrChar_m(a) AptrChar_m(_Generic(a, struct A *: a, \
                                            const struct A *: a,  \
                                            struct A: &a,        \
                                            const struct A: &a))