r/cprogramming • u/Itap88 • Apr 26 '26
Alternative to enums as function arguments?
Say I have a function argument, like a cardinal direction, that has no business being a number in the call, but could be a number internally. Now I could write an enum, but what if I need multiple values and don't want to pollute the namespace of the caller?
This is all hypothetical, for now at least.
1
Upvotes
2
u/bearheart Apr 26 '26
If you have so many possible values that you’re worried about polluting the namespace, you may consider using a lookup table or, for a larger set of possibilities, perhaps a small database like SQLite.
2
u/My124thRedditAccount Apr 26 '26
https://xyproblem.info/
It's hard to answer this without understanding why you think it's important that you don't "pollute" any namespaces. Like another comment pointed out, it doesn't really make sense. The symbol needs to be accessible in the caller's namespace, otherwise the caller can't pass it to the function. It can't be private and public at the same time.
I would just define an enum like
DIRECTION_NORTHand so on.It's also not entirely clear what "need multiple values" means.
4
u/duane11583 Apr 26 '26
I use lowercase letters like ‘n’ or ‘e’ or l or r for left right It’s easy that way
1
u/WittyStick Apr 26 '26 edited Apr 26 '26
You can use an enum with a stronger type than an
int, by wrapping theintin a struct.We'll use a GCC extension to improve this, but make it optional so that other compilers can compile the code just fine.
Then use the
designated_initon a struct containing a single integer field.It becomes awkward for someone to manually create a
Directionbesides the provided ones as they have to use the designated initializer_direction_as_num(if using GCC).We can optionally prevent the user from creating a new
Directionwith GCC by adding the lineThat strongly "encapsulates" our typed enum so that there's no trivial way to add new enumeration values.
However it also means we can't use regular operators like
==on theDirectiontype, as they're not numbers, but structs. We'd need to define a functiondirection_equalsbefore we poison the struct field.If we define all such functions before the line where we
poisonthe field - the user of this API basically has a "black box"Directiontype and some named instances of it, but has no standard way to access the internal number.2
u/Gabrunken Apr 26 '26
So i don’t see the problem with enums and how it does relate to the problem, then, if its an additional argument or 2, and they are required, they simply are, you can’t avoid that.
You can try to see if the function called can figure out the arguments by itself, with some math maybe.
You could try an array if that works for you.
And the final thing is: struct If you feel like the arguments could be grouped in a struct like format, then do it.
2
u/weregod Apr 26 '26
One option is to prefix enum with library name: MY_LIBRARY_EAST. Another option is to move arguments to function name:
If you expect extra options later you need to make struct with other arguments or make flags variable my_func(MY_FUNC_DIR_EAST | MY_FUNC_FLAG1).
Is your API/ABI experimental and can breaks or you need stable API/ABI? What other argumenys you might need and how many bits you need for extra arguments?
2
u/Longjumping-Emu3095 Apr 27 '26
Bit flags are your go to there
1
u/zhivago Apr 27 '26
Well, you can always use pointers to incomplete types as pure identities, l guess.
1
u/TheTrueXenose Apr 27 '26
Struct with bit fields or you can just type out all the cases.
1
u/un_virus_SDF Apr 30 '26
You can also have a macro for name mangling.
For exemple, you can have
Or
However I would not recommend the second option.
The first solution is the close st you can get of c++ enum class
7
u/[deleted] Apr 26 '26
[removed] — view removed comment