Reputation: 3430
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() {
}
Upvotes: 3
Views: 121
Reputation: 103751
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.
Upvotes: 4
Reputation: 907
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.
Upvotes: 1