An example of what I'm referring to:
#include <type_traits>
void voidFunction() {}
template <typename Function>
void lambdaTest(Function func) {
[func]() -> void {
int someInt;
if constexpr (std::is_same_v<std::invoke_result_t<Function>, int>) {
someInt = std::invoke(func);
} else {
std::invoke(func);
}
};
}
int main(int argc, char** argv) {
lambdaTest(&voidFunction);
return 0;
}
This compiles in gcc 7.2. However, with MSVC 19.11.25547 I get this error:error C2440: '=': cannot convert from 'void' to 'int'
This code compiles fine with both compilers:
#include <type_traits>
void voidFunction() {}
template <typename Function>
void nonLambdaTest(Function func) {
int someInt;
if constexpr (std::is_same_v<std::invoke_result_t<Function>, int>) {
someInt = std::invoke(func);
} else {
std::invoke(func);
}
}
int main(int argc, char** argv) {
nonLambdaTest(&voidFunction);
return 0;
}
To me it looks like MSVC just ignores the constexpr if. Is this a bug in MSVC, or are constexpr if officially forbidden in lambdas?
constexpr ifin separate functions?template <typename Function> void lambdaTest(Function func) { var lambda = [func](){ nonLambdaTest(func) }; lambda(); }- Ben Voigtdecltype(Function())is supposed to yield returned type of function I think thatdecltype(Function())is supposed to bedecltype(::std::declval<Function>()())because type ofFunctionwould be a pointer to function. So this code seems to be broken in both cases. - user7860670