Skip to content

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

Open
duckie wants to merge 1 commit intovivkin:masterfrom
duckie:cpp11_macro_to_constexpr
Open

C++11 good practices: constexpr instead of #define#17
duckie wants to merge 1 commit intovivkin:masterfrom
duckie:cpp11_macro_to_constexpr

Conversation

@duckie
Copy link
Copy Markdown

@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.

Copy link
Copy Markdown
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
Copy Markdown
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