Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

c++ - Why is [[maybe_unused]] in captured list not possible?

I wonder why I can't use the [[maybe_unused]] attribute in the capture list of a lambda. Is there a reason that variables of a captured lists can't be marked as [[maybe_unused]]? This would avoid to capture all variables, means [&], in cases like this:

auto lambda = [&x, [[maybe_unused]] &y](){ 
    if constexpr( x >= 0) {
        return x;
    }
    else {
        return y;
    }
}

In each case where x >= 0 there is a compiler warning that y is not used. But if x < 0, there is no warning. My first try was to use [[maybe_unused]], but that is not possible.

My question: is there a reason, why I can't use the [[maybe_unused]] attribute in the capture list of lambda?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I asked the same on the mailing list. The answer from Arthur O'Dwyer was:

Ah, yes, that would be a place where [[maybe_unused]] might apply. However, for now, you can easily write

(void)a;

to suppress the warning: https://godbolt.org/z/7ZHSYz That's less cluttering to the code (thus easier to read and maintain), and also works in existing compilers (thus easier to deploy).

"Yeah, but using this logic, isn't [[maybe_unused]] completely redundant and unnecessary and should never have been standardized?" AFAIK, yes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...