<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Operator overload matching failure with enum as first operand"
   href="https://bugs.llvm.org/show_bug.cgi?id=40329">40329</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Operator overload matching failure with enum as first operand
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>Kevin.Bracey@arm.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=21334" name="attach_21334" title="Compilable example">attachment 21334</a> <a href="attachment.cgi?id=21334&action=edit" title="Compilable example">[details]</a></span>
Compilable example

The attached code using a C++98-compatible "SafeEnum" compiles and "works" with
all other compilers tried than clang. I say "works" because it isn't really
"safe", but it at least compiles.

In summary, we have

    template<typename Target, typename LayoutType = unsigned int>
    struct SafeEnum {
        /**
         * Type of the representation.
         */
        typedef LayoutType representation_t;

    /**
     * Construction of an enumeration value.
     */
        SafeEnum(LayoutType value) : _value(value) { }

        friend bool operator!=(SafeEnum lhs, SafeEnum rhs);
    };

    struct hci_error_code_t : SafeEnum<hci_error_code_t, uint8_t> {
        enum type {
            SUCCESS = 0x00
        };
        hci_error_code_t(type value);
        explicit hci_error_code_t(uint8_t raw_value);
    };

    hci_error_code_t hci_status;

Then, all compilers will accept 

    if (4 != hci_status) // so much for type safety
or 
    if (hci_status != hci_error_code_t::SUCCESS)


But clang alone will not accept

    if (hci_error_code_t::SUCCESS != hci_status)

It seems the overload using the argument conversion from hci_error_code_t::type
-> uint8_t -> SafeEnum<hci_error_code_t, uint8_t> isn't found if it's the first
argument, but is if it's the second.

Clearly the SafeEnum class doesn't do what is intended, and can be replaced by
`enum class`, but it seems something is off in clang's overload resolution - I
can find no reason in why argument order should matter here.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>