C++ Logo

STD-PROPOSALS

Advanced search

Subject: Re: [std-proposals] Allow maybe_unused for lambda captures?
From: Markus Dreseler (isocpp.org_at_[hidden])
Date: 2019-12-15 12:51:29


Got it, thank you for the link to the clang bug.

> [[maybe_unused]] should be used when the use is either completely
absent, or effectively absent because it's used inside a macro that
might be conditionally defined otherwise, or inside an #ifdef block that
was compiled out, or inside an #include file whose contents are
platform-dependent.

If I understand you correctly, this would apply to the following
example. Could you please give your thoughts on this one:

     #include <cassert>
     void foo(int a) {
       [&a](){
         assert(a == 3);
       }();
       // actually do something with a
     }

https://godbolt.org/z/udHag8

Thanks
Markus

On 15.12.19 19:00, Arthur O'Dwyer wrote:
> On Sun, Dec 15, 2019 at 11:32 AM Markus Dreseler via Std-Proposals
> <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]>>
> wrote:
>
>
> I wonder if it might make sense to [...] allow maybe_unused for
> lambda captures.
>
> The motivation for this can be seen in the following code, where `foo`
> increments `a` depending on the template parameter:
>
>      #include <iostream>
>      #include <type_traits>
>
>      template <bool Enable>
>      int foo() {
>        int a = 0;
>        [&a]() {
>          if constexpr (Enable) {
>            ++a;
>          }
>        }();
>        return a;
>      }
>
>      int main() {
>        std::cout << foo<true>() << std::endl;   // prints 1
>        std::cout << foo<false>() << std::endl;  // prints 0
>      }
>
> As the `foo<false>` instantiation disables the `++a` part, clang
> currently emits a warning:
>
>      capture.cpp:7:5: warning: lambda capture 'a' is not used
> [-Wunused-lambda-capture]
>        [&a]() {
>          ^
>
>
> This is a bug in Clang trunk.  A use inside a discarded statement should
> suppress -Wunused-lambda-capture exactly as it suppresses -Wunused-variable:
>
> // https://godbolt.org/z/XV-svx
> void foo() { int a = 1; if constexpr (E) { throw a; } }  // OK
> auto bar = [a=1]{ if constexpr (E) { throw a; } };  // WARNING
>
> https://bugs.llvm.org/show_bug.cgi?id=35450
>
> The [[maybe_unused]] attribute shouldn't be used to indicate "this
> variable is maybe-used depending on which template instantiation I'm
> in." The compiler is perfectly capable of detecting templates on its
> own.  [[maybe_unused]] should be used when the use is either completely
> absent, or effectively absent because it's used inside a macro that
> might be conditionally defined otherwise, or inside an #ifdef block that
> was compiled out, or inside an #include file whose contents are
> platform-dependent.
> So what we have here is a use inside a template, that Clang fails to
> detect and suppress. That's just a straight-up Clang bug. And someone
> else has already noticed and filed it.
>
> HTH,
> -Arthur


STD-PROPOSALS list run by std-proposals-owner@lists.isocpp.org

Standard Proposals Archives on Google Groups