diff --git a/.travis.yml b/.travis.yml index 484c9350d..fd93971e5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -206,7 +206,7 @@ matrix: compiler: gcc env: - COMPILER=g++-8 - - CXXFLAGS=-std=c++17 + - CMAKE_OPTIONS=-DJSON_ForceCpp17=ON addons: apt: sources: ['ubuntu-toolchain-r-test'] @@ -290,7 +290,7 @@ matrix: compiler: clang env: - COMPILER=clang++-7 - - CXXFLAGS=-std=c++1z + - CMAKE_OPTIONS=-DJSON_ForceCpp17=ON addons: apt: sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-7'] diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index b9558a939..9a7b0da3c 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -72,6 +72,10 @@ SOFTWARE. #include #include +#if defined(JSON_HAS_CPP_17) + #include +#endif + /*! @brief namespace for Niels Lohmann @see https://github.com/nlohmann @@ -2863,9 +2867,9 @@ class basic_json #ifndef _MSC_VER // fix for issue #167 operator<< ambiguity under VS2015 and not std::is_same>::value -#if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) and _MSC_VER <= 1914)) - and not std::is_same::value #endif +#if defined(JSON_HAS_CPP_17) + and not std::is_same::value #endif and detail::is_detected::value , int >::type = 0 > diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 1f11538cb..e8863cb20 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -12841,6 +12841,10 @@ class serializer // #include +#if defined(JSON_HAS_CPP_17) + #include +#endif + /*! @brief namespace for Niels Lohmann @see https://github.com/nlohmann @@ -15632,9 +15636,9 @@ class basic_json #ifndef _MSC_VER // fix for issue #167 operator<< ambiguity under VS2015 and not std::is_same>::value -#if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) and _MSC_VER <= 1914)) - and not std::is_same::value #endif +#if defined(JSON_HAS_CPP_17) + and not std::is_same::value #endif and detail::is_detected::value , int >::type = 0 > diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 74df75227..0963e01e3 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,6 +2,7 @@ option(JSON_Sanitizer "Build test suite with Clang sanitizer" OFF) option(JSON_Valgrind "Execute test suite with Valgrind" OFF) option(JSON_NoExceptions "Build test suite without exceptions" OFF) option(JSON_Coverage "Build test suite with coverage information" OFF) +option(JSON_ForceCpp17 "Use C++17 standard" OFF) if(JSON_Sanitizer) message(STATUS "Building test suite with Clang sanitizer") @@ -85,7 +86,7 @@ if(MSVC) # Disable warning C4566: character represented by universal-character-name '\uFF01' cannot be represented in the current code page (1252) # Disable warning C4996: 'nlohmann::basic_json::operator <<': was declared deprecated set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4389 /wd4309 /wd4566 /wd4996") - + # https://github.com/nlohmann/json/issues/1114 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /bigobj") endif() @@ -104,6 +105,15 @@ foreach(file ${files}) CATCH_CONFIG_FAST_COMPILE $<$:_SCL_SECURE_NO_WARNINGS> ) + if (${CMAKE_VERSION} VERSION_LESS "3.8.0") + if (JSON_ForceCpp17) + message(FATAL_ERROR "Cannot force C++17 with this CMake version, you need at least version 3.8") + endif() + else() + if (JSON_ForceCpp17) + target_compile_features(${testcase} PRIVATE cxx_std_17) + endif() + endif() target_compile_options(${testcase} PRIVATE $<$:/EHsc;$<$:/Od>> $<$>:-Wno-deprecated;-Wno-float-equal> diff --git a/test/src/unit-conversions.cpp b/test/src/unit-conversions.cpp index c55fd5c04..48c6b7b61 100644 --- a/test/src/unit-conversions.cpp +++ b/test/src/unit-conversions.cpp @@ -51,6 +51,20 @@ using nlohmann::json; #include #endif +template +struct is_implicitly_convertible +{ + private: + template + static std::true_type test(decltype(&C::operator To)); + + template + static std::false_type test(...); + + public: + static constexpr bool value = std::is_same(nullptr))>::value; +}; + TEST_CASE("value conversion") { SECTION("get an object (explicit)") @@ -480,8 +494,7 @@ TEST_CASE("value conversion") #if defined(JSON_HAS_CPP_17) SECTION("std::string_view") { - std::string_view s = j; - CHECK(json(s) == j); + static_assert(!is_implicitly_convertible::value); } #endif