fix #182: Merge branch 'workaround-msvc-cplusplus-macro'

This commit is contained in:
ToruNiina 2021-12-18 15:41:43 +09:00
commit bf2384d8da
14 changed files with 72 additions and 33 deletions

View File

@ -40,7 +40,7 @@ else()
endif()
if(MSVC)
add_definitions("/Zc:__cplusplus") # define __cplusplus value correctly
# add_definitions("/Zc:__cplusplus") # define __cplusplus value correctly
add_definitions("/utf-8") # enable to use u8"" literal
if(MSVC_VERSION LESS 1910)
message(STATUS "MSVC < 1910. DEFINE_CONVERSION_NON_INTRUSIVE is disabled")

View File

@ -272,7 +272,7 @@ BOOST_AUTO_TEST_CASE(test_construct_value_with_comments)
BOOST_TEST(v.is_string());
BOOST_TEST(v.as_string() == "str");
}
#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
{
using namespace std::literals::string_view_literals;
const value_type v("str"sv, {"comment1", "comment2"});

View File

@ -13,7 +13,7 @@
#include <list>
#include <deque>
#include <array>
#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
#include <string_view>
#endif
#include <tuple>
@ -477,7 +477,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_find_string_type, value_type, test_value_type
BOOST_TEST("foo" == moved);
}
#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
{
value_type v{{"key", toml::string("foo", toml::string_t::basic)}};
BOOST_TEST("foo" == toml::find<std::string_view>(v, "key"));

View File

@ -12,7 +12,7 @@
#include <deque>
#include <array>
#include <tuple>
#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
#include <string_view>
#endif

View File

@ -12,7 +12,7 @@
#include <deque>
#include <array>
#include <tuple>
#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
#include <string_view>
#endif

View File

@ -12,7 +12,7 @@
#include <deque>
#include <array>
#include <tuple>
#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
#include <string_view>
#endif
@ -224,7 +224,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_get_string_type, value_type, test_value_types
BOOST_TEST("foobar" == x);
}
#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
{
value_type v("foo", toml::string_t::basic);
BOOST_TEST("foo" == toml::get<std::string_view>(v));

View File

@ -12,7 +12,7 @@
#include <deque>
#include <array>
#include <tuple>
#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
#include <string_view>
#endif

View File

@ -133,7 +133,7 @@ BOOST_AUTO_TEST_CASE(test_string_add_assign)
str += str2;
BOOST_TEST(str.str == "foobar");
}
#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
// std::string_view
{
toml::string str("foo");

View File

@ -9,7 +9,7 @@
#include <map>
#include <list>
#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
#include <string_view>
#endif
@ -423,7 +423,7 @@ BOOST_AUTO_TEST_CASE(test_value_string)
BOOST_TEST(v2.as_boolean() == true);
BOOST_TEST(v3.as_boolean() == true);
#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
std::string_view sv = "foo";
toml::value v7(sv);

View File

@ -25,14 +25,6 @@
#ifndef TOML_FOR_MODERN_CPP
#define TOML_FOR_MODERN_CPP
#ifndef __cplusplus
# error "__cplusplus is not defined"
#endif
#if __cplusplus < 201103L && _MSC_VER < 1900
# error "toml11 requires C++11 or later."
#endif
#define TOML11_VERSION_MAJOR 3
#define TOML11_VERSION_MINOR 7
#define TOML11_VERSION_PATCH 0

View File

@ -2,12 +2,15 @@
// Distributed under the MIT License.
#ifndef TOML11_STRING_HPP
#define TOML11_STRING_HPP
#include "version.hpp"
#include <cstdint>
#include <algorithm>
#include <string>
#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
#if __has_include(<string_view>)
#define TOML11_USING_STRING_VIEW 1
#include <string_view>

View File

@ -5,6 +5,7 @@
#include "from.hpp"
#include "into.hpp"
#include "version.hpp"
#include <chrono>
#include <forward_list>
@ -13,7 +14,7 @@
#include <type_traits>
#include <utility>
#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
#if __has_include(<string_view>)
#include <string_view>
#endif // has_include(<string_view>)
@ -146,7 +147,7 @@ struct has_specialized_into : decltype(has_specialized_into_impl::check<T>(nullp
// ---------------------------------------------------------------------------
// C++17 and/or/not
#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
using std::conjunction;
using std::disjunction;
@ -208,7 +209,7 @@ template<typename T>
struct is_container : conjunction<
negation<is_map<T>>, // not a map
negation<std::is_same<T, std::string>>, // not a std::string
#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
#if __has_include(<string_view>)
negation<std::is_same<T, std::string_view>>, // not a std::string_view
#endif // has_include(<string_view>)
@ -233,7 +234,7 @@ struct is_basic_value<::toml::basic_value<C, M, V>>: std::true_type{};
// ---------------------------------------------------------------------------
// C++14 index_sequence
#if __cplusplus >= 201402L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201402L
using std::index_sequence;
using std::make_index_sequence;
@ -263,12 +264,12 @@ struct index_sequence_maker<0>
template<std::size_t N>
using make_index_sequence = typename index_sequence_maker<N-1>::type;
#endif // __cplusplus >= 2014
#endif // cplusplus >= 2014
// ---------------------------------------------------------------------------
// C++14 enable_if_t
#if __cplusplus >= 201402L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201402L
using std::enable_if_t;
@ -277,12 +278,12 @@ using std::enable_if_t;
template<bool B, typename T>
using enable_if_t = typename std::enable_if<B, T>::type;
#endif // __cplusplus >= 2014
#endif // cplusplus >= 2014
// ---------------------------------------------------------------------------
// return_type_of_t
#if __cplusplus >= 201703L && defined(__cpp_lib_is_invocable) && __cpp_lib_is_invocable>=201703
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L && defined(__cpp_lib_is_invocable) && __cpp_lib_is_invocable>=201703
template<typename F, typename ... Args>
using return_type_of_t = std::invoke_result_t<F, Args...>;

View File

@ -7,8 +7,9 @@
#include <utility>
#include "traits.hpp"
#include "version.hpp"
#if __cplusplus >= 201402L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201402L
# define TOML11_MARK_AS_DEPRECATED(msg) [[deprecated(msg)]]
#elif defined(__GNUC__)
# define TOML11_MARK_AS_DEPRECATED(msg) __attribute__((deprecated(msg)))
@ -21,7 +22,7 @@
namespace toml
{
#if __cplusplus >= 201402L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201402L
using std::make_unique;
@ -33,7 +34,7 @@ inline std::unique_ptr<T> make_unique(Ts&& ... args)
return std::unique_ptr<T>(new T(std::forward<Ts>(args)...));
}
#endif // __cplusplus >= 2014
#endif // TOML11_CPLUSPLUS_STANDARD_VERSION >= 2014
namespace detail
{
@ -91,7 +92,7 @@ T from_string(const std::string& str, T opt)
namespace detail
{
#if __cplusplus >= 201402L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201402L
template<typename T>
decltype(auto) last_one(T&& tail) noexcept
{

42
toml/version.hpp Normal file
View File

@ -0,0 +1,42 @@
#ifndef TOML11_VERSION_HPP
#define TOML11_VERSION_HPP
// This file checks C++ version.
#ifndef __cplusplus
# error "__cplusplus is not defined"
#endif
// Since MSVC does not define `__cplusplus` correctly unless you pass
// `/Zc:__cplusplus` when compiling, the workaround macros are added.
// Those enables you to define version manually or to use MSVC specific
// version macro automatically.
//
// The value of `__cplusplus` macro is defined in the C++ standard spec, but
// MSVC ignores the value, maybe because of backward compatibility. Instead,
// MSVC defines _MSVC_LANG that has the same value as __cplusplus defined in
// the C++ standard. First we check the manual version definition, and then
// we check if _MSVC_LANG is defined. If neither, use normal `__cplusplus`.
//
// FYI: https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=msvc-170
// https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170
//
#if defined(TOML11_ENFORCE_CXX11)
# define TOML11_CPLUSPLUS_STANDARD_VERSION 201103L
#elif defined(TOML11_ENFORCE_CXX14)
# define TOML11_CPLUSPLUS_STANDARD_VERSION 201402L
#elif defined(TOML11_ENFORCE_CXX17)
# define TOML11_CPLUSPLUS_STANDARD_VERSION 201703L
#elif defined(TOML11_ENFORCE_CXX20)
# define TOML11_CPLUSPLUS_STANDARD_VERSION 202002L
#elif defined(_MSVC_LANG) && defined(_MSC_VER) && 1910 <= _MSC_VER
# define TOML11_CPLUSPLUS_STANDARD_VERSION _MSVC_LANG
#else
# define TOML11_CPLUSPLUS_STANDARD_VERSION __cplusplus
#endif
#if TOML11_CPLUSPLUS_STANDARD_VERSION < 201103L && _MSC_VER < 1900
# error "toml11 requires C++11 or later."
#endif
#endif// TOML11_VERSION_HPP