Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GCC 48 #1257

Merged
merged 2 commits into from
Sep 28, 2018
Merged

GCC 48 #1257

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ To make changes, you need to edit the following files:

## Please don't

- The C++11 support varies between different **compilers** and versions. Please note the [list of supported compilers](https://github.com/nlohmann/json/blob/master/README.md#supported-compilers). Some compilers like GCC 4.8 (and earlier), Clang 3.3 (and earlier), or Microsoft Visual Studio 13.0 and earlier are known not to work due to missing or incomplete C++11 support. Please refrain from proposing changes that work around these compiler's limitations with `#ifdef`s or other means.
- The C++11 support varies between different **compilers** and versions. Please note the [list of supported compilers](https://github.com/nlohmann/json/blob/master/README.md#supported-compilers). Some compilers like GCC 4.7 (and earlier), Clang 3.3 (and earlier), or Microsoft Visual Studio 13.0 and earlier are known not to work due to missing or incomplete C++11 support. Please refrain from proposing changes that work around these compiler's limitations with `#ifdef`s or other means.
- Specifically, I am aware of compilation problems with **Microsoft Visual Studio** (there even is an [issue label](https://github.com/nlohmann/json/issues?utf8=✓&q=label%3A%22visual+studio%22+) for these kind of bugs). I understand that even in 2016, complete C++11 support isn't there yet. But please also understand that I do not want to drop features or uglify the code just to make Microsoft's sub-standard compiler happy. The past has shown that there are ways to express the functionality such that the code compiles with the most recent MSVC - unfortunately, this is not the main objective of the project.
- Please refrain from proposing changes that would **break [JSON](http://json.org) conformance**. If you propose a conformant extension of JSON to be supported by the library, please motivate this extension.
- We shall not extend the library to **support comments**. There is quite some [controversy](https://www.reddit.com/r/programming/comments/4v6chu/why_json_doesnt_support_comments_douglas_crockford/) around this topic, and there were quite some [issues](https://github.com/nlohmann/json/issues/376) on this. We believe that JSON is fine without comments.
Expand Down
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Read the [Contribution Guidelines](https://github.com/nlohmann/json/blob/develop

## Please don't

- The C++11 support varies between different **compilers** and versions. Please note the [list of supported compilers](https://github.com/nlohmann/json/blob/master/README.md#supported-compilers). Some compilers like GCC 4.8 (and earlier), Clang 3.3 (and earlier), or Microsoft Visual Studio 13.0 and earlier are known not to work due to missing or incomplete C++11 support. Please refrain from proposing changes that work around these compiler's limitations with `#ifdef`s or other means.
- The C++11 support varies between different **compilers** and versions. Please note the [list of supported compilers](https://github.com/nlohmann/json/blob/master/README.md#supported-compilers). Some compilers like GCC 4.7 (and earlier), Clang 3.3 (and earlier), or Microsoft Visual Studio 13.0 and earlier are known not to work due to missing or incomplete C++11 support. Please refrain from proposing changes that work around these compiler's limitations with `#ifdef`s or other means.
- Specifically, I am aware of compilation problems with **Microsoft Visual Studio** (there even is an [issue label](https://github.com/nlohmann/json/issues?utf8=✓&q=label%3A%22visual+studio%22+) for these kind of bugs). I understand that even in 2016, complete C++11 support isn't there yet. But please also understand that I do not want to drop features or uglify the code just to make Microsoft's sub-standard compiler happy. The past has shown that there are ways to express the functionality such that the code compiles with the most recent MSVC - unfortunately, this is not the main objective of the project.
- Please refrain from proposing changes that would **break [JSON](http://json.org) conformance**. If you propose a conformant extension of JSON to be supported by the library, please motivate this extension.
- Please do not open pull requests that address **multiple issues**.
10 changes: 9 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,15 @@ matrix:

- os: linux
compiler: gcc
env: COMPILER=g++-4.9
env: compiler=g++-4.8
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-4.8', 'ninja-build']

- os: linux
compiler: gcc
env: compiler=g++-4.9
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ json j_from_ubjson = json::from_ubjson(v_ubjson);

Though it's 2018 already, the support for C++11 is still a bit sparse. Currently, the following compilers are known to work:

- GCC 4.9 - 8.2 (and possibly later)
- GCC 4.8 - 8.2 (and possibly later)
- Clang 3.4 - 6.1 (and possibly later)
- Intel C++ Compiler 17.0.2 (and possibly later)
- Microsoft Visual C++ 2015 / Build Tools 14.0.25123.0 (and possibly later)
Expand All @@ -853,7 +853,7 @@ I would be happy to learn about other compilers/versions.

Please note:

- GCC 4.8 does not work because of two bugs ([55817](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55817) and [57824](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57824)) in the C++11 support. Note there is a [pull request](https://github.com/nlohmann/json/pull/212) to fix some of the issues.
- GCC 4.8 has a bug [57824](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57824)): multiline raw strings cannot be the arguments to macros. Don't use multiline raw strings directly in macros with this compiler.
- Android defaults to using very old compilers and C++ libraries. To fix this, add the following to your `Application.mk`. This will switch to the LLVM C++ library, the Clang compiler, and enable C++11 and other features disabled by default.

```
Expand All @@ -872,6 +872,7 @@ The following compilers are currently used in continuous integration at [Travis]

| Compiler | Operating System | Version String |
|-----------------|------------------------------|----------------|
| GCC 4.8.5 | Ubuntu 14.04.5 LTS | g++-4.8 (Ubuntu 4.8.5-2ubuntu1~14.04.2) 4.8.5 |
| GCC 4.9.4 | Ubuntu 14.04.1 LTS | g++-4.9 (Ubuntu 4.9.4-2ubuntu1~14.04.1) 4.9.4 |
| GCC 5.5.0 | Ubuntu 14.04.1 LTS | g++-5 (Ubuntu 5.5.0-12ubuntu1~14.04) 5.5.0 20171010 |
| GCC 6.4.0 | Ubuntu 14.04.1 LTS | g++-6 (Ubuntu 6.4.0-17ubuntu1~14.04) 6.4.0 20180424 |
Expand Down
2 changes: 1 addition & 1 deletion include/nlohmann/detail/macro_scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
#endif
#elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40900
#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
#error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
#endif
#endif
Expand Down
39 changes: 24 additions & 15 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4943,6 +4943,26 @@ class basic_json
return {it, res.second};
}

/// Helper for insertion of an iterator
/// @note: This uses std::distance to support GCC 4.8,
/// see https://github.com/nlohmann/json/pull/1257
template<typename... Args>
iterator insert_iterator(const_iterator pos, Args&& ... args)
{
iterator result(this);
assert(m_value.array != nullptr);

auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator);
m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
result.m_it.array_iterator = m_value.array->begin() + insert_pos;

// This could have been written as:
// result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
// but the return value of insert is missing in GCC 4.8, so it is written this way instead.

return result;
}

/*!
@brief inserts element

Expand Down Expand Up @@ -4977,9 +4997,7 @@ class basic_json
}

// insert to array and return iterator
iterator result(this);
result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, val);
return result;
return insert_iterator(pos, val);
}

JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
Expand Down Expand Up @@ -5030,9 +5048,7 @@ class basic_json
}

// insert to array and return iterator
iterator result(this);
result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
return result;
return insert_iterator(pos, cnt, val);
}

JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
Expand Down Expand Up @@ -5094,12 +5110,7 @@ class basic_json
}

// insert to array and return iterator
iterator result(this);
result.m_it.array_iterator = m_value.array->insert(
pos.m_it.array_iterator,
first.m_it.array_iterator,
last.m_it.array_iterator);
return result;
return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
}

/*!
Expand Down Expand Up @@ -5141,9 +5152,7 @@ class basic_json
}

// insert to array and return iterator
iterator result(this);
result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, ilist.begin(), ilist.end());
return result;
return insert_iterator(pos, ilist.begin(), ilist.end());
}

/*!
Expand Down
41 changes: 25 additions & 16 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ using json = basic_json<>;
#error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
#endif
#elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40900
#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
#error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
#endif
#endif
Expand Down Expand Up @@ -16016,6 +16016,26 @@ class basic_json
return {it, res.second};
}

/// Helper for insertion of an iterator
/// @note: This uses std::distance to support GCC 4.8,
/// see https://github.com/nlohmann/json/pull/1257
template<typename... Args>
iterator insert_iterator(const_iterator pos, Args&& ... args)
{
iterator result(this);
assert(m_value.array != nullptr);

auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator);
m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
result.m_it.array_iterator = m_value.array->begin() + insert_pos;

// This could have been written as:
// result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
// but the return value of insert is missing in GCC 4.8, so it is written this way instead.

return result;
}

/*!
@brief inserts element

Expand Down Expand Up @@ -16050,9 +16070,7 @@ class basic_json
}

// insert to array and return iterator
iterator result(this);
result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, val);
return result;
return insert_iterator(pos, val);
}

JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
Expand Down Expand Up @@ -16103,9 +16121,7 @@ class basic_json
}

// insert to array and return iterator
iterator result(this);
result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
return result;
return insert_iterator(pos, cnt, val);
}

JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
Expand Down Expand Up @@ -16167,12 +16183,7 @@ class basic_json
}

// insert to array and return iterator
iterator result(this);
result.m_it.array_iterator = m_value.array->insert(
pos.m_it.array_iterator,
first.m_it.array_iterator,
last.m_it.array_iterator);
return result;
return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
}

/*!
Expand Down Expand Up @@ -16214,9 +16225,7 @@ class basic_json
}

// insert to array and return iterator
iterator result(this);
result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, ilist.begin(), ilist.end());
return result;
return insert_iterator(pos, ilist.begin(), ilist.end());
}

/*!
Expand Down
5 changes: 3 additions & 2 deletions test/src/unit-json_patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ TEST_CASE("JSON patch")
// is not an error, because "a" exists, and "b" will be added to
// its value.
CHECK_NOTHROW(doc1.patch(patch));
CHECK(doc1.patch(patch) == R"(
auto doc1_ans = R"(
{
"a": {
"foo": 1,
Expand All @@ -72,7 +72,8 @@ TEST_CASE("JSON patch")
}
}
}
)"_json);
)"_json;
CHECK(doc1.patch(patch) == doc1_ans);

// It is an error in this document:
json doc2 = R"({ "q": { "bar": 2 } })"_json;
Expand Down
28 changes: 17 additions & 11 deletions test/src/unit-regression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1427,20 +1427,26 @@ TEST_CASE("regression tests")
}
})"_json;

CHECK_THROWS_AS(model.patch(R"([{"op": "move",
"from": "/one/two/three",
"path": "/a/b/c"}])"_json), json::out_of_range&);
CHECK_THROWS_WITH(model.patch(R"([{"op": "move",
"from": "/one/two/three",
"path": "/a/b/c"}])"_json),
auto p1 = R"([{"op": "move",
"from": "/one/two/three",
"path": "/a/b/c"}])"_json;
CHECK_THROWS_AS(model.patch(p1), json::out_of_range&);

auto p2 = R"([{"op": "move",
"from": "/one/two/three",
"path": "/a/b/c"}])"_json;
CHECK_THROWS_WITH(model.patch(p2),
"[json.exception.out_of_range.403] key 'a' not found");

CHECK_THROWS_AS(model.patch(R"([{"op": "copy",
"from": "/one/two/three",
"path": "/a/b/c"}])"_json), json::out_of_range&);
CHECK_THROWS_WITH(model.patch(R"([{"op": "copy",
auto p3 = R"([{"op": "copy",
"from": "/one/two/three",
"path": "/a/b/c"}])"_json;
CHECK_THROWS_AS(model.patch(p3), json::out_of_range&);

auto p4 = R"([{"op": "copy",
"from": "/one/two/three",
"path": "/a/b/c"}])"_json),
"path": "/a/b/c"}])"_json;
CHECK_THROWS_WITH(model.patch(p4),
"[json.exception.out_of_range.403] key 'a' not found");
}

Expand Down
11 changes: 7 additions & 4 deletions test/src/unit-testsuites.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ TEST_CASE("RFC 7159 examples")
SECTION("13 Examples")
{
{
CHECK_NOTHROW(json(R"(
auto json_contents = R"(
{
"Image": {
"Width": 800,
Expand All @@ -420,11 +420,13 @@ TEST_CASE("RFC 7159 examples")
"IDs": [116, 943, 234, 38793]
}
}
)"));
)";

CHECK_NOTHROW(json(json_contents));
}

{
CHECK_NOTHROW(json(R"(
auto json_contents = R"(
[
{
"precision": "zip",
Expand All @@ -446,7 +448,8 @@ TEST_CASE("RFC 7159 examples")
"Zip": "94085",
"Country": "US"
}
])"));
])";
CHECK_NOTHROW(json(json_contents));
}

CHECK(json::parse("\"Hello world!\"") == json("Hello world!"));
Expand Down