Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it called "implicit casting" or "implicit conversion"?

Tags:

c++

types

casting

Even though I see the term implicit casting used widely in C++ tutorials to denote the fact that when you assign some type to another type the conversion of types will be done automatically (implicitly), but I often hear that it should be called implicit conversion and not implicit casting. Can anyone tell me the difference between the two?


1 Answers

It should normally be called implicit conversion.

Just about the only time you might see somebody reasonably talk about an "implicit cast" would be when talking about a cast operator in a class. For example:

class T {
    int x;
public:
    T (int x) : x(x) {}
    operator int() { return x; }
};

Some people call this a cast operator, and this is one that can be implicitly invoked. As of C++11, you can add explicit to it:

class T {
    int x;
public:
    T (int x) : x(x) {}
    explicit operator int() { return x; }
};

...to prevent implicit invocation. For example, this means:

T t(10);
int x = t; // works with the first version, not the second
int y = static_cast<int>(t); // works with either version

So, if somebody were comparing/contrasting these two, it could sort of make sense for them to refer to the first one as an "implicit cast operator" (or something like that) to differentiate it from the second one.

like image 135
Jerry Coffin Avatar answered May 09 '26 16:05

Jerry Coffin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!