CppCon 2018: Dan Saks “Making New Friends”

CppCon · 10K views · 2018 · 1:01:32

Open on YouTube

C, C++, Rust, Go & systems languagesProgramming language designtalk

Why Stack Overflow recommends it

Stack Overflow users link it when explaining hidden friends, templated operators and why non-member friend functions of class templates behave the way they do.

Recommended by 1 people across 1 different months, 2018 to 2024.

What it is

A CppCon 2018 talk by Dan Saks on using friend function declarations inside class templates as a factory for new non-member functions, rather than as a way to grant access to private members. It also covers operator overloading, overload resolution, template instantiation and name lookup for friend functions.

What people said

Strong rec tbxfreeware · 2023-07 · Stack Overflow · on “Hidden Friend Concept in C++”

Hidden Friends Are Your Friend Dan Saks gave a great talk explaining hidden friends at CppCon2018. It is entitled Making New Friends [this video] . In addition to the issues explained by @john, templates are another big reason for mastering the "hidden friends" idiom. The stream insertion and extraction operators, operator<< and operator>> are best written in terms of std::basic_ostream and std::basic_istream , the templates on which std::ostream and std::istream are based. Written that way, the operators will work with any character type. When the objects you are reading and writing are themselves templates, things can get complicated fast. If the stream insertion and extraction operator functio

Cites Resurrection · 2018-10 · Stack Overflow · on “Declaring operator== for a nested template class”

It does not work for me either. It does indeed complain as you point out about non-member being declared (Dan Saks on that topic: [this video] ) but the non-member definition is not actually used. It suffers from the same problem as what was proposed in the OP's comments (now deleted). Forward declaring it as you suggest and adding `<>` to the friend declaration complains about no matching template. See here: [LINK]

Cites Onkar N Mahajan · 2020-11 · Stack Overflow · on “Using converting constructor in multiple parameter case”

I was looking at this video CppCon 2018: Dan Saks “Making New Friends” please look here [this video] I this talk, he refers to a (hypothetical) rational number class (which has numerator and denominator as private members and all of the computation revolves around this. I'm trying to implement whatever is mentioned in this talk just to understand templates well. I that, Dan referred to a function to perform multiplication int*rational //~ rational.h template <typename> class rational; template <typename T> std::ostream& operator<<(std::ostream&, const rational<T> &); template <typename T> rational<T> operator*(const rational<T>&, const rational<T>&); template <typename T> class rational { friend

Cites tbxfreeware · 2024-05 · Stack Overflow · on “Error when changing order of operators with templates in C++”

In general, definitions for the member functions of a class template should not be placed in a separate .cpp file. Rather, they should be defined within the template, in the .h file. This answer eliminates the .cpp file. This avoids the hassle of constantly repeating template<class Scalar> everywhere. More importantly, it allows the complier to automatically inline the member functions, and it eliminates the need for explicit template instantiation [LINK] . In addition, there is an idiom for operator+ [LINK] that involves first defining operator+= , and then implementing operator+ as a hidden friend [this video] , which invokes operator+= . See below. // vectors.h #ifndef INCLUDE_HEADERS_VECTORS_