I am not able to understand why does the following code
fail to compile:
int main(void)
{
int x = _Generic("hello", char *: 1);
}
Compiling it with clang-3.2 gives the following error:
f.c:5:20: error: controlling expression type 'char [6]' not compatible
with any generic association type
int a = _Generic("hello", char *: 1);
n1570 $6.3.2.1 3rd point states:
"Except when it is the operand of the sizeof operator, the _Alignof
operator, or the
unary & operator, or is a string literal used to initialize an array,
an expression that has type ‘‘array of type’’ is converted to an
expression with type ‘‘pointer to type’’ that points to the initial
element of the array object and is not an lvalue. If the array object
has register storage class, the behavior is undefined."
Since this clause has no mention for _Generic, shouldn't the type of "hello"
be converted from char[6] to char * when it's used as a controlling expression ?
Or am I missing something ?
I am not able to understand why does the following code
fail to compile:
int main(void)
{
int x = _Generic("hello", char *: 1);
}
Compiling it with clang-3.2 gives the following error:
f.c:5:20: error: controlling expression type 'char [6]' not compatible
with any generic association type
int a = _Generic("hello", char *: 1);
n1570 $6.3.2.1 3rd point states:
"Except when it is the operand of the sizeof operator, the _Alignof
operator, or the
unary & operator, or is a string literal used to initialize an array,
an expression that has type ‘‘array of type’’ is converted to an
expression with type ‘‘pointer to type’’ that points to the initial
element of the array object and is not an lvalue. If the array object
has register storage class, the behavior is undefined."
Since this clause has no mention for _Generic, shouldn't the type of
"hello"
be converted from char[6] to char * when it's used as a controlling
expression ?
Or am I missing something ?
Yes, I think it should -- this looks like a bug. Please file a bug report!
I don't think so. It looks more like an overlook to me. CC'ed Clark (the
author of _Generic).
At a first glance, performing an array-to-pointer conversion make things
easier, but imagine something like this:
#define F(e) _Generic(e, char *: sizeof(e))
So what, sizeof a pointer is the array length? This leads very obscure
programs. Please don't. In unevalued context (stealing C++ term here),
no convert should be performed.
Here is an solution (no matter this is a bug in which side):
>> Since this clause has no mention for _Generic, shouldn't the type of
>> "hello"
>> be converted from char[6] to char * when it's used as a controlling
>> expression ?
>> Or am I missing something ?
>
>
> Yes, I think it should -- this looks like a bug. Please file a bug
report!
I don't think so. It looks more like an overlook to me. CC'ed Clark (the
author of _Generic).
"The controlling expression of a generic selection was very carefully
not added to the list of contexts in which lvalue conversion is not
done and type qualification is discarded", the minutes [from the C
committee meeting] say
At a first glance, performing an array-to-pointer conversion make things
I am not able to understand why does the following code
fail to compile:
int main(void)
{
int x = _Generic("hello", char *: 1);
}
Compiling it with clang-3.2 gives the following error:
f.c:5:20: error: controlling expression type 'char [6]' not compatible
with any generic association type
int a = _Generic("hello", char *: 1);
n1570 $6.3.2.1 3rd point states:
"Except when it is the operand of the sizeof operator, the _Alignof
operator, or the
unary & operator, or is a string literal used to initialize an array,
an expression that has type ‘‘array of type’’ is converted to an
expression with type ‘‘pointer to type’’ that points to the initial
element of the array object and is not an lvalue. If the array object
has register storage class, the behavior is undefined."
Since this clause has no mention for _Generic, shouldn't the type of
"hello"
be converted from char[6] to char * when it's used as a controlling
expression ?
Or am I missing something ?
Yes, I think it should -- this looks like a bug. Please file a bug report!