Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Casing of Constants in the C1X Standard
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
wswartzendruber
Veteran
Veteran


Joined: 23 Mar 2004
Posts: 1261
Location: Idaho, USA

PostPosted: Sat Nov 12, 2011 5:08 am    Post subject: Casing of Constants in the C1X Standard Reply with quote

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
View user's profile Send private message
username234
Guru
Guru


Joined: 09 May 2007
Posts: 332

PostPosted: Sat Nov 12, 2011 2:05 pm    Post subject: Reply with quote

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
View user's profile Send private message
wswartzendruber
Veteran
Veteran


Joined: 23 Mar 2004
Posts: 1261
Location: Idaho, USA

PostPosted: Sat Nov 12, 2011 6:37 pm    Post subject: Reply with quote

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
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21624

PostPosted: Sat Nov 12, 2011 7:11 pm    Post subject: Reply with quote

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
View user's profile Send private message
username234
Guru
Guru


Joined: 09 May 2007
Posts: 332

PostPosted: Sat Nov 12, 2011 7:46 pm    Post subject: Reply with quote

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
View user's profile Send private message
wswartzendruber
Veteran
Veteran


Joined: 23 Mar 2004
Posts: 1261
Location: Idaho, USA

PostPosted: Sun Nov 13, 2011 7:00 am    Post subject: Reply with quote

Perhaps thread.h functions return different values depending on the underlying API (pthreads, etc...)?
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum