Custom implicit enumeration class conversion when invoking an overloaded statement is not performed

Consider the following example:

struct ConvertibleStruct {}; enum class ConvertibleEC {}; struct Target { // Implicit conversion constructors Target(ConvertibleStruct) {} Target(ConvertibleEC) {} }; Target operator~(const Target& t) { return t; } Target anotherFunction(const Target& t) { return t; } int main() { ConvertibleStruct t; ConvertibleEC ec; ~t; // 1. Works finding the operator overloaded above ~ec; // 2. Fails to compile on clang 3.4 and gcc 4.8.2 operator~(ec); // 3. Works finding the operator overloaded above anotherFunction(ec); // 4. Works } 

Compiler Version:

The above findings apply to clang 3.4 and gcc 4.8.2 . Test 2. really compiles on gcc 4.7.3 with -std=c++11 . Perhaps a bug in the early implementation of GCC C ++ 11?

Statements:

  • Given that 1. compiles, user implicit conversions are checked when the ~ operator is called.
  • Given that 4. compiles, custom implicit conversions are checked for enum class objects.

Questions:

  • Are the above statements correct?
  • If so, why 2. can’t compile?
  • Given that 2. fails to compile, why 3. compile?
+5
source share
1 answer

In the second ~ec test, a feature of searching for names for operators in expressions arises: [over.match.oper] / 3 (from the "ancient" N3797):

For a unary operator @ with an operand of type whose cv-unqualified version is T1 [...]

The set of non-member candidates is the result of an unqualified search operator@ in the context of an expression in accordance with the usual rules for finding names in unqualified function calls, except that all member functions are ignored. However, if no operand has a class type, only those functions that are not members in the search set that have the first parameter of type T1 or "reference to (possibly cv-qualified) T1 " when T1 is the type of the enumeration of the [...] function candidate .

Therefore ::operator~(const Target&) should not be searched / used with a for expressions with a unary operator applied to an operand of type ConvertibleEC .


For the first, ~t , the operand has a class type, and the above exception does not apply.

Both the third and fourth tests do not use operator search, but a regular unskilled search. A regular unskilled search finds ::operator~(const Target&) (in cases 1 and 3) and anotherFunction (in case 4).

+4
source

Source: https://habr.com/ru/post/1208293/


All Articles