简体   繁体   中英

Implicit conversion and operator overloading

I have the following code:

struct helper {
    template<typename T> helper(T const&);
};

helper operator*(helper const&);

struct A {};

int main() {
    // (1)
    A a;
    sizeof(*a);

    // (2)
    int i;
    sizeof(*i);
}

Case (1) compiles fine and I understand that it is using the implicit conversion to the helper type and the given operator overload.

For case (2), however, I get a compiler error:

invalid type argument of unary '*' (have 'int')

Why is the implicit conversion used for type A but not for int ?

When no user defined type is involved, any operator is assumed to be a built-in operator. So

helper operator*(helper const&);

cannot be found for *i when i is of built-in type (such as int ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM