Skip to content

Commit

Permalink
fix ToruNiina#182: Merge branch 'workaround-msvc-cplusplus-macro'
Browse files Browse the repository at this point in the history
  • Loading branch information
ToruNiina committed Dec 18, 2021
2 parents 4077707 + 8ebf9c9 commit bf2384d
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 33 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_comments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"});
Expand Down
4 changes: 2 additions & 2 deletions tests/test_find.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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"));
Expand Down
2 changes: 1 addition & 1 deletion tests/test_find_or.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <deque>
#include <array>
#include <tuple>
#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
#include <string_view>
#endif

Expand Down
2 changes: 1 addition & 1 deletion tests/test_find_or_recursive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <deque>
#include <array>
#include <tuple>
#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
#include <string_view>
#endif

Expand Down
4 changes: 2 additions & 2 deletions tests/test_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <deque>
#include <array>
#include <tuple>
#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
#include <string_view>
#endif

Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion tests/test_get_or.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <deque>
#include <array>
#include <tuple>
#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
#include <string_view>
#endif

Expand Down
2 changes: 1 addition & 1 deletion tests/test_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions tests/test_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <map>
#include <list>

#if __cplusplus >= 201703L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
#include <string_view>
#endif

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 0 additions & 8 deletions toml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion toml/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
17 changes: 9 additions & 8 deletions toml/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "from.hpp"
#include "into.hpp"
#include "version.hpp"

#include <chrono>
#include <forward_list>
Expand All @@ -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>)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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>)
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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...>;
Expand Down
9 changes: 5 additions & 4 deletions toml/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -21,7 +22,7 @@
namespace toml
{

#if __cplusplus >= 201402L
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201402L

using std::make_unique;

Expand All @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down
42 changes: 42 additions & 0 deletions toml/version.hpp
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit bf2384d

Please sign in to comment.