View previous topic :: View next topic |
Author |
Message |
wswartzendruber Veteran
Joined: 23 Mar 2004 Posts: 1261 Location: Idaho, USA
|
Posted: Sat Nov 12, 2011 5:08 am Post subject: Casing of Constants in the C1X Standard |
|
|
I'm looking at the current draft of the C1X standard. Here's an excerpt from the threading section:
Quote: | The thrd_create function returns thrd_success on success, or thrd_nomem if no memory could be allocated for the thread requested, or thrd_error if the request could not be honored. |
Why are these not uppercase? |
|
Back to top |
|
|
username234 Guru
Joined: 09 May 2007 Posts: 332
|
Posted: Sat Nov 12, 2011 2:05 pm Post subject: |
|
|
The obvious answer is that thrd_success, thrd_nomem, and thrd_error are enum constants instead of macros, so to maintain consistency in the standard they shouldn't be capitalized. Now, why they're enum constants as opposed to macros, I can't say. Hopefully, they'll post a C1X Standard rationale that explains it. _________________ Creating usernames when you're
in a creative slump is a bad idea
because if you are when you do
then you end up with uninspiring
alphanumeric cocktails like the
one directly above. |
|
Back to top |
|
|
wswartzendruber Veteran
Joined: 23 Mar 2004 Posts: 1261 Location: Idaho, USA
|
Posted: Sat Nov 12, 2011 6:37 pm Post subject: |
|
|
I found this, but no explanation as to why. It seems that there are about two macros (uppercase) in the threading portion of C1X and about twenty enums. |
|
Back to top |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21633
|
Posted: Sat Nov 12, 2011 7:11 pm Post subject: |
|
|
I cannot speak for why they chose enums in this case, but the most common argument that I see in support of using enums is that macros are expanded by the preprocessor, which leads to less readable error messages if you misuse them, and that macros require extra work to make the values type safe. You usually want an error if you compare a foo_result to a bar_success, since foo and bar may have different numeric values for what constitutes "success." Using simple macros, those might be defined as integers, and the compiler would silently allow you to make a mistake. If you use an enum, you can do: Code: | enum class foo_result {
success,
fail1,
fail2,
};
enum class bar_result {
fail1,
fail2,
success,
}; |
|
|
Back to top |
|
|
username234 Guru
Joined: 09 May 2007 Posts: 332
|
Posted: Sat Nov 12, 2011 7:46 pm Post subject: |
|
|
That does remind me that you can do something like Code: | #include <stdlib.h>
#ifdef NULL
#undef NULL
#endif
#define NULL (void *)27 | However, you can't do anything similar with enums. The one problem I have with this part of the C1X standard is that the functions have an integer return type. I would prefer to see something like Code: | enum thrd_return_cnsts {
thrd_success,
thrd_nomem,
thrd_error };
enum thrd_return_cnsts thread_create(thrd_t *, thrd_start_t, void *); | But I don't really get a say in these things. _________________ Creating usernames when you're
in a creative slump is a bad idea
because if you are when you do
then you end up with uninspiring
alphanumeric cocktails like the
one directly above. |
|
Back to top |
|
|
wswartzendruber Veteran
Joined: 23 Mar 2004 Posts: 1261 Location: Idaho, USA
|
Posted: Sun Nov 13, 2011 7:00 am Post subject: |
|
|
Perhaps thread.h functions return different values depending on the underlying API (pthreads, etc...)? |
|
Back to top |
|
|
|