Overloaded lambdas in C++ and differences between clang and gcc

前端 未结 2 1850
抹茶落季
抹茶落季 2020-11-29 06:47

I\'m playing with a trick to overload lambdas in C++. Specifically:

// For std::function
#include          


        
2条回答
  •  遥遥无期
    2020-11-29 07:02

    The original code shouldn't compile, gcc is correct here. See [class.member.lookup]:

    Otherwise (i.e., C does not contain a declaration of f or the resulting declaration set is empty), S(f,C) is initially empty. If C has base classes, calculate the lookup set for f in each direct base class subobject Bi, and merge each such lookup set S(f,Bi) in turn into S(f,C).
    — [..]
    — Otherwise, if the declaration sets of S(f,Bi) and S(f,C) differ, the merge is ambiguous...

    The initial declaration set is empty (overload has no methods) - so merge all the bases, all of whom have differing sets. So the merge should fail. That rule only applies if the declaration set of overload is empty though, which is why the explicit adding of the using F1::operator() works.

提交回复
热议问题