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
f5c4e9f3
Unverified
Commit
f5c4e9f3
authored
Jan 23, 2018
by
Niels Lohmann
Committed by
GitHub
Jan 23, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #919 from theodelrieu/fix/sfinae_for_incomplete_types
fix sfinae on basic_json UDT constructor
parents
7eabb6ba
dcee778c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
83 additions
and
20 deletions
+83
-20
develop/detail/meta.hpp
develop/detail/meta.hpp
+25
-1
develop/json.hpp
develop/json.hpp
+7
-9
src/json.hpp
src/json.hpp
+32
-10
test/src/unit-udt.cpp
test/src/unit-udt.cpp
+19
-0
No files found.
develop/detail/meta.hpp
View file @
f5c4e9f3
...
...
@@ -96,6 +96,14 @@ template<> struct priority_tag<0> {};
// has_/is_ functions //
////////////////////////
// source: https://stackoverflow.com/a/37193089/4116453
template
<
typename
T
,
typename
=
void
>
struct
is_complete_type
:
std
::
false_type
{};
template
<
typename
T
>
struct
is_complete_type
<
T
,
decltype
(
void
(
sizeof
(
T
)))
>
:
std
::
true_type
{};
NLOHMANN_JSON_HAS_HELPER
(
mapped_type
);
NLOHMANN_JSON_HAS_HELPER
(
key_type
);
NLOHMANN_JSON_HAS_HELPER
(
value_type
);
...
...
@@ -171,7 +179,6 @@ struct is_compatible_integer_type
RealIntegerType
,
CompatibleNumberIntegerType
>
::
value
;
};
// trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
template
<
typename
BasicJsonType
,
typename
T
>
struct
has_from_json
...
...
@@ -221,6 +228,23 @@ struct has_to_json
std
::
declval
<
typename
BasicJsonType
::
template
json_serializer
<
T
,
void
>
>
()))
>::
value
;
};
template
<
typename
BasicJsonType
,
typename
CompatibleCompleteType
>
struct
is_compatible_complete_type
{
static
constexpr
bool
value
=
not
std
::
is_base_of
<
std
::
istream
,
CompatibleCompleteType
>::
value
and
not
std
::
is_same
<
BasicJsonType
,
CompatibleCompleteType
>::
value
and
not
is_basic_json_nested_type
<
BasicJsonType
,
CompatibleCompleteType
>::
value
and
has_to_json
<
BasicJsonType
,
CompatibleCompleteType
>::
value
;
};
template
<
typename
BasicJsonType
,
typename
CompatibleType
>
struct
is_compatible_type
:
conjunction
<
is_complete_type
<
CompatibleType
>
,
is_compatible_complete_type
<
BasicJsonType
,
CompatibleType
>>
{
};
// taken from ranges-v3
template
<
typename
T
>
struct
static_const
...
...
develop/json.hpp
View file @
f5c4e9f3
...
...
@@ -1551,15 +1551,13 @@ class basic_json
@since version 2.1.0
*/
template
<
typename
CompatibleType
,
typename
U
=
detail
::
uncvref_t
<
CompatibleType
>,
detail
::
enable_if_t
<
not
std
::
is_base_of
<
std
::
istream
,
U
>::
value
and
not
std
::
is_same
<
U
,
basic_json_t
>::
value
and
not
detail
::
is_basic_json_nested_type
<
basic_json_t
,
U
>::
value
and
detail
::
has_to_json
<
basic_json
,
U
>::
value
,
int
>
=
0
>
basic_json
(
CompatibleType
&&
val
)
noexcept
(
noexcept
(
JSONSerializer
<
U
>::
to_json
(
std
::
declval
<
basic_json_t
&>
(),
std
::
forward
<
CompatibleType
>
(
val
))))
template
<
typename
CompatibleType
,
typename
U
=
detail
::
uncvref_t
<
CompatibleType
>,
detail
::
enable_if_t
<
detail
::
is_compatible_type
<
basic_json_t
,
U
>::
value
,
int
>
=
0
>
basic_json
(
CompatibleType
&&
val
)
noexcept
(
noexcept
(
JSONSerializer
<
U
>::
to_json
(
std
::
declval
<
basic_json_t
&>
(),
std
::
forward
<
CompatibleType
>
(
val
))))
{
JSONSerializer
<
U
>::
to_json
(
*
this
,
std
::
forward
<
CompatibleType
>
(
val
));
assert_invariant
();
...
...
src/json.hpp
View file @
f5c4e9f3
...
...
@@ -310,6 +310,14 @@ template<> struct priority_tag<0> {};
// has_/is_ functions //
////////////////////////
// source: https://stackoverflow.com/a/37193089/4116453
template
<
typename
T
,
typename
=
void
>
struct
is_complete_type
:
std
::
false_type
{};
template
<
typename
T
>
struct
is_complete_type
<
T
,
decltype
(
void
(
sizeof
(
T
)))
>
:
std
::
true_type
{};
NLOHMANN_JSON_HAS_HELPER
(
mapped_type
);
NLOHMANN_JSON_HAS_HELPER
(
key_type
);
NLOHMANN_JSON_HAS_HELPER
(
value_type
);
...
...
@@ -385,7 +393,6 @@ struct is_compatible_integer_type
RealIntegerType
,
CompatibleNumberIntegerType
>
::
value
;
};
// trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
template
<
typename
BasicJsonType
,
typename
T
>
struct
has_from_json
...
...
@@ -435,6 +442,23 @@ struct has_to_json
std
::
declval
<
typename
BasicJsonType
::
template
json_serializer
<
T
,
void
>
>
()))
>::
value
;
};
template
<
typename
BasicJsonType
,
typename
CompatibleCompleteType
>
struct
is_compatible_complete_type
{
static
constexpr
bool
value
=
not
std
::
is_base_of
<
std
::
istream
,
CompatibleCompleteType
>::
value
and
not
std
::
is_same
<
BasicJsonType
,
CompatibleCompleteType
>::
value
and
not
is_basic_json_nested_type
<
BasicJsonType
,
CompatibleCompleteType
>::
value
and
has_to_json
<
BasicJsonType
,
CompatibleCompleteType
>::
value
;
};
template
<
typename
BasicJsonType
,
typename
CompatibleType
>
struct
is_compatible_type
:
conjunction
<
is_complete_type
<
CompatibleType
>
,
is_compatible_complete_type
<
BasicJsonType
,
CompatibleType
>>
{
};
// taken from ranges-v3
template
<
typename
T
>
struct
static_const
...
...
@@ -10379,15 +10403,13 @@ class basic_json
@since version 2.1.0
*/
template
<
typename
CompatibleType
,
typename
U
=
detail
::
uncvref_t
<
CompatibleType
>,
detail
::
enable_if_t
<
not
std
::
is_base_of
<
std
::
istream
,
U
>::
value
and
not
std
::
is_same
<
U
,
basic_json_t
>::
value
and
not
detail
::
is_basic_json_nested_type
<
basic_json_t
,
U
>::
value
and
detail
::
has_to_json
<
basic_json
,
U
>::
value
,
int
>
=
0
>
basic_json
(
CompatibleType
&&
val
)
noexcept
(
noexcept
(
JSONSerializer
<
U
>::
to_json
(
std
::
declval
<
basic_json_t
&>
(),
std
::
forward
<
CompatibleType
>
(
val
))))
template
<
typename
CompatibleType
,
typename
U
=
detail
::
uncvref_t
<
CompatibleType
>,
detail
::
enable_if_t
<
detail
::
is_compatible_type
<
basic_json_t
,
U
>::
value
,
int
>
=
0
>
basic_json
(
CompatibleType
&&
val
)
noexcept
(
noexcept
(
JSONSerializer
<
U
>::
to_json
(
std
::
declval
<
basic_json_t
&>
(),
std
::
forward
<
CompatibleType
>
(
val
))))
{
JSONSerializer
<
U
>::
to_json
(
*
this
,
std
::
forward
<
CompatibleType
>
(
val
));
assert_invariant
();
...
...
test/src/unit-udt.cpp
View file @
f5c4e9f3
...
...
@@ -692,3 +692,22 @@ TEST_CASE("custom serializer that does adl by default", "[udt]")
CHECK
(
me
==
j
.
get
<
udt
::
person
>
());
CHECK
(
me
==
cj
.
get
<
udt
::
person
>
());
}
namespace
{
struct
incomplete
;
// std::is_constructible is broken on macOS' libc++
// use the cppreference implementation
template
<
typename
T
,
typename
=
void
>
struct
is_constructible_patched
:
std
::
false_type
{};
template
<
typename
T
>
struct
is_constructible_patched
<
T
,
decltype
(
void
(
json
(
std
::
declval
<
T
>
())))
>
:
std
::
true_type
{};
}
TEST_CASE
(
"an incomplete type does not trigger a compiler error in non-evaluated context"
,
"[udt]"
)
{
static_assert
(
not
is_constructible_patched
<
json
,
incomplete
>::
value
,
""
);
}
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