This is the mail archive of the gcc@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]

Re: idea for new function attribute ?


Ulrich Drepper <drepper@cygnus.com> writes:

> jamesa@demon.net (James A) writes:
> 
> >  Ok I've thought that it would be nice to be able to have an attribute 
> > for functions in gcc like the following...
> > 
> > char *my_strnchr(const char *, size_t, char) __attribute__ ((return_const(1)));
> 
> I think that unless you have the possibilities to overload the
> functions like in C++ this does not make that much sense.  E.g., one
> never could declare the real string function in <string.h> this way
> since then it would be possible anymore to use them on non-const
> strings.  And if you have a function which is only used on
> const-strings then it is much easier to declare the function right
> away in a way that it returns a const char *.

 I'm not sure if you know what I meant... The idea was that if the
first argument (or whatever number is passed as the parameter to the
attribute) was const, then the return value would be const as well
_but_ if the first argument was non-const then the return value would
be non-const which I think is the point you didn't get.
 The problem (if you view it that way) at the moment is that...

char *abcd = strchr("abcd", 'b');

 must has to work without any warnings, because strchr has to work
with both const and non-const strings.

> What situation do you have in mind?

 If everyone thought it was ok, then I would prefer to have it on
strchr, memchr and strstr etc.

 But even if that isn't acceptable then it would be useful for
functions with the same properties (gets an object which can be either 
const or non-const and then returns said object) in my code.

-- 
# James Antill -- jamesa@demon.net
:0:
* ^From: .*jamesa@demon.net
/dev/null


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