friend

Is it possible to friend a class in an anonymous namespace in C++?

走远了吗. 提交于 2020-12-29 02:44:02
问题 I am porting code from Java to c++ and I'd like to replicate some anonymous functionalities. In file A.h I have : class A { private: int a; class AnonClass; friend class AnonClass; }; In file A.cpp I have : namespace { class AnonClass { public: AnonClass(A* parent) { parent->a = 0; // This doesn't work, a is not accessible } } } Is it possible to friend a class in an anonymous namespace in C++? In Java you can declare anonymous classes so it would be very similar. Also it would not expose

Is it possible to friend a class in an anonymous namespace in C++?

风格不统一 提交于 2020-12-29 02:41:13
问题 I am porting code from Java to c++ and I'd like to replicate some anonymous functionalities. In file A.h I have : class A { private: int a; class AnonClass; friend class AnonClass; }; In file A.cpp I have : namespace { class AnonClass { public: AnonClass(A* parent) { parent->a = 0; // This doesn't work, a is not accessible } } } Is it possible to friend a class in an anonymous namespace in C++? In Java you can declare anonymous classes so it would be very similar. Also it would not expose

c++ implementing friend/inline functions

瘦欲@ 提交于 2020-12-08 05:57:08
问题 I can't seem to find the answer to this newbie question. If I have a class // Header file (.h) Class X { public: friend bool operator==(const X&, const X&); inline size_type rows() const; }; etc... when I go to implement the .cpp file of X, should I include the words inline & friend in the function names in the .cpp file. ie, should I implement my file similar to the below // CPP file (.cpp) #include "X.h" friend bool operator==(const X&, const X&) { //implementation goes here //return true

Does an explicit instantiation declaration of a member function of a class template cause instantiation of the class template?

穿精又带淫゛_ 提交于 2020-12-06 16:00:14
问题 [dcl.spec.auto]/14 states [ emphasis mine]: An explicit instantiation declaration does not cause the instantiation of an entity declared using a placeholder type , but it also does not prevent that entity from being instantiated as needed to determine its type. [  Example: template <typename T> auto f(T t) { return t; } extern template auto f(int); // does not instantiate f<int> int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit // instantiation definition

Does an explicit instantiation declaration of a member function of a class template cause instantiation of the class template?

浪子不回头ぞ 提交于 2020-12-06 15:55:26
问题 [dcl.spec.auto]/14 states [ emphasis mine]: An explicit instantiation declaration does not cause the instantiation of an entity declared using a placeholder type , but it also does not prevent that entity from being instantiated as needed to determine its type. [  Example: template <typename T> auto f(T t) { return t; } extern template auto f(int); // does not instantiate f<int> int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit // instantiation definition

Does an explicit instantiation declaration of a member function of a class template cause instantiation of the class template?

孤者浪人 提交于 2020-12-06 15:52:14
问题 [dcl.spec.auto]/14 states [ emphasis mine]: An explicit instantiation declaration does not cause the instantiation of an entity declared using a placeholder type , but it also does not prevent that entity from being instantiated as needed to determine its type. [  Example: template <typename T> auto f(T t) { return t; } extern template auto f(int); // does not instantiate f<int> int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit // instantiation definition

Is there a way to specify all classes in a variadic parameter pack to be friend of the template in order to use operator=?

*爱你&永不变心* 提交于 2020-12-02 17:05:01
问题 I have seen a CRTP solution, which extracted the interface into the base class, and friended only one of the pack arguments per base class. Then the most derived class inherited all the friended base classes and implemented the interface. I cannot use this approach, since I need to protect the assignment operator, which is not inherited. Also, since the assignment operator has a defined signature with exactly one parameter, I cannot use a key pattern. This is what I would like to have:

Is there a way to specify all classes in a variadic parameter pack to be friend of the template in order to use operator=?

余生长醉 提交于 2020-12-02 17:04:19
问题 I have seen a CRTP solution, which extracted the interface into the base class, and friended only one of the pack arguments per base class. Then the most derived class inherited all the friended base classes and implemented the interface. I cannot use this approach, since I need to protect the assignment operator, which is not inherited. Also, since the assignment operator has a defined signature with exactly one parameter, I cannot use a key pattern. This is what I would like to have:

Is there a way to specify all classes in a variadic parameter pack to be friend of the template in order to use operator=?

心已入冬 提交于 2020-12-02 17:02:26
问题 I have seen a CRTP solution, which extracted the interface into the base class, and friended only one of the pack arguments per base class. Then the most derived class inherited all the friended base classes and implemented the interface. I cannot use this approach, since I need to protect the assignment operator, which is not inherited. Also, since the assignment operator has a defined signature with exactly one parameter, I cannot use a key pattern. This is what I would like to have: