Use it to suppress compiler warnings of unused variables. Whether or not you choose to use this approach depends on the coding standards and practices of the team or organization you're working with.
In some workplaces, using the approach of commenting out unused variables like this:
bool f (auto /*unusedVariable*/) {return false;}
is common practice. It effectively communicates the intention to keep the variable, even if it appears unused.
Conversely, in other development environments, using techniques like macros might be preferred to address this issue and keep the codebase clean from such comments.
bool f (auto unusedVariable)
{
UNUSED (unusedVariable);
return false;
}
Ultimately, the choice between these methods or any other approach should be made in line with the coding conventions and requirements of your specific project or team. There's no one-size-fits-all answer, and what's important is to maintain consistency within your codebase.
Here's an example where [[maybe_unused]]
is commonly accepted:
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v {0, 1, 2, 3 };
std::cout << std::count_if (v.begin (), v.end (), [] (int i) {return (i & 1);}) << "\n";
std::cout << std::count_if (v.begin (), v.end (), [] ([[maybe_unused]] int i) {return true;}) << "\n"; // Compiles without a warning
std::cout << std::count_if (v.begin (), v.end (), [] (int i) {return false;}) << "\n"; // Produces a warning
}