friend-function

Template friend function of a template class

我的未来我决定 提交于 2019-11-28 21:41:55
I was struggling with the issue described in this question (declaring a template function as a friend of a template class), and I believe the 2nd answer is what I want to do (forward declare the template function, then name a specialization as a friend). I have a question about whether a slightly different solution is actually correct or just happens to work in Visual C++ 2008. Test code is: #include <iostream> // forward declarations template <typename T> class test; template <typename T> std::ostream& operator<<(std::ostream &out, const test<T> &t); template <typename T> class test { friend

How to make std::make_unique a friend of my class

蹲街弑〆低调 提交于 2019-11-28 10:42:39
I want to declare std::make_unique function as a friend of my class. The reason is that I want to declare my constructor protected and provide an alternative method of creating the object using unique_ptr . Here is a sample code: #include <memory> template <typename T> class A { public: // Somehow I want to declare make_unique as a friend friend std::unique_ptr<A<T>> std::make_unique<A<T>>(); static std::unique_ptr<A> CreateA(T x) { //return std::unique_ptr<A>(new A(x)); // works return std::make_unique<A>(x); // doesn't work } protected: A(T x) { (void)x; } }; int main() { std::unique_ptr<A

Template friend function of a template class

本秂侑毒 提交于 2019-11-27 14:16:34
问题 I was struggling with the issue described in this question (declaring a template function as a friend of a template class), and I believe the 2nd answer is what I want to do (forward declare the template function, then name a specialization as a friend). I have a question about whether a slightly different solution is actually correct or just happens to work in Visual C++ 2008. Test code is: #include <iostream> // forward declarations template <typename T> class test; template <typename T>

c++ error C2662 cannot convert 'this' pointer from 'const Type' to 'Type &'

你说的曾经没有我的故事 提交于 2019-11-27 11:47:05
问题 I am trying to overload the c++ operator== but im getting some errors... error C2662: 'CombatEvent::getType' : cannot convert 'this' pointer from 'const CombatEvent' to 'CombatEvent &' this error is at this line if (lhs.getType() == rhs.getType()) see the code bellow: class CombatEvent { public: CombatEvent(void); ~CombatEvent(void); enum CombatEventType { AttackingType, ... LowResourcesType }; CombatEventType getType(); BaseAgent* getAgent(); friend bool operator<(const CombatEvent& lhs,

Friend function declaration/definition inside a namespace

守給你的承諾、 提交于 2019-11-27 07:18:37
问题 Consider a class inside a namespace. The definition of the class declares a friend function. namespace Foo { class Bar { friend void baz(); }; } This should, based on what I know, declare baz() as a member of the innermost enclosing namespace, i.e. Foo . Therefore, I expected the following definition for baz() to be correct: void Foo::baz() { } However, GCC (4.7) gives me an error. error: ‘void Foo::baz()’ should have been declared inside ‘Foo’ Several solutions seem to work: Declare baz()

How to make std::make_unique a friend of my class

独自空忆成欢 提交于 2019-11-27 03:45:51
问题 I want to declare std::make_unique function as a friend of my class. The reason is that I want to declare my constructor protected and provide an alternative method of creating the object using unique_ptr . Here is a sample code: #include <memory> template <typename T> class A { public: // Somehow I want to declare make_unique as a friend friend std::unique_ptr<A<T>> std::make_unique<A<T>>(); static std::unique_ptr<A> CreateA(T x) { //return std::unique_ptr<A>(new A(x)); // works return std:

Operator overloading : member function vs. non-member function?

空扰寡人 提交于 2019-11-25 22:44:28
问题 I read that an overloaded operator declared as member function is asymmetric because it can have only one parameter and the other parameter passed automatically is the this pointer. So no standard exists to compare them. On the other hand, overloaded operator declared as a friend is symmetric because we pass two arguments of the same type and hence, they can be compared. My question is that when i can still compare a pointer\'s lvalue to a reference, why are friends preferred? (using an