Ambiguous operator overload on clang

前端 未结 1 338
隐瞒了意图╮
隐瞒了意图╮ 2021-01-11 11:33

When I try to compile this test program:

struct comma_guard
{
    template
    const comma_guard& operator,(T&&) const
    {
              


        
相关标签:
1条回答
  • 2021-01-11 11:49

    From my understanding of ADL lookup, the member function in comma_guard should be preferred and so therefore shouldn't be ambiguous. Is this correct?


    Answer: During overload resolution and according to the standard § 13.3.1/2 & 7 Candidate functions and argument lists [over.match.funcs]:

    2 The set of candidate functions can contain both member and non-member functions to be resolved against the same argument list.

    7 In each case where a candidate is a function template, candidate function template specializations are generated using template argument deduction (14.8.3, 14.8.2). Those candidates are then handled as candidate functions in the usual way [126].

    [footnote 126] The process of argument deduction fully determines the parameter types of the function template specializations, i.e., the parameters of function template specializations contain no template parameter types. Therefore, except where specified otherwise, function template specializations and non-template functions (8.3.5) are treated equivalently for the remainder of overload resolution.

    Consequently, the template member overloaded operator hasn't have greater priority in terms of overload resolution picking than the template free overloaded operator.

    Even if it had GCC picks the free template overloaded operator LIVE DEMO.

    So in my humble opinion, here is GCC that shows a non standard compliant behaviour and Clang rightfully complains about overload resolution ambiguity.


    Also, is there a workaround so that the operator in comma_guard will always be preferred?


    Answer: Yes although kinda ugly: (comma_guard().operator,(foo())); LIVE DEMO

    0 讨论(0)
提交回复
热议问题