friend-function Questions

2

Solved

Apparently today, MSVC is trying its best to convince me to switch to clang. But I won't give up. Earlier I asked this question wondering how to declare std::make_unique as a friend of my class. I...
Chemoreceptor asked 24/11, 2015 at 23:25

2

Solved

The following code snippet: struct a { [[nodiscard]] friend int b(); }; Produces this error when compiling on clang++ (trunk 342102) with -std=c++17: <source>:3:5: error: an attribute li...
Smyth asked 14/9, 2018 at 12:37

1

Solved

According to the standard, friend function declared and defined in class can only be find by ADL. So, I think the following code should compile. template<int M> struct test{ template<int...
Photoluminescence asked 20/6, 2016 at 2:19

1

Solved

Following the Czech song from Eurovision 2019 in Tel-Aviv It is known that in C++ a friend of a friend is not (automatically) a friend. Clang however differs on the following code with GCC and MSVC...
Swashbuckling asked 28/5, 2021 at 15:26

4

Solved

I am trying to overload the c++ operator== but im getting some errors... error C2662: 'CombatEvent::getType' : cannot convert 'this' pointer from 'const CombatEvent' to 'CombatEvent &' this ...
Jointed asked 22/8, 2012 at 7:33

1

I have found a situation where code compiles successfully under C++17, but FAILS under C++20. This blocks us from upgrading our existing code design to the newer standard. Why doesn't this compile ...
Decretive asked 18/3, 2021 at 4:2

2

Solved

Consider the following class template, which contains two (hidden) friend declarations of the same friend (same function type; see below), which also defines the friend (and the friend is thus inli...
Erland asked 9/11, 2020 at 20:19

3

Solved

I don't understand why the following does not compile (e.g. in gcc 9.10 or MS VS C++ 2019): class X { public: friend bool operator==(int, X const &); }; int main() { 2 == X(); // ok... sta...
Principium asked 3/8, 2020 at 8:44

4

Solved

I have the following template class and template function which intends to access the class' private data member: #include <iostream> template<class T> class MyVar { int x; }; templ...
Darrel asked 25/5, 2015 at 13:43

2

I understand that the following C++ code snippet should produce an error in the definition of g, because p.t.x is private and cannot be accessed there. class P { class T { int x; friend class P...
Hite asked 15/4, 2020 at 10:2

3

Solved

Consider the following example: struct S { template<typename T = void> friend void foo(S) { } }; int main() { S s; foo(s); // (1) foo<void>(s); // (2) } My GCC 9.2.0 fails to ...
Pithos asked 7/3, 2020 at 18:16

2

Solved

C++ has a feature, that in-class-defined friend functions can only be found by ADL (argument dependent lookup): struct Foo { friend void fn(Foo) { } // fn can only be called by ADL, it won't be f...

1

Solved

TL;DR Before you attempt to read this whole post, know that: a solution to the presented issue has been found by myself, but I'm still eager to know if the analysis is correct; I've packaged the s...

1

Solved

When a derived class inherits from a base class via public access, the question is the same as that in Are friend functions inherited? and why would a base class FRIEND function work on a derived c...
Prow asked 31/12, 2019 at 13:17

0

I'm having the following problem. The code below runs fine on gdb online, however locally compiling like this: /.../g++ -std=c++17 -g -O3 /.../Test.cpp -o /.../Test produces: error: 'pri...
Sprage asked 8/11, 2019 at 8:40

1

Solved

If I have a normal class I can "inject" a non-free friend function inside the class. (That among other things can be only be found by ADL). case 1: class A{ double p_; friend double f(A const&a...

1

Solved

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...
Neopythagoreanism asked 31/8, 2018 at 20:51

0

This is a follow-up on my answer to this question. The original answer I posted does not have any namespaces, and it solves the problem. However, the OP subsequently made an edit claiming that the...
Fright asked 30/7, 2018 at 2:16

2

Solved

I would like to know if what I am aiming for is possible. I have a class Class such that #include<iostream> template<class T> class Class; template<class T, class W> Class<W...
Aftermath asked 27/7, 2018 at 11:2

2

Solved

class baseClass { public: friend int friendFuncReturn(baseClass &obj) { return obj.baseInt; } baseClass(int x) : baseInt(x) {} private: int baseInt; }; class derivedClass : public baseClass...
Retaliate asked 24/11, 2017 at 9:4

4

Solved

I have a struct template A<x> and a + operator with int. #include <iostream> template<int x> struct A{ int a; }; template<int x> int operator+(A<x> a, int b){ retu...
Marketing asked 17/8, 2017 at 4:24

4

Today i have a doubt regarding friend function. Can two classes have same friend function? Say example friend void f1(); declared in class A and class B. Is this possible? If so, can a function f...
Beseech asked 23/8, 2013 at 13:38

2

I was wondering if there is a way such that we make all functions defined within a specific namespace friend with a class? In particular, I have a class, for example: class C { private: // ... ...
Fazio asked 30/12, 2013 at 11:7

4

Solved

Previously I learnt about overloading Operators in C++ as member functions as well as friend functions of a class. Although, I know how to overload operators in C++ using both techniques. But I am ...
Eggshaped asked 29/3, 2017 at 9:15

5

I have a doubt related to friend functions in C++. Friend function is not a member function of the claas and can be invoked directly from the main. So, what difference does it make if we keep the f...
Isis asked 19/6, 2012 at 19:17

© 2022 - 2024 — McMap. All rights reserved.