LambdaConstants.h
#ifndef LAMBDA_CONSTANTS_H
#define LAMBDA_CONSTANTS_H
/*
given a lambda that wants to capture two constants
const auto k1 = 1000;
const auto k2 = 2000;
int v = 0;
auto lambda = [&v, &k1, &k2]() {
v = k1 * k2;
}
Then unfortunately clang will correctly warn about unnecessary captures. And MSVC will fail to compile if you don't capture.
https://stackoverflow.com/questions/52416362/unused-lambda-capture-warning-when-capture-is-actually-used
An imperfect solution is to declare the lambda using the LAMBDA_CONSTANTS macro.
auto lambda = [&v
LAMBDA_CONSTANTS(&k1, &k2)
](){
v = k1 * k2;
}
This should work correctly. Most of the time.
NOTE: There is no comma after the final capture variable before the LAMBDA_CONSTANTS macro. The macro is variadic and will work with 1 or more captures.
*/
#if _MSC_VER && !defined(__clang__)
#define LAMBDA_CONSTANTS(...) ,__VA_ARGS__
#else
#define LAMBDA_CONSTANTS(...)
#endif
#endif // LAMBDA_CONSTANTS_H