C++ : Inheriting from lambdas, overload resolution "quirk"

34 views Asked by At

Why is the using-declaration inside struct S necessary?

struct S inherits from both l1's and l2's types, which means their respective operator()s are considered when calling s(123). This is corroborated by the fact that, once I remove using, gcc tells me the "request for member ‘operator()’ is ambiguous", and goes on to list both lambdas as candidates.

I'm having difficulty understanding how the resolution is ambiguous to the compiler in one case (raw inheritance), and not in the other (using-declaration).

#include <iostream>

template <typename... Fs>
struct S : Fs...
{
    S(Fs... funcs) : Fs(std::move(funcs))...
    {
    }

    using Fs::operator()...; // Removing this line breaks the code
};

auto main() -> int
{
    auto l1 = []() { return 0; };
    auto l2 = [](int i) { return i; };
    S s(l1, l2);
    std::cout << s(123)
              << std::endl;
};
0

There are 0 answers