c++ - How to declare friend function of non-member function declared in a class by friend keywork -
i have 2 questions friend declaration in following code. (1) declared friend function. program looks working got warning compiler. original function non-member function defined in class friend declaration. need care of it? or have problem? (2) if possible, declare friendship template class functions owns non-member function. tried declare friend ship class failed. please give me solution or suggestions. thank much.
#include <vector> template <typename derived, typename t> class function0 { public: friend bool operator== (derived& org, derived& cmp) { // ***** line gives warning derived& org_value = static_cast<derived&>(org); derived& cmp_value = static_cast<derived&>(cmp); return *(org_value.value_) == *(cmp_value.value_); } }; template <typename t, template <typename derived, typename t_t> class functions> class pointer : public functions<pointer<t,functions>, t> { public: pointer() {}; pointer(t* new_value) : value_(new_value) {}; bool operator== (pointer& cmp) = delete; // ***** line gives warning virtual ~pointer() { }; private: // friend class functions<pointer<t,functions>, t>; // *** did not work. friend bool operator== (pointer<t,functions>& org, pointer<t,functions>& cmp); // *** looks work gave me warning t* value_ = nullptr; }; class testa { public: testa(unsigned int id) : id_(id) {}; virtual ~testa() {}; unsigned int id(void) { return id_; } bool operator== (testa& cmp) { return (id_ == cmp.id()) ? true : false; } private: unsigned int id_ = 0; }; template <typename element> element findcorrespondingfirst(element& obj, std::vector<element>& vec) { (unsigned int = 0; < vec.size(); ++i) { auto o = vec[i]; if (obj == o) { // dispatches error massage return o; } } return element(); } void test_pointer_class(void) { std::vector<pointer<testa, function0>> ptr_vector; testa* raw_ptr0 = new testa(1); testa* raw_ptr1 = nullptr; testa* raw_ptr2 = new testa(2); pointer<testa, function0> ptr0 = pointer<testa, function0>(raw_ptr0); pointer<testa, function0> ptr1 = pointer<testa, function0>(raw_ptr1); pointer<testa, function0> ptr2 = pointer<testa, function0>(raw_ptr2); testa* raw_ptr3 = new testa(1); pointer<testa, function0> ptr3 = pointer<testa, function0>(raw_ptr3); ptr_vector.push_back(ptr0); ptr_vector.push_back(ptr1); ptr_vector.push_back(ptr2); ptr_vector.push_back(ptr3); auto result1 = findcorrespondingfirst(ptr3, ptr_vector); delete raw_ptr0; delete raw_ptr1; delete raw_ptr2; delete raw_ptr3; }
the warning message gcc following,
friend declaration ‘bool operator==(pointer<t, functions>&, pointer<t, functions>&)’ declares non-template function -[wnon-template-friend]
Comments
Post a Comment