friend

How to define a friend template function of a template class outside of its declaration?

丶灬走出姿态 提交于 2019-12-08 03:13:26
问题 If I define the friend template function inside the template class declaration, as follows, it can compile. #include <iostream> template <typename T> class X { public: X(int i) : n(i) {} private: T n; template <typename U> friend void doStuff(const X<T>& x, const U& u) { std::cout << (x.n + u) << std::endl; } }; int main() { X<int> x(1); doStuff(x, 3); return 0; } But if I move the definition of doStuff() out of the class decalaration, just after the declaration of class X<> , as follows, it

Making a class friend itself

霸气de小男生 提交于 2019-12-07 17:33:15
问题 EDIT: It looks like I'm completely misinformed. Please close this thread. Gah. For the record, the following compiles and works: class ForeverAlone { private: int m_friends; HANDLE m_handle; public: ForeverAlone() { m_handle = CreateThread(NULL, 0, &ForeverAlone::SadThread, reinterpret_cast<void*>(this), 0, NULL); } ~ForeverAlone() { if (m_handle != NULL) CloseHandle(m_handle); } protected: static unsigned long WINAPI SadThread(void* param) { ForeverAlone* thisObject = reinterpret_cast

Name which introduced by friend declaration

[亡魂溺海] 提交于 2019-12-07 16:05:33
问题 I'm considering N3797 working draft. There is a quote from 3.3.1/4 friend declarations (11.3) may introduce a (possibly not visible) name into an enclosing namespace And further in 3.3.2/11 I found Friend declaration refer to function or classes that are member of the nearest enclosing namespace, but they don't introduce new name into that namespace. So the name declared by friend declaration is not visible or doesn't introduce at all? 回答1: It can be found by Argument Dependent Lookup, but

Friend template functions (in non-template classes), C++

无人久伴 提交于 2019-12-07 08:04:10
问题 If I have a non-template (i.e. "normal") class and wish to have a template friend function, how do I write it without causing a compiler error? Here is an example to illustrate what I am trying to do: template <class T> void bar(T* ptr); class MyClass // note that this isn't a template class { private: void foo(); template <class T> friend void bar(T*); // ERROR: compiler gives me all kinds of grief }; template <class T> void bar(T* ptr) { if (ptr) { MyClass obj; obj.foo(); } } I'm using

friend function of std::make_shared() in Visual Studio 2010 (not Boost)

大憨熊 提交于 2019-12-07 06:58:23
问题 how to make friend function of std::make_shared() . I tried: class MyClass{ public: friend std::shared_ptr<MyClass> std::make_shared<MyClass>(); //or //friend std::shared_ptr<MyClass> std::make_shared(); protected: MyClass(); }; but it does not work (i'am using Visual Studio 2010 SP1) 回答1: How about adding a static method to your class: class Foo { public: static shared_ptr<Foo> create() { return std::shared_ptr<Foo>(new Foo); } private: // ... }; Here's a little hackaround: class Foo {

g++ and clang++ different behaviour with friend template function defined inside a template class

ぐ巨炮叔叔 提交于 2019-12-06 21:38:34
问题 Another question of type "who's right between g++ and clang++?" for C++ standard gurus. The following code template <int> struct foo { template <typename> friend void bar () { } }; int main () { foo<0> f0; foo<1> f1; } compile without problem with clang++ (only two "unused variable" warnings) but give a the following error tmp_002-11,14,gcc,clang.cpp: In instantiation of ‘struct foo<1>’: tmp_002-11,14,gcc,clang.cpp:27:12: required from here tmp_002-11,14,gcc,clang.cpp:20:16: error:

Is a friend function template defined in the class available for lookup? clang++ and g++ disagree

99封情书 提交于 2019-12-06 21:24:08
问题 Here is the code: struct foo { template<typename T = void> friend foo f() { return {}; } }; int main() { auto x = f(); // clang++ can't find it, g++ can. } clang++ 3.4 gives: fni2.cpp:8:12: error: use of undeclared identifier 'f' auto x = f(); // clang++ can't find it, g++ can. ^ 1 error generated. g++ 4.9.0 compiles it, but I don't think it should have. This is a related question, but there was no definitive answer. Section 15.4.2/2,4 discuss this, but neither of them say anything to suggest

Is it possible to mark an alias template as a friend?

折月煮酒 提交于 2019-12-06 19:56:28
问题 Imagine we have this code: template <class, class> class Element {}; template <class T> class Util { public: template <class U> using BeFriend = Element<T, U>; }; Is it possible to mark BeFriend as a friend ? (Of Util , or any other class). Edit The "obvious" syntax were tried, but both failed with Clang 3.6. template <class> friend class BeFriend; template <class> friend BeFriend; I did not know about the second syntax, but found it in this answer. It seems to work (and be required) for non

Declaring main as friend considered harmful?

女生的网名这么多〃 提交于 2019-12-06 18:18:12
问题 Discussion I know that main can be a friend of a class : #include <iostream> class foo { friend int main(); int i = 4; }; int main() { foo obj; std::cout << obj.i << std::endl; } LIVE DEMO However, I feel that although this is perfectably allowable it conceals many dangers. Questions Are there any valuable uses in making main a friend of a class? Are there any reasons that declaring main as friend of a class should be considered harmful? 回答1: The choice whether to use or avoid a legal feature

why does g++ need both definitions of template class and its friend function?

余生颓废 提交于 2019-12-06 11:20:28
I want to write a friend function for a template class. In visual studio, I can ignore the pre-definition both. But in g++, it is mandatory. Why? #include <iostream> using namespace std; // g++ needs, vs do not needs template <class T> class A; template <class T> ostream & operator<<(ostream & c, const A<T> & v); //- end of g++ needs template <class T> class A { T _v; public: A() {} A(T v) : _v(v) {} friend ostream & operator<<<T>(ostream & c, const A<T> & v); }; template <class T> ostream & operator<<(ostream & c, const A<T> & v) { c << v._v; return c; } Because friend ostream & operator<<<T>