Skip to content

Commit cbc00d6

Browse files
authored
add TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA workaround MSVC error C2057 (#247)
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
1 parent d6af261 commit cbc00d6

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ UTF-8 decoding is performed using a state machine based on Bjoern Hoehrmann's '[
290290
- **[@levicki](https://github.com/levicki)** - Helped design some new features
291291
- **[@moorereason](https://github.com/moorereason)** - Reported a whole bunch of bugs
292292
- **[@mosra](https://github.com/mosra)** - Created the awesome [m.css] used to generate the API docs
293+
- **[@N-Dekker](https://github.com/N-Dekker)** - Added a workaround for the legacy lambda processor of MSVC 2019/2022
293294
- **[@ned14](https://github.com/ned14)** - Reported a bunch of bugs and helped design some new features
294295
- **[@okureta](https://github.com/okureta)** - Reported a bug
295296
- **[@prince-chrismc](https://github.com/prince-chrismc)** - Added toml++ to ConanCenter, and fixed some typos

include/toml++/impl/array.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,10 @@ TOML_NAMESPACE_START
951951
static_cast<node_ref>(static_cast<Array&&>(arr)[i])
952952
.visit(
953953
[&]([[maybe_unused]] auto&& elem) //
954+
// Define this macro as a workaround to compile errors caused by a bug in MSVC's "legacy lambda processor".
955+
#if !TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA
954956
noexcept(for_each_is_nothrow_one<Func&&, Array&&, decltype(elem)>::value)
957+
#endif
955958
{
956959
using elem_ref = for_each_elem_ref<decltype(elem), Array&&>;
957960
static_assert(std::is_reference_v<elem_ref>);

include/toml++/impl/preprocessor.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,17 @@ TOML_ENABLE_WARNINGS;
12411241
/// \detail Defaults to `0`.
12421242
//# }}
12431243

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

include/toml++/impl/table.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,10 @@ TOML_NAMESPACE_START
930930
static_cast<node_ref>(*kvp.second)
931931
.visit(
932932
[&]([[maybe_unused]] auto&& v) //
933+
// Define this macro as a workaround to compile errors caused by a bug in MSVC's "legacy lambda processor".
934+
#if !TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA
933935
noexcept(for_each_is_nothrow_one<Func&&, Table&&, decltype(v)>::value)
936+
#endif
934937
{
935938
using value_ref = for_each_value_ref<decltype(v), Table&&>;
936939
static_assert(std::is_reference_v<value_ref>);

toml.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,10 @@ TOML_ENABLE_WARNINGS;
11331133
#define TOML_ENABLE_FLOAT16 0
11341134
#endif
11351135

1136+
#ifndef TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA
1137+
#define TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA 0
1138+
#endif
1139+
11361140
#if !defined(TOML_FLOAT_CHARCONV) && (TOML_GCC || TOML_CLANG || (TOML_ICC && !TOML_ICC_CL))
11371141
// not supported by any version of GCC or Clang as of 26/11/2020
11381142
// not supported by any version of ICC on Linux as of 11/01/2021
@@ -6799,7 +6803,10 @@ TOML_NAMESPACE_START
67996803
static_cast<node_ref>(static_cast<Array&&>(arr)[i])
68006804
.visit(
68016805
[&]([[maybe_unused]] auto&& elem) //
6806+
// Define this macro as a workaround to compile errors caused by a bug in MSVC's "legacy lambda processor".
6807+
#if !TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA
68026808
noexcept(for_each_is_nothrow_one<Func&&, Array&&, decltype(elem)>::value)
6809+
#endif
68036810
{
68046811
using elem_ref = for_each_elem_ref<decltype(elem), Array&&>;
68056812
static_assert(std::is_reference_v<elem_ref>);
@@ -8161,7 +8168,10 @@ TOML_NAMESPACE_START
81618168
static_cast<node_ref>(*kvp.second)
81628169
.visit(
81638170
[&]([[maybe_unused]] auto&& v) //
8171+
// Define this macro as a workaround to compile errors caused by a bug in MSVC's "legacy lambda processor".
8172+
#if !TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA
81648173
noexcept(for_each_is_nothrow_one<Func&&, Table&&, decltype(v)>::value)
8174+
#endif
81658175
{
81668176
using value_ref = for_each_value_ref<decltype(v), Table&&>;
81678177
static_assert(std::is_reference_v<value_ref>);

0 commit comments

Comments
 (0)