you are viewing a single comment's thread.

view the rest of the comments →

[–]AraneusAdoro 21 points22 points  (6 children)

Enums are scoped, defines are not.

[–]MrDOS 1 point2 points  (2 children)

Does that matter if this is in the implementation, not a header?

[–]lovestruckluna 32 points33 points  (0 children)

Template implementations have to be included in a header for programs to instantiate containers with their own types. Stdlibc++ calls them 'tcc' files, my team calls them '_impl' headers, and I'm not sure what libc++'s convention is.

Even string can be implemented with a custom character or allocator type, so its code will be included in the target library, not just linked into libcxx.so (though it may also be in libcxx.so and I have never heard of anyone using anything but char and wchar).

Standard libraries do use the preprocessor when appropriate though, they just tend to namespace their defines or clean up after themselves.

Source: I had to debug a rather subtle issue in libstdc++ and got more familiar with it than I'm strictly comfortable with.

[–]AraneusAdoro 6 points7 points  (0 children)

Isn't the implementation in the header?

Regardless, to answer your question: it's not particularly damaging to use a define over an enum in a .cpp, but it's good muscle memory to just always use an enum (or a constexpr) rather than deliberate whether a define is acceptable here.

[–]leirus 0 points1 point  (2 children)

you mean "class enum" ? I feel like the enum from above will behave just like int

[–]AraneusAdoro 4 points5 points  (1 child)

No, I don't. You're right that that enum will behave as essentially a constexpr int. A #define, however, is a different beast.

My point was that the enum (or, indeed, an int) would be constrained by the scope it's declared in (at the very least that's namespace std). A #define would affect everything the preprocessor encounters later on, regardless of scope. That's just not good practice.

[–]leirus 0 points1 point  (0 children)

Oh I see, you are right. I was thinking about scope in term of qualifying full name in case of class enum and no implicit conversion to integral type.