Skip to content

Commit

Permalink
add TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA workaround MSVC error C2057
Browse files Browse the repository at this point in the history
Offered a workaround to compile errors like "error C2057: expected constant expression", when using the "legacy lambda processor" of Visual C++. Such compile errors were reported by Kevin Dick, Jan 19, 2024, at issue #219
  • Loading branch information
N-Dekker committed Feb 7, 2025
1 parent d00dd1f commit 7d5918a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/toml++/impl/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,10 @@ TOML_NAMESPACE_START
static_cast<node_ref>(static_cast<Array&&>(arr)[i])
.visit(
[&]([[maybe_unused]] auto&& elem) //
// Define this macro as a workaround to compile errors caused by a bug in MSVC's "legacy lambda processor".
#if !TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA
noexcept(for_each_is_nothrow_one<Func&&, Array&&, decltype(elem)>::value)
#endif
{
using elem_ref = for_each_elem_ref<decltype(elem), Array&&>;
static_assert(std::is_reference_v<elem_ref>);
Expand Down
11 changes: 11 additions & 0 deletions include/toml++/impl/preprocessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,17 @@ TOML_ENABLE_WARNINGS;
/// \detail Defaults to `0`.
//# }}

#ifndef TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA
#define TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA 0
#endif
//# {{
/// \def TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA
/// \brief Disable using noexcept(<condition>) in lambda definitions within the toml++ library implementation.
/// \detail This macro offers a workaround to a bug in the old "legacy lambda processor" of Visual C++, which
/// caused compile errors like "error C2057: expected constant expression", when it encountered such lambda's.
/// These compile errors were reported by Kevin Dick, Jan 19, 2024, at https://github.com/marzer/tomlplusplus/issues/219
//# }}

/// @}
//#====================================================================================================================
//# CHARCONV SUPPORT
Expand Down
3 changes: 3 additions & 0 deletions include/toml++/impl/table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,10 @@ TOML_NAMESPACE_START
static_cast<node_ref>(*kvp.second)
.visit(
[&]([[maybe_unused]] auto&& v) //
// Define this macro as a workaround to compile errors caused by a bug in MSVC's "legacy lambda processor".
#if !TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA
noexcept(for_each_is_nothrow_one<Func&&, Table&&, decltype(v)>::value)
#endif
{
using value_ref = for_each_value_ref<decltype(v), Table&&>;
static_assert(std::is_reference_v<value_ref>);
Expand Down
10 changes: 10 additions & 0 deletions toml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,10 @@ TOML_ENABLE_WARNINGS;
#define TOML_ENABLE_FLOAT16 0
#endif

#ifndef TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA
#define TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA 0
#endif

#if !defined(TOML_FLOAT_CHARCONV) && (TOML_GCC || TOML_CLANG || (TOML_ICC && !TOML_ICC_CL))
// not supported by any version of GCC or Clang as of 26/11/2020
// not supported by any version of ICC on Linux as of 11/01/2021
Expand Down Expand Up @@ -6799,7 +6803,10 @@ TOML_NAMESPACE_START
static_cast<node_ref>(static_cast<Array&&>(arr)[i])
.visit(
[&]([[maybe_unused]] auto&& elem) //
// Define this macro as a workaround to compile errors caused by a bug in MSVC's "legacy lambda processor".
#if !TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA
noexcept(for_each_is_nothrow_one<Func&&, Array&&, decltype(elem)>::value)
#endif
{
using elem_ref = for_each_elem_ref<decltype(elem), Array&&>;
static_assert(std::is_reference_v<elem_ref>);
Expand Down Expand Up @@ -8161,7 +8168,10 @@ TOML_NAMESPACE_START
static_cast<node_ref>(*kvp.second)
.visit(
[&]([[maybe_unused]] auto&& v) //
// Define this macro as a workaround to compile errors caused by a bug in MSVC's "legacy lambda processor".
#if !TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA
noexcept(for_each_is_nothrow_one<Func&&, Table&&, decltype(v)>::value)
#endif
{
using value_ref = for_each_value_ref<decltype(v), Table&&>;
static_assert(std::is_reference_v<value_ref>);
Expand Down

0 comments on commit 7d5918a

Please sign in to comment.