0

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;
};
Talmid
  • 15
  • 5
  • For me it shows an Intellisense error, but compiles and runs successfully –  May 07 '21 at 00:33
  • 1
    See [this question](https://stackoverflow.com/questions/5368862/why-do-multiple-inherited-functions-with-same-name-but-different-signatures-not). – 1201ProgramAlarm May 07 '21 at 00:35
  • The same thing happens when inheriting from non-lambda classes, so I'd be surprised if inheriting from lambdas (which are essentially syntactic sugar for anonymous classes) would behave differently. You'll still get ambiguity if both the inherited `operator()` have the same signature. – Peter May 07 '21 at 00:44
  • Works fine for [clang and msvc](https://godbolt.org/z/MdnqPPsbc), though that doesn't necessarily mean they're correct. – Patrick Roberts May 07 '21 at 00:47

0 Answers0