Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
1be63431
Unverified
Commit
1be63431
authored
Jun 30, 2019
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
✨
make emplace_back return a reference #1609
parent
9a775bcb
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
8 deletions
+26
-8
include/nlohmann/json.hpp
include/nlohmann/json.hpp
+9
-2
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+9
-2
test/src/unit-modifiers.cpp
test/src/unit-modifiers.cpp
+8
-4
No files found.
include/nlohmann/json.hpp
View file @
1be63431
...
...
@@ -4952,6 +4952,8 @@ class basic_json
@param[in] args arguments to forward to a constructor of @ref basic_json
@tparam Args compatible types to create a @ref basic_json object
@return reference to the inserted element
@throw type_error.311 when called on a type other than JSON array or
null; example: `"cannot use emplace_back() with number"`
...
...
@@ -4961,10 +4963,10 @@ class basic_json
elements to a JSON array. Note how the `null` value was silently converted
to a JSON array.,emplace_back}
@since version 2.0.8
@since version 2.0.8
, returns reference since 3.7.0
*/
template
<
class
...
Args
>
void
emplace_back
(
Args
&&
...
args
)
reference
emplace_back
(
Args
&&
...
args
)
{
// emplace_back only works for null objects or arrays
if
(
JSON_UNLIKELY
(
not
(
is_null
()
or
is_array
())))
...
...
@@ -4981,7 +4983,12 @@ class basic_json
}
// add element to array (perfect forwarding)
#ifdef JSON_HAS_CPP_17
return
m_value
.
array
->
emplace_back
(
std
::
forward
<
Args
>
(
args
)...);
#else
m_value
.
array
->
emplace_back
(
std
::
forward
<
Args
>
(
args
)...);
return
m_value
.
array
->
back
();
#endif
}
/*!
...
...
single_include/nlohmann/json.hpp
View file @
1be63431
...
...
@@ -17763,6 +17763,8 @@ class basic_json
@param[in] args arguments to forward to a constructor of @ref basic_json
@tparam Args compatible types to create a @ref basic_json object
@return reference to the inserted element
@throw type_error.311 when called on a type other than JSON array or
null; example: `"cannot use emplace_back() with number"`
...
...
@@ -17772,10 +17774,10 @@ class basic_json
elements to a JSON array. Note how the `null` value was silently converted
to a JSON array.,emplace_back}
@since version 2.0.8
@since version 2.0.8
, returns reference since 3.7.0
*/
template
<
class
...
Args
>
void
emplace_back
(
Args
&&
...
args
)
reference
emplace_back
(
Args
&&
...
args
)
{
// emplace_back only works for null objects or arrays
if
(
JSON_UNLIKELY
(
not
(
is_null
()
or
is_array
())))
...
...
@@ -17792,7 +17794,12 @@ class basic_json
}
// add element to array (perfect forwarding)
#ifdef JSON_HAS_CPP_17
return
m_value
.
array
->
emplace_back
(
std
::
forward
<
Args
>
(
args
)...);
#else
m_value
.
array
->
emplace_back
(
std
::
forward
<
Args
>
(
args
)...);
return
m_value
.
array
->
back
();
#endif
}
/*!
...
...
test/src/unit-modifiers.cpp
View file @
1be63431
...
...
@@ -290,8 +290,10 @@ TEST_CASE("modifiers")
SECTION
(
"null"
)
{
json
j
;
j
.
emplace_back
(
1
);
j
.
emplace_back
(
2
);
auto
&
x1
=
j
.
emplace_back
(
1
);
CHECK
(
x1
==
1
);
auto
&
x2
=
j
.
emplace_back
(
2
);
CHECK
(
x2
==
2
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
CHECK
(
j
==
json
({
1
,
2
}));
}
...
...
@@ -299,7 +301,8 @@ TEST_CASE("modifiers")
SECTION
(
"array"
)
{
json
j
=
{
1
,
2
,
3
};
j
.
emplace_back
(
"Hello"
);
auto
&
x
=
j
.
emplace_back
(
"Hello"
);
CHECK
(
x
==
"Hello"
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
CHECK
(
j
==
json
({
1
,
2
,
3
,
"Hello"
}));
}
...
...
@@ -307,7 +310,8 @@ TEST_CASE("modifiers")
SECTION
(
"multiple values"
)
{
json
j
;
j
.
emplace_back
(
3
,
"foo"
);
auto
&
x
=
j
.
emplace_back
(
3
,
"foo"
);
CHECK
(
x
==
json
({
"foo"
,
"foo"
,
"foo"
}));
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
CHECK
(
j
==
json
({{
"foo"
,
"foo"
,
"foo"
}}));
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment