Kacy
Kacy

Reputation: 3430

Why does this compile when I make the class a template?

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

Answers (2)

Benjamin Lindley
Benjamin Lindley

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

Polentino
Polentino

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

Related Questions