This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Why does gcc allow implicit conversion from const T* to T* (in C)?


A member at the site gamedev.net came up with the question why the
following only gives a warning, even with -Wall -Wextra -std=c99
-pedantic:



#if defined(__cplusplus)
struct foo_t {};
#else
typedef struct {} foo_t;
#endif

// Function of interest.
foo_t* bar(const foo_t* array, int index) {
??????? return &array[index];
}

int main() {return 0;}


(http://www.gamedev.net/community/forums/topic.asp?topic_id=533764)

Compiled as a C++ program, I get "error: invalid conversion from
`const foo_t*' to `foo_t*'", but as a C program, I fail to get an
error: "warning: return discards qualifiers from pointer target type".

I then looked up the standards at
http://www.open-std.org/JTC1/SC22/wg14/www/docs/n1124.pdf , and found
the following, seemingly interesting sections:


? "6.3.2.3 Pointers
7. A pointer to an object or incomplete type may be converted to a
pointer to a different object or incomplete type. If the resulting
pointer is not correctly aligned57) for the pointed-to type, the
behavior is undefined. Otherwise, when converted back again, the
result shall compare equal to the original pointer. [...]"

? "6.3.2.3 Pointers
2. For any qualifier q, a pointer to a non-q-qualified type may be
converted to a pointer to the q-qualified version of the type; the
values stored in the original and converted pointers shall compare
equal."


From 6.3.2.3/Item 7 I read that you can convert? a type T to a type U,
but where the type-id does not include how the object is qualified (in
our case const), i.e. item 7 does not say anything about stripping off
qualifiers.

>From Item 2 I read that you can add qualifiers, but may not strip them off.

Then I looked up the following paragraph:

? "6.5.16.1 Simple Assignment
1.? [...]? — both operands are pointers to qualified or unqualified
versions of compatible types, and the type pointed to?? by the left
has all the qualifiers of the type pointed to by the right;? [...]"

Which now explicitly states that you may not strip qualifiers, as it
says that the lhs in the assignment has at least all qualifiers of the
rhs. But then, 6.5.16.1/1 is about assignment, and I am unsure if a
return statement can be counted as a special form of assignment in
C-standard. But even if they are not, I have a hard time believing
that implicit conversions of the form const T* -> void* -> T* are
allowed in C.

Now my questions are:
0) Is gcc just liberal?
0.yes) Why?
0.no) Which would be the relevant section of the standard?


Sebastian Mach
{0}@gmail.com, {0}=phresnel
http://phresnel.org | http://picogen.org


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]