I have the following, which does not compile:
void func(std::optional<std::function<void(int, int)>> arg1)
{
return;
}
void callee(bool flag)
{
auto arg1 = [](int a1)
{
return;
};
auto arg2 = [](int a1)
{
return;
};
func(
(!flag ? std::optional{arg2} : (flag ? std::optional{arg1} : std::nullopt)),
std::optional{arg2});
}
int main(int argc, char **argv) {
callee(true);
}
For the following reason:
error: operands to '?:' have different types 'std::optional<callee(bool)::<lambda(int)> >' and 'std::optional<callee(bool)::<lambda(int)> >'
Explicitly defining the type of either arg1
or arg2
instead of using auto
allows this to compile.
Why is the compiler unable to deduce that they are in fact the same type?