Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++11 good practices: constexpr instead of #define #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

duckie
Copy link

@duckie duckie commented Jul 20, 2015

No API breakage here, juste replacing some pre-processor macros by C++11 constant expressions.

If constexpr is a problem for people using vs2013 prior to the November CTP, then a simple const would do the job.

#define JSON_ZONE_SIZE 4096
#define JSON_STACK_SIZE 32
constexpr size_t JSON_ZONE_SIZE = 4096;
constexpr size_t JSON_STACK_SIZE = 32;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use int, as (a) that is the old type, and (b) you are casting to int anyway!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this is not correct.

The parser compares this value to signed integers and the allocator to unsigned integers.

When you use a macro, it is not an int ! It is a integer literal which behavior depends on how you use it in the code. This demonstrates why macros are bad practices.

If you use an int, you have to cast to unsigned int in the allocator. If you use an unsigned int, you would have to cast it to an int in the parser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants