Improved customization of RAPIDJSON_NEW macro #811
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Reason for change
I am currently integrating RapidJSON into a large project with high performance requirements where custom allocation support is a must. Allocation is done via a custom
scalable_alloc()
function, so the syntax differs from thenew
expression (as does most any custom allocation function's syntax).So, the object initialization should be
new(scalable_alloc(sizeof(Type)) Type(Args...)
. But the macro's argumentx
supplies the expressionType(Args...)
. Thankfully, all uses ofRAPIDJSON_NEW
in the library have the formRAPIDJSON_NEW(AllocatorType())
. This allowed me to hack together a custom type trait that extracts theType
part ofType()
expressions with empty parentheses (works because the expressionType()
is also a function signature):Then the macro reads something like:
new(scalable_alloc(sizeof(return_type_t<x>)) x
which works if default initialization is used but breaks in any other case.Proposed solution
Change the
RAPIDJSON_NEW
macro signature toRAPIDJSON_NEW(type, ...)
and the default implementation tonew type(__VA_ARGS__)
as is done in this pull request.This will allow custom allocation functions to be used without type trait hacking consistently with any amount of constructor arguments, e.g.
new(scalable_alloc(sizeof(type))) type(__VA_ARGS__)
.Committed code tested on Windows 7 with Visual Studio 2012 (64 bit).