Fix compilation of input_adapter(container) in edge cases

This fixes a compilation issue with the library if trying to use containers that
don't have non-member `begin()` and `end()` functions via ADL.

This patch extends the `using std::begin` and `using std::end` declarations to
also cover the return type deduction of the input_adapter() template
specialization for containers. The previous implementation only enabled the
detection of `std::begin()` and `std::end()` in the function body, making the
specialization unusable for container types that only have member `begin()` and
`end()` functions.

It is not typical to have `using` declarations in the namespace scope in a
header file. But a C++11 implementation can't rely on fully automatic return
type deduction, and needs to rely on ADL enabled helper templates. To prevent
the using declarations leaking, they are enclosed in another nested namespace.
This commit is contained in:
Jaakko Moisio
2020-12-28 22:20:37 +01:00
parent dfedefb993
commit 467f622c65
3 changed files with 84 additions and 17 deletions

View File

@@ -63,7 +63,7 @@ const char* end(const MyContainer& c)
return c.data + strlen(c.data);
}
TEST_CASE("Custom container")
TEST_CASE("Custom container non-member begin/end")
{
MyContainer data{"[1,2,3,4]"};
@@ -75,6 +75,31 @@ TEST_CASE("Custom container")
}
TEST_CASE("Custom container member begin/end")
{
struct MyContainer2
{
const char* data;
const char* begin() const
{
return data;
}
const char* end() const
{
return data + strlen(data);
}
};
MyContainer2 data{"[1,2,3,4]"};
json as_json = json::parse(data);
CHECK(as_json.at(0) == 1);
CHECK(as_json.at(1) == 2);
CHECK(as_json.at(2) == 3);
CHECK(as_json.at(3) == 4);
}
TEST_CASE("Custom iterator")
{
const char* raw_data = "[1,2,3,4]";