friend

Issue with friend template functions clang++ / msvc++ and enable_if

 ̄綄美尐妖づ 提交于 2019-12-06 10:48:11
I get a compiler error in clang++. MSVC++ is happy. I believe my declarations are correct. Am I incorrect in my beliefs and I am "lucky" in MSVC? Is there a non #ifndef _MSC_VER ... public: way to make this work in both compilers? I'd like to keep the constructor private. The real code is slightly more complex. (additional template meta-programming and "perfect forwarding") The following is a distilled version for the question and to isolate the issue as much as possible. I've tried a number of variations for the friend declaration. The one that "seems best" is shown in the example. #include

Symfony Doctrine entity friend hasFriend followers

[亡魂溺海] 提交于 2019-12-06 10:34:15
问题 I need to set follower, following (myFriends) hasFriend logic to my project. column "odobera" means "following" to example nick(id user) odobera (following to this user). User (25) is following user(37). Requests table: User entity: /** * @ORM\OneToMany(targetEntity="TB\RequestsBundle\Entity\Requests", mappedBy="odobera") */ protected $followers; /** * @ORM\OneToMany(targetEntity="TB\RequestsBundle\Entity\Requests", mappedBy="nick") */ protected $myFriends; public function __construct() {

Why does friend function found successfully via ADL

限于喜欢 提交于 2019-12-06 09:40:30
问题 Consider the following code: #include <stdio.h> class A { public: friend void foo(A a){ printf("3\n"); } }; int main() { foo(A()); } It works. But I thought that this code is invalid. It is because 3.4.1/3: For purposes of determining (during parsing) whether an expression is a postfix-expression for a function call, the usual name lookup rules apply. Usual name lookup rules could not find the friend function because name declared by friend is invisible in the global namespace in my case.

友元

不羁岁月 提交于 2019-12-06 08:50:18
友元 c++控制对类私有部分的访问。 友元有3种。 友元函数 例子 A = 2.34 * B; 非成员函数不是由对象调用,它使用所有值都是显式参数。 Time operator*(double m, const Time & t); 但有时候非成员函数不可以调用成员数据。然而有一种特殊的非成员函数可以访问类的私有成员,他们被称为友元函数。 友元类 友元成员函数 通过让函数成为类的友元,可以赋予该函数与类的成员函数相同的访问权限。 创建友元函数 创建友元函数的第一步是将其原型放在类声明中,并在原型声明前加上关键字friend。 friend Time operator*(double m, const Time& t); 上述原型意味着一下两点。 operator*()函数是在类声明中声明的,但他不是成员函数,因此不能使用成员运算符来调用。 operator*()不是成员函数,但它与成员函数访问权限相同。 第二步是编写函数定义。 因为它不是成员函数,所以不要使用Time::限定符。另外不要再定义函数头使用friend。 当函数定义也是原型时,需要加friend关键字。 常用的友元 重载<<运算符 第一个重载版本 因为第一个操作数是ostream类对象cout,所以需要使用友元函数。 而且没有使用到ostream中的私有部分,所以只需要作为Time类的友元函数。 在类中声明friend

private inheritance, friends, and exception-handling

徘徊边缘 提交于 2019-12-06 06:13:28
问题 When class A privately inherits from class B it means that B is a private base class subobject of A. But not for friends, for friends it is a public sububject. And when there are multiple catch handlers the first one that matches (that is, if the exception type can be implicitly converted to the handler's parameter type) is called. So will anyone explain to me why the following code does not work as I expect? Is this behavior intended by the standard or is this a MSVC bug? class A { }; class

What is the right way to write friend function declarations in template class?

橙三吉。 提交于 2019-12-06 05:17:51
问题 I'm trying to write my own vector template class, but I have some problems when writing friend function declarations. At first I wrote like this: template <typename T, typename Alloc = std::allocator<T>> class vector { public: friend bool operator==(const vector<T, Alloc>&, const vector<T, Alloc>&); }; But the compiler reports a warning that I declare a non-template function. So I changed the friend declaration to this: template <typename T, typename Alloc = std::allocator<T>> class vector {

Friend class not working

时光总嘲笑我的痴心妄想 提交于 2019-12-06 00:05:25
问题 I am getting the typical '... is private within this context' error. Can you tell me what I am doing wrong? Code is shortened for readability. in class SceneEditorWidgetController: (settingsdialog and the variable used here is defined in the header) SceneEditorPluginWidgetController::SceneEditorPluginWidgetController() { } void SceneEditorPluginWidgetController::configured() { priorKnowledge_setting = settingsDialog->priorKnowledgeProxyFinder->getSelectedProxyName().toStdString(); //This is

Name which introduced by friend declaration

南楼画角 提交于 2019-12-05 18:39:02
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? It can be found by Argument Dependent Lookup, but only that way. E.g. you can implement a comparison operator that way: struct Point { int x, y; friend auto

What is the point of the complicated scoping rules for friend declarations?

随声附和 提交于 2019-12-05 16:30:18
问题 I recently discovered that friend declarations scoping follows extremely peculiar rules - if you have a friend declaration (definition) for a function or a class that is not already declared, it is automatically declared (defined) in the immediately enclosing namespace, but it is invisible to non-qualified and qualified lookup; however, friend function declarations remain visible through argument-dependent lookup. struct M { friend void foo(); friend void bar(M); }; void baz() { foo(); //

class friend function inside a namespace

懵懂的女人 提交于 2019-12-05 16:11:42
问题 Im trying to define a class friend function outside the namespace like this: namespace A{ class window{ private: int a; friend void f(window); }; } void f(A::window rhs){ cout << rhs.a << endl; } Im getting an error said that there is ambiguity. and there is two candidates void A::f(A::window); and void f(A::window) . So my question is : 1) How to make the global function void f(A::window rhs) a friend of the class A::window. EDIT: (After reading the answers) 2) why do I need to qualify the