From 818f65e30a485a48d8f289bf9fea2ee2356231ed Mon Sep 17 00:00:00 2001 From: Markus Werle Date: Thu, 7 May 2020 12:14:56 +0200 Subject: [PATCH 1/5] make implicit capture explicit --- include/fmt/format.h | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index b828ad292a60..d895b6632f5c 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1467,9 +1467,10 @@ template struct int_writer { void on_dec() { auto num_digits = count_digits(abs_value); - out = write_int(out, num_digits, get_prefix(), specs, [=](iterator it) { - return format_decimal(it, abs_value, num_digits); - }); + out = + write_int(out, num_digits, get_prefix(), specs, [=, this](iterator it) { + return format_decimal(it, abs_value, num_digits); + }); } void on_hex() { @@ -1478,9 +1479,11 @@ template struct int_writer { prefix[prefix_size++] = specs.type; } int num_digits = count_digits<4>(abs_value); - out = write_int(out, num_digits, get_prefix(), specs, [=](iterator it) { - return format_uint<4, Char>(it, abs_value, num_digits, specs.type != 'x'); - }); + out = + write_int(out, num_digits, get_prefix(), specs, [=, this](iterator it) { + return format_uint<4, Char>(it, abs_value, num_digits, + specs.type != 'x'); + }); } void on_bin() { @@ -1489,9 +1492,10 @@ template struct int_writer { prefix[prefix_size++] = static_cast(specs.type); } int num_digits = count_digits<1>(abs_value); - out = write_int(out, num_digits, get_prefix(), specs, [=](iterator it) { - return format_uint<1, Char>(it, abs_value, num_digits); - }); + out = + write_int(out, num_digits, get_prefix(), specs, [=, this](iterator it) { + return format_uint<1, Char>(it, abs_value, num_digits); + }); } void on_oct() { @@ -1501,9 +1505,10 @@ template struct int_writer { // is not greater than the number of digits. prefix[prefix_size++] = '0'; } - out = write_int(out, num_digits, get_prefix(), specs, [=](iterator it) { - return format_uint<3, Char>(it, abs_value, num_digits); - }); + out = + write_int(out, num_digits, get_prefix(), specs, [=, this](iterator it) { + return format_uint<3, Char>(it, abs_value, num_digits); + }); } enum { sep_size = 1 }; From 91251b90696ab3769b31937e5716abaf33e68da1 Mon Sep 17 00:00:00 2001 From: Markus Werle Date: Thu, 7 May 2020 22:55:27 +0200 Subject: [PATCH 2/5] capture of this: support C++14 & C++20 via macro --- include/fmt/format.h | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index d895b6632f5c..1dbf099418d8 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -172,6 +172,12 @@ FMT_END_NAMESPACE # define FMT_BUILTIN_CLZLL(n) __builtin_clzll(n) #endif +#if (__cplusplus >= 201703L) +# define FMT_CAPTURE_OF_THIS , this +#else +# define FMT_CAPTURE_OF_THIS +#endif + // Some compilers masquerade as both MSVC and GCC-likes or otherwise support // __builtin_clz and __builtin_clzll, so only define FMT_BUILTIN_CLZ using the // MSVC intrinsics if the clz and clzll builtins are not available. @@ -1467,10 +1473,10 @@ template struct int_writer { void on_dec() { auto num_digits = count_digits(abs_value); - out = - write_int(out, num_digits, get_prefix(), specs, [=, this](iterator it) { - return format_decimal(it, abs_value, num_digits); - }); + out = write_int(out, num_digits, get_prefix(), specs, + [= FMT_CAPTURE_OF_THIS](iterator it) { + return format_decimal(it, abs_value, num_digits); + }); } void on_hex() { @@ -1479,11 +1485,11 @@ template struct int_writer { prefix[prefix_size++] = specs.type; } int num_digits = count_digits<4>(abs_value); - out = - write_int(out, num_digits, get_prefix(), specs, [=, this](iterator it) { - return format_uint<4, Char>(it, abs_value, num_digits, - specs.type != 'x'); - }); + out = write_int(out, num_digits, get_prefix(), specs, + [= FMT_CAPTURE_OF_THIS](iterator it) { + return format_uint<4, Char>(it, abs_value, num_digits, + specs.type != 'x'); + }); } void on_bin() { @@ -1492,10 +1498,10 @@ template struct int_writer { prefix[prefix_size++] = static_cast(specs.type); } int num_digits = count_digits<1>(abs_value); - out = - write_int(out, num_digits, get_prefix(), specs, [=, this](iterator it) { - return format_uint<1, Char>(it, abs_value, num_digits); - }); + out = write_int(out, num_digits, get_prefix(), specs, + [= FMT_CAPTURE_OF_THIS](iterator it) { + return format_uint<1, Char>(it, abs_value, num_digits); + }); } void on_oct() { @@ -1505,10 +1511,10 @@ template struct int_writer { // is not greater than the number of digits. prefix[prefix_size++] = '0'; } - out = - write_int(out, num_digits, get_prefix(), specs, [=, this](iterator it) { - return format_uint<3, Char>(it, abs_value, num_digits); - }); + out = write_int(out, num_digits, get_prefix(), specs, + [= FMT_CAPTURE_OF_THIS](iterator it) { + return format_uint<3, Char>(it, abs_value, num_digits); + }); } enum { sep_size = 1 }; From ae0a2cab53b88faa4f5fd135f18bbbe1e1223113 Mon Sep 17 00:00:00 2001 From: Markus Werle Date: Thu, 7 May 2020 23:01:04 +0200 Subject: [PATCH 3/5] narrow this capture to C++20 --- include/fmt/format.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 1dbf099418d8..9fd795be45a4 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -172,7 +172,7 @@ FMT_END_NAMESPACE # define FMT_BUILTIN_CLZLL(n) __builtin_clzll(n) #endif -#if (__cplusplus >= 201703L) +#if (__cplusplus > 201703L) # define FMT_CAPTURE_OF_THIS , this #else # define FMT_CAPTURE_OF_THIS From 9f8cacb8a47b22298f4a2550fc223d8b4ec5bb38 Mon Sep 17 00:00:00 2001 From: Markus Werle Date: Fri, 8 May 2020 14:42:05 +0200 Subject: [PATCH 4/5] rename FMT_CAPTURE_OF_THIS to FMT_CAPTURE_THIS --- include/fmt/format.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 9fd795be45a4..0f0758b6a705 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -173,9 +173,9 @@ FMT_END_NAMESPACE #endif #if (__cplusplus > 201703L) -# define FMT_CAPTURE_OF_THIS , this +# define FMT_CAPTURE_THIS , this #else -# define FMT_CAPTURE_OF_THIS +# define FMT_CAPTURE_THIS #endif // Some compilers masquerade as both MSVC and GCC-likes or otherwise support @@ -1474,7 +1474,7 @@ template struct int_writer { void on_dec() { auto num_digits = count_digits(abs_value); out = write_int(out, num_digits, get_prefix(), specs, - [= FMT_CAPTURE_OF_THIS](iterator it) { + [= FMT_CAPTURE_THIS](iterator it) { return format_decimal(it, abs_value, num_digits); }); } @@ -1486,7 +1486,7 @@ template struct int_writer { } int num_digits = count_digits<4>(abs_value); out = write_int(out, num_digits, get_prefix(), specs, - [= FMT_CAPTURE_OF_THIS](iterator it) { + [= FMT_CAPTURE_THIS](iterator it) { return format_uint<4, Char>(it, abs_value, num_digits, specs.type != 'x'); }); @@ -1499,7 +1499,7 @@ template struct int_writer { } int num_digits = count_digits<1>(abs_value); out = write_int(out, num_digits, get_prefix(), specs, - [= FMT_CAPTURE_OF_THIS](iterator it) { + [= FMT_CAPTURE_THIS](iterator it) { return format_uint<1, Char>(it, abs_value, num_digits); }); } @@ -1512,7 +1512,7 @@ template struct int_writer { prefix[prefix_size++] = '0'; } out = write_int(out, num_digits, get_prefix(), specs, - [= FMT_CAPTURE_OF_THIS](iterator it) { + [= FMT_CAPTURE_THIS](iterator it) { return format_uint<3, Char>(it, abs_value, num_digits); }); } From 393fd86ab98e0ec44f38b055cf93dd3e2b963b0f Mon Sep 17 00:00:00 2001 From: Markus Werle Date: Sat, 9 May 2020 17:55:44 +0200 Subject: [PATCH 5/5] explicit capture to avoid MACRO --- include/fmt/format.h | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 0f0758b6a705..c100f6b9e7af 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -172,12 +172,6 @@ FMT_END_NAMESPACE # define FMT_BUILTIN_CLZLL(n) __builtin_clzll(n) #endif -#if (__cplusplus > 201703L) -# define FMT_CAPTURE_THIS , this -#else -# define FMT_CAPTURE_THIS -#endif - // Some compilers masquerade as both MSVC and GCC-likes or otherwise support // __builtin_clz and __builtin_clzll, so only define FMT_BUILTIN_CLZ using the // MSVC intrinsics if the clz and clzll builtins are not available. @@ -1474,7 +1468,7 @@ template struct int_writer { void on_dec() { auto num_digits = count_digits(abs_value); out = write_int(out, num_digits, get_prefix(), specs, - [= FMT_CAPTURE_THIS](iterator it) { + [this, num_digits](iterator it) { return format_decimal(it, abs_value, num_digits); }); } @@ -1486,7 +1480,7 @@ template struct int_writer { } int num_digits = count_digits<4>(abs_value); out = write_int(out, num_digits, get_prefix(), specs, - [= FMT_CAPTURE_THIS](iterator it) { + [this, num_digits](iterator it) { return format_uint<4, Char>(it, abs_value, num_digits, specs.type != 'x'); }); @@ -1499,7 +1493,7 @@ template struct int_writer { } int num_digits = count_digits<1>(abs_value); out = write_int(out, num_digits, get_prefix(), specs, - [= FMT_CAPTURE_THIS](iterator it) { + [this, num_digits](iterator it) { return format_uint<1, Char>(it, abs_value, num_digits); }); } @@ -1512,7 +1506,7 @@ template struct int_writer { prefix[prefix_size++] = '0'; } out = write_int(out, num_digits, get_prefix(), specs, - [= FMT_CAPTURE_THIS](iterator it) { + [this, num_digits](iterator it) { return format_uint<3, Char>(it, abs_value, num_digits); }); }