diff --git a/.travis.yml b/.travis.yml index bf749c1e33..b12d5540c4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -222,7 +222,7 @@ matrix: compiler: gcc env: - COMPILER=g++-9 - - CXXFLAGS=-std=c++2a + - CXX_STANDARD=17 addons: apt: sources: ['ubuntu-toolchain-r-test'] @@ -306,7 +306,7 @@ matrix: compiler: clang env: - COMPILER=clang++-7 - - CXXFLAGS=-std=c++1z + - CXX_STANDARD=17 addons: apt: sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-7'] @@ -333,6 +333,12 @@ script: # by default, use implicit conversions - if [[ "${IMPLICIT_CONVERSIONS}" == "" ]]; then export IMPLICIT_CONVERSIONS=ON; fi + # append CXX_STANDARD to CMAKE_OPTIONS if required + - CMAKE_OPTIONS+=${CXX_STANDARD:+ -DCMAKE_CXX_STANDARD=$CXX_STANDARD -DCMAKE_CXX_STANDARD_REQUIRED=ON} + + # force verbose build + - CMAKE_OPTIONS+=" -DCMAKE_VERBOSE_MAKEFILE=ON" + # compile and execute unit tests - mkdir -p build && cd build - cmake .. ${CMAKE_OPTIONS} -DJSON_MultipleHeaders=${MULTIPLE_HEADERS} -DJSON_ImplicitConversions=${IMPLICIT_CONVERSIONS} -DJSON_BuildTests=On -GNinja && cmake --build . --config Release diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index 77acf04c76..8dd34ac97c 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -31,6 +31,25 @@ #define JSON_HAS_CPP_14 #endif +namespace nlohmann { +namespace std_aliases { } +using namespace std_aliases; +} + +#if defined(JSON_HAS_CPP_17) + #if __has_include() + #include + namespace nlohmann::std_aliases { + using std::string_view; + } + #elif __has_include() + #include + namespace nlohmann::std_aliases { + using std::experimental::string_view; + } + #endif +#endif + // disable float-equal warnings on GCC/clang #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) #pragma GCC diagnostic push diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index cd56dafdc4..a82c1bed5c 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3230,7 +3230,7 @@ class basic_json !detail::is_basic_json::value && !std::is_same>::value #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914)) - && !std::is_same::value + && !std::is_same::value #endif && detail::is_detected::value , int >::type = 0 > @@ -5432,12 +5432,8 @@ class basic_json } // add element to array (perfect forwarding) -#ifdef JSON_HAS_CPP_17 - return m_value.array->emplace_back(std::forward(args)...); -#else m_value.array->emplace_back(std::forward(args)...); return m_value.array->back(); -#endif } /*! diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index e9dbc87dad..7965b4857b 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2048,6 +2048,25 @@ JSON_HEDLEY_DIAGNOSTIC_POP #define JSON_HAS_CPP_14 #endif +namespace nlohmann { +namespace std_aliases { } +using namespace std_aliases; +} + +#if defined(JSON_HAS_CPP_17) + #if __has_include() + #include + namespace nlohmann::std_aliases { + using std::string_view; + } + #elif __has_include() + #include + namespace nlohmann::std_aliases { + using std::experimental::string_view; + } + #endif +#endif + // disable float-equal warnings on GCC/clang #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) #pragma GCC diagnostic push @@ -19736,7 +19755,7 @@ class basic_json !detail::is_basic_json::value && !std::is_same>::value #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914)) - && !std::is_same::value + && !std::is_same::value #endif && detail::is_detected::value , int >::type = 0 > @@ -21938,12 +21957,8 @@ class basic_json } // add element to array (perfect forwarding) -#ifdef JSON_HAS_CPP_17 - return m_value.array->emplace_back(std::forward(args)...); -#else m_value.array->emplace_back(std::forward(args)...); return m_value.array->back(); -#endif } /*! diff --git a/test/src/unit-conversions.cpp b/test/src/unit-conversions.cpp index c71e230d96..091c4130ce 100644 --- a/test/src/unit-conversions.cpp +++ b/test/src/unit-conversions.cpp @@ -32,6 +32,7 @@ SOFTWARE. #define JSON_TESTS_PRIVATE #include using nlohmann::json; +using namespace nlohmann::std_aliases; #include #include @@ -48,10 +49,6 @@ using nlohmann::json; #define JSON_HAS_CPP_14 #endif -#if defined(JSON_HAS_CPP_17) - #include -#endif - TEST_CASE("value conversion") { SECTION("get an object (explicit)") @@ -465,7 +462,7 @@ TEST_CASE("value conversion") #if defined(JSON_HAS_CPP_17) SECTION("std::string_view") { - std::string_view s = j.get(); + string_view s = j.get(); CHECK(json(s) == j); } #endif @@ -514,27 +511,27 @@ TEST_CASE("value conversion") #if defined(JSON_HAS_CPP_17) SECTION("exception in case of a non-string type using string_view") { - CHECK_THROWS_AS(json(json::value_t::null).get(), json::type_error&); - CHECK_THROWS_AS(json(json::value_t::object).get(), json::type_error&); - CHECK_THROWS_AS(json(json::value_t::array).get(), json::type_error&); - CHECK_THROWS_AS(json(json::value_t::boolean).get(), json::type_error&); - CHECK_THROWS_AS(json(json::value_t::number_integer).get(), json::type_error&); - CHECK_THROWS_AS(json(json::value_t::number_unsigned).get(), json::type_error&); - CHECK_THROWS_AS(json(json::value_t::number_float).get(), json::type_error&); + CHECK_THROWS_AS(json(json::value_t::null).get(), json::type_error&); + CHECK_THROWS_AS(json(json::value_t::object).get(), json::type_error&); + CHECK_THROWS_AS(json(json::value_t::array).get(), json::type_error&); + CHECK_THROWS_AS(json(json::value_t::boolean).get(), json::type_error&); + CHECK_THROWS_AS(json(json::value_t::number_integer).get(), json::type_error&); + CHECK_THROWS_AS(json(json::value_t::number_unsigned).get(), json::type_error&); + CHECK_THROWS_AS(json(json::value_t::number_float).get(), json::type_error&); - CHECK_THROWS_WITH(json(json::value_t::null).get(), + CHECK_THROWS_WITH(json(json::value_t::null).get(), "[json.exception.type_error.302] type must be string, but is null"); - CHECK_THROWS_WITH(json(json::value_t::object).get(), + CHECK_THROWS_WITH(json(json::value_t::object).get(), "[json.exception.type_error.302] type must be string, but is object"); - CHECK_THROWS_WITH(json(json::value_t::array).get(), + CHECK_THROWS_WITH(json(json::value_t::array).get(), "[json.exception.type_error.302] type must be string, but is array"); - CHECK_THROWS_WITH(json(json::value_t::boolean).get(), + CHECK_THROWS_WITH(json(json::value_t::boolean).get(), "[json.exception.type_error.302] type must be string, but is boolean"); - CHECK_THROWS_WITH(json(json::value_t::number_integer).get(), + CHECK_THROWS_WITH(json(json::value_t::number_integer).get(), "[json.exception.type_error.302] type must be string, but is number"); - CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get(), + CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get(), "[json.exception.type_error.302] type must be string, but is number"); - CHECK_THROWS_WITH(json(json::value_t::number_float).get(), + CHECK_THROWS_WITH(json(json::value_t::number_float).get(), "[json.exception.type_error.302] type must be string, but is number"); } #endif @@ -562,7 +559,7 @@ TEST_CASE("value conversion") SECTION("std::string_view") { std::string s = "previous value"; - std::string_view sv = s; + string_view sv = s; j.get_to(sv); CHECK(json(sv) == j); } @@ -617,7 +614,7 @@ TEST_CASE("value conversion") #if defined(JSON_HAS_CPP_17) SECTION("std::string_view") { - std::string_view s = j.get(); + string_view s = j.get(); CHECK(json(s) == j); } #endif diff --git a/test/src/unit-regression1.cpp b/test/src/unit-regression1.cpp index 9dcc75b092..ff9ff6d8b5 100644 --- a/test/src/unit-regression1.cpp +++ b/test/src/unit-regression1.cpp @@ -47,10 +47,6 @@ using nlohmann::json; #define JSON_HAS_CPP_17 #endif -#ifdef JSON_HAS_CPP_17 - #include -#endif - #include "fifo_map.hpp" ///////////////////////////////////////////////////////////////////// diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index ca50cdd41d..c832f7d5a5 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -48,7 +48,10 @@ using nlohmann::json; #endif #ifdef JSON_HAS_CPP_17 - #include + #if __has_include() + #define HAS_STD_VARIANT + #include + #endif #endif ///////////////////////////////////////////////////////////////////// @@ -247,7 +250,7 @@ TEST_CASE("regression tests 2") CHECK(diffs.size() == 1); // Note the change here, was 2 } -#ifdef JSON_HAS_CPP_17 +#ifdef HAS_STD_VARIANT SECTION("issue #1292 - Serializing std::variant causes stack overflow") { static_assert(