I am purposely using the dot and arrow operator incorrectly, but I'm confused why it compiles when I decide to make the class a template.
Compiles:
template <class B>
struct Boss {
bool operator==( Boss & other ) {
return this.x == other -> x;
}
};
int main() {
}
Does not compile:
struct Boss {
bool operator==( Boss & other ) {
return this.x == other -> x;
}
};
int main() {
}
Templates are not fully checked for correctness if they are not instantiated. They are only checked for syntax. this.x
, while not semantically correct (because this
is not, and cannot be a type that supports that operation), is still syntactically correct.
It compiles because templates are not checked until you use it. If you try to do something useful in your main()
, it will give you a compile error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With