friend

g++ 4.5 can't find a friend function

廉价感情. 提交于 2019-12-02 00:35:48
问题 G'day! I have a question around the use of friend in C++. Consider the following piece of code: #include <ostream> struct F { }; struct N { friend std::ostream& operator<< (std::ostream&, const N&); friend std::ostream& operator<< (std::ostream&, const F&); }; void foo(std::ostream &out) { F bar; out << bar; } My understanding always was, that friend is similar to static with the additional property that the friend function has access to the private part of the class. Under that assumption,

C++ friend class std::vector

做~自己de王妃 提交于 2019-12-01 23:41:33
问题 Is it possible to do the following portably: struct structure { structure() {} private: // only allow container copy construct structure(const structure&) {} // in general, does not work because allocator (not vector) calls copy construct friend class std::vector<structure>; }; example message trying to compile above: In member function void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = kernel_data<const double*>::block]: ... /usr/include/c++/4.3/ext/new_allocator.h

C++ friend class std::vector

自作多情 提交于 2019-12-01 22:13:00
Is it possible to do the following portably: struct structure { structure() {} private: // only allow container copy construct structure(const structure&) {} // in general, does not work because allocator (not vector) calls copy construct friend class std::vector<structure>; }; example message trying to compile above: In member function void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = kernel_data<const double*>::block]: ... /usr/include/c++/4.3/ext/new_allocator.h:108: error: within this context Thanks I do have workaround, but I am curious as how this could be

g++ 4.5 can't find a friend function

笑着哭i 提交于 2019-12-01 22:12:15
G'day! I have a question around the use of friend in C++. Consider the following piece of code: #include <ostream> struct F { }; struct N { friend std::ostream& operator<< (std::ostream&, const N&); friend std::ostream& operator<< (std::ostream&, const F&); }; void foo(std::ostream &out) { F bar; out << bar; } My understanding always was, that friend is similar to static with the additional property that the friend function has access to the private part of the class. Under that assumption, the code should compile, since there is an operator<< that takes an ostream& and a (const) F& . It

templated friend function lookup

拥有回忆 提交于 2019-12-01 20:09:59
The following simple code compiles fine class A { int x[3]; public: A() { x[0]=1; x[1]=2; x[2]=3; } friend int const&at(A const&a, unsigned i) noexcept { return a.x[i]; } friend int foo(A const&a, unsigned i) noexcept { int tmp = at(a,i); return tmp*tmp; } }; but if the friends are made templates class A { int x[3]; public: A() { x[0]=1; x[1]=2; x[2]=3; } template<unsigned I> friend int const&at(A const&a) noexcept { static_assert(I<3,"array boundary exceeded"); return a.x[I]; } template<unsigned I> friend int foo(A const&a) noexcept { int tmp = at<I>(a); // <- error: use of undeclared

Can friend class be declared conditionally in C++03?

元气小坏坏 提交于 2019-12-01 18:36:41
I want to declare a friend class only if some (compile-time) condition is true. For example: // pseudo-C++ class Foo { if(some_compile_time_condition) { friend class Bar; } }; I did not find any solution on the internet. I went through all the answers to the question Generating Structures dynamically at compile time . Many of them use the C++11 std::conditional , but I would like to know if it is possible to do this in C++03 without using the preprocessor . This solution https://stackoverflow.com/a/11376710/252576 will not work because friend ship is not inherited ( friend class with

partial template specialization for friend classes?

回眸只為那壹抹淺笑 提交于 2019-12-01 17:57:34
I have a simple class X, and set of templatized classes Y<T,U>. I'd like all classes Y where the first templatization parameter happens to be X to be a friend of X itself. The following hopefully conveys what I want, but the friend statement gives a compile error. template<typename T, typename U> class Y { }; class X { public: X(int value) : i(value) {} const int& getI() const { return i; } private: int i; template<class U> friend class Y<X,U>; }; I'm not sure templatization of friend statements is allowed at all (let alone partial templatization of friend statements). Is there a way to do

partial template specialization for friend classes?

泪湿孤枕 提交于 2019-12-01 17:35:57
问题 I have a simple class X, and set of templatized classes Y<T,U>. I'd like all classes Y where the first templatization parameter happens to be X to be a friend of X itself. The following hopefully conveys what I want, but the friend statement gives a compile error. template<typename T, typename U> class Y { }; class X { public: X(int value) : i(value) {} const int& getI() const { return i; } private: int i; template<class U> friend class Y<X,U>; }; I'm not sure templatization of friend

friend functions of a class inside a namespace

落爺英雄遲暮 提交于 2019-12-01 17:08:02
问题 I've two question about this code bellow: namespace A { class window; } void f(A::window); namespace A { class window { private: int a; friend void ::f(window); }; } void f(A::window rhs) { std::cout << rhs.a << std::endl; } 1) why do I need to qualify the member function f inside window class to be global by doing ::f(window) ? 2) why do I need to predeclare the function f(A::window) in this particular case, whereas when the class is not defined inside a namespace it's ok for the function to

recursive friend classes

◇◆丶佛笑我妖孽 提交于 2019-12-01 17:07:44
Is there any way around this: class B; class C { public: C() { } private: int i; friend B::B(); }; class B { public: B() { } private: int i; friend C::C(); }; Gives error: prog.cpp:8: error: invalid use of incomplete type ‘struct B’ prog.cpp:1: error: forward declaration of ‘struct B’ You just can't do this. Remove the circular dependency. According to IBM's documentation (which I realize is not normative): A class Y must be defined before any member of Y can be declared a friend of another class. So I think the answer is "no". Of course, you can use friend class B; ...instead of friend B::B()