- Использование Template - Compiles без выпуска < /p>
Код: Выделить всё
#include #include using namespace std; template class Test; template bool operator==(const Test &, const Test &); template class Test { friend bool operator==(const Test &, const Test &); private: int data; }; template bool operator==(const Test &lhs, const Test &rhs) { return lhs->data == rhs->data; // clause 1. This seems to compile even though lhs/rhs is not a pointer. The arrow operator would be something like (*lhs).data. But in this case lhs/rhs is a reference. } int main() { return 0; } < /code> < /li> Без шаблона - компилируется с выпуском < /p> #include #include using namespace std; class Test { friend bool operator==(const Test&, const Test&); public: int data; }; bool operator==(const Test&lhs, const Test&rhs) { return lhs->data == rhs->data; // clause 2. Now this fail which is expected. //return lhs.data == rhs.data; // clause 3. This would work as expected. } int main() { return 0; }
Подробнее здесь: https://stackoverflow.com/questions/794 ... r-from-inv