friend

Using variadic templates to specify friend classes

亡梦爱人 提交于 2019-12-03 15:44:34
I'm trying to use variadic templates to specify friend classes. I try with the following syntax, but it doesn't work. template <class... Args> struct A { friend Args...; }; I try to code some workarounds, but it seems to be not so simple since the friendship is not transitive and inherited. So the question is if there is a correct syntax or any workaround to make each individual class in Args be a friend of A? Maybe the following CRTP variant would be sufficient for your use: template<typename Arg> class Abase { friend Arg; virtual int foo(int) = 0; // this is the private interface you want to

When should you use friend classes? [duplicate]

最后都变了- 提交于 2019-12-03 13:36:13
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: When should you use 'friend' in C++? I have come to a stumbling block because of lack of documentation on friend classes. Most books just explain it briefly, e.g an excerpt from C++: the Complete Reference : Friend Classes are seldom used. They are supported to allow certain special case situations to be handled. And frankly, I have never seen a friend class in any good code made by an experienced C++ programmer

Grant access to private constructor without friends?

拜拜、爱过 提交于 2019-12-03 12:52:05
I am working on some code, where I encountered a situation similar to this one: struct Bar; struct Foo{ friend struct Bar; private: Foo(){} void f(){} void g(){} }; struct Bar { Foo* f; Bar() { f = new Foo();} ~Bar() { delete f;} }; int main(){ Bar b; } I would prefer to have Bar not as friend of Foo , because besides Foo s constructor Bar does not need access to any of Foo s private methods (and thus should not have access). Is there a way to allow only Bar to create Foo s without making them friends? PS : realized that the question might not be 100% clear. I don't mind if it is via friends

Template friend function and return type deduction

不羁的心 提交于 2019-12-03 12:41:45
Note: This question is really close to Return type deduction for in-class friend functions , but I did not find the answer to my problem there. Tested with clang 3.4 with std=c++1y and clang 3.5 with std=c++14 and std=c++1z This code compiles: #include <iostream> template<class T> class MyClass { public: MyClass(T const& a) : impl(a) {} template<class T0, class T1> friend auto // requires operator+(T0,T1) exists operator+(MyClass<T0> const& a, MyClass<T1> const& b) { return MyClass<decltype(a.impl+b.impl)>{a.impl + b.impl}; } T getImpl() const { return impl; } private: T impl; }; int main() {

How to propagate friend for derived classes

六眼飞鱼酱① 提交于 2019-12-03 11:45:13
问题 I want to have a class hierarchy and be able to create objects from it only inside a Factory. Example: class Base { protected: Base(){}; virtual void Init(){}; friend class Factory; }; class SomeClass : public Base { public://I want protected here! Now it's possible to call new SomeClass from anywhere! SomeClass(){}; void Init(){}; }; class Factory { public: template<class T> T* Get() { T* obj = new T(); obj->Init(); return obj; } }; int main() { Factory factory; SomeClass *obj = factory.Get

Friend function is unable to construct a unique pointer of the class

♀尐吖头ヾ 提交于 2019-12-03 10:48:31
I have a certain design strategy where the constructor of my class is private and can only be constructed by friends of the class. Inside the friend function, I am trying to create a unique_pointer of my class using std::make_unique but it doesn't compile. My VC12 compiler complains c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(1639): error C2248: 'Spam::Spam' : cannot access private member declared in class 'Spam' The relevant code which fails during compilation is as follows #include <memory> class Spam { public: friend void Foo(); private: Spam(int mem) :mem(mem) {}

In C++, why isn't it possible to friend a template class member function using the template type of another class?

梦想与她 提交于 2019-12-03 10:47:53
问题 In other words, why does this compile fine : template<typename Type> class A{ public: void f(); }; class B{ friend void A<int>::f(); }; template<> void A<int>::f(){ B* var = new B(); } While this doesn't : template<typename Type> class A{ public: void f(); }; template<typename Type> // B is now a templated class class B{ friend void A<Type>::f(); // Friending is done using B templated type }; template<> void A<int>::f(){ B<int>* var = new B<int>(); // var is now declared using int as its

Why does the Standard prohibit friend declarations of partial specializations?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 10:20:00
The C++ standard prohibits friend declarations of partial specializations. (§14.5.3/8): Friend declarations shall not declare partial specializations. [Example: template<class T> class A { }; class X { template <class T> friend class A<T*>; //error }; --end example] Other questions, e.g. this one , have received answers that invoke this prohibition, but I would like to know the rationale. I don't see it and can't find it with my favourite search engine. I can find however that it goes right back to the C++98 standard, so presumably the rationale is quite basic and clear. Can someone explain it

Why can't a PRIVATE member function be a friend function of another class?

三世轮回 提交于 2019-12-03 08:39:55
问题 class x { void xx() {} }; class y { friend void x::xx(); }; This results in an error like error: friend function 'xx' is a private member of 'x' Why can't I declare a private member function to be a friend of another class? 回答1: [class.friend]/9: A name nominated by a friend declaration shall be accessible in the scope of the class containing the friend declaration. The reason is quite simple; private members shall obey a clear and definite rule: A member of a class can be private ; that is,

How to make google-test classes friends with my classes?

空扰寡人 提交于 2019-12-03 07:28:14
问题 I heard there is a possibility to enable google-test TestCase classes friends to my classes, thus enabling tests to access my private/protected members. How to accomplish that? 回答1: Try this (straight from Google Test docs...): FRIEND_TEST(TestCaseName, TestName); For example: // foo.h #include <gtest/gtest_prod.h> // Defines FRIEND_TEST. class Foo { ... private: FRIEND_TEST(FooTest, BarReturnsZeroOnNull); int Bar(void* x); }; // foo_test.cc ... TEST(FooTest, BarReturnsZeroOnNull) { Foo foo;