简体   繁体   中英

Why do C standard libraries neglect const correctness?

Looking at most of the functions in the C standard libraries, there appears to be a lack of const, where specifying so, would normally be preferred.

For example:
ctype.h/c

extern int isupper(int __c) __ATTR_CONST__;
extern int islower(int __c) __ATTR_CONST__;
extern int isdigit(int __c) __ATTR_CONST__;  

Why aren't these instead:

extern int isupper(const int __c) __ATTR_CONST__;
extern int islower(const int __c) __ATTR_CONST__;
extern int isdigit(const int __c) __ATTR_CONST__; 

They only observe the parameter after all:

int isupper(int c) {
  return _pctype[c] & _UPPER;
}

int islower(int c) {
  return _pctype[c] & _LOWER;
}

int isdigit(int c) {
  return _pctype[c] & _DIGIT;
}

Or let's take a function in string.c :

void *memcpy(void *dest, const void *src, size_t count)
{
         char *tmp = dest;
         const char *s = src;

         while (count--)
                 *tmp++ = *s++;
         return dest;
}

Why not:

void *memcpy(void *const dest, const void *const src, size_t count);

Is const excluded in these places for a reason?
Would it be wrong to include const in the ways I've shown?

I assume the functions have remained this way for historical reasons,
but I figured I should ask in case I've missed something.

Those are const-correct signatures.

You almost never write const before pass-by-value arguments. The function gets its own copy so there's no danger there.

void *memcpy(void *dest, const void *src, size_t count)

is const correct too. Only the second pointer promises not to change what it points to. The destination pointer, on the other hand, is all about changing what it points to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM