schar and uchar macros in usbdrv.h
Posted: Thu Nov 14, 2013 10:34 pm
Perhaps a minor issue, but I'd rather mention it than keep quiet. I am thrilled with V-USB so small things like this stand out more. I am also aware that there's a feedback channel, but I'd rather discuss this with other users rather than merely have it be a request.
The schar and uchar macros can conflict with user code that uses those names for other things in a local scope. e.g.
If they were typedefs, this would be fine:
I realize that the macro might serve a second purpose: to avoid a duplicate typedef in case someone else has also defined schar:
This serves the same purpose but doesn't conflict with user code using the name locally:
Ideally a library's header file only defines things needed by the user, and doesn't introduce things like schar and uchar into the global space at all, especially as macros, them being lowercase. Things for the implementer's convenience ideally have the library's prefix in their names.
The schar and uchar macros can conflict with user code that uses those names for other things in a local scope. e.g.
Code: Select all
#include "usbdrv.h"
void f( void )
{
int schar = 123; // error: two or more data types in declaration specifiers
}
If they were typedefs, this would be fine:
Code: Select all
typedef signed char schar;
void f( void )
{
int schar = 123; // no error
}
I realize that the macro might serve a second purpose: to avoid a duplicate typedef in case someone else has also defined schar:
Code: Select all
// other.h
#define schar signed char
// usbdrv.h
#ifndef schar // no problem, already defined so this is skipped
#define schar signed char
#endif
This serves the same purpose but doesn't conflict with user code using the name locally:
Code: Select all
#ifndef schar
#define schar schar
typedef signed char schar;
#endif
void f( void )
{
int schar = 123; // no error
}
Ideally a library's header file only defines things needed by the user, and doesn't introduce things like schar and uchar into the global space at all, especially as macros, them being lowercase. Things for the implementer's convenience ideally have the library's prefix in their names.