--- title: "Template Friends | Microsoft Docs" ms.custom: "" ms.date: "11/04/2016" ms.reviewer: "" ms.suite: "" ms.technology: - "cpp-language" ms.tgt_pltfrm: "" ms.topic: "language-reference" dev_langs: - "C++" ms.assetid: 077acea5-0d0f-4b33-916d-1211797e5e28 caps.latest.revision: 14 author: "mikeblome" ms.author: "mblome" manager: "ghogen" robots: noindex,nofollow translation.priority.ht: - "cs-cz" - "de-de" - "es-es" - "fr-fr" - "it-it" - "ja-jp" - "ko-kr" - "pl-pl" - "pt-br" - "ru-ru" - "tr-tr" - "zh-cn" - "zh-tw" --- # Template Friends Class templates can have [friends](http://msdn.microsoft.com/en-us/bf412640-d857-4acb-b2b5-513131cb9681). A class or class template, function, or function template can be a friend to a template class. Friends can also be specializations of a class template or function template, but not partial specializations. **C++ 11**: A type parameter can be declared as a friend by using the form `friend T;` . ``` template class my_class { friend T; //... }; ``` ## Example In the following example, a friend function is defined as a function template within the class template. This code produces a version of the friend function for every instantiation of the template. This construct is useful if your friend function depends on the same template parameters as the class does. ``` // template_friend1.cpp // compile with: /EHsc #include using namespace std; template class Array { T* array; int size; public: Array(int sz): size(sz) { array = new T[size]; memset(array, 0, size * sizeof(T)); } Array(const Array& a) { size = a.size; array = new T[size]; memcpy_s(array, a.array, sizeof(T)); } T& operator[](int i) { return *(array + i); } int Length() { return size; } void print() { for (int i = 0; i < size; i++) cout << *(array + i) << " "; cout << endl; } template friend Array* combine(Array& a1, Array& a2); }; template Array* combine(Array& a1, Array& a2) { Array* a = new Array(a1.size + a2.size); for (int i = 0; i < a1.size; i++) (*a)[i] = *(a1.array + i); for (int i = 0; i < a2.size; i++) (*a)[i + a1.size] = *(a2.array + i); return a; } int main() { Array alpha1(26); for (int i = 0 ; i < alpha1.Length() ; i++) alpha1[i] = 'A' + i; alpha1.print(); Array alpha2(26); for (int i = 0 ; i < alpha2.Length() ; i++) alpha2[i] = 'a' + i; alpha2.print(); Array*alpha3 = combine(alpha1, alpha2); alpha3->print(); delete alpha3; } ``` ```Output A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z ``` ## Example The next example involves a friend that has a template specialization. A function template specialization is automatically a friend if the original function template is a friend. It is also possible to declare only the specialized version of the template as the friend, as the comment before the friend declaration in the following code indicates. If you do this, you must put the definition of the friend template specialization outside of the template class. ``` // template_friend2.cpp // compile with: /EHsc #include using namespace std; template class Array; template void f(Array& a); template class Array { T* array; int size; public: Array(int sz): size(sz) { array = new T[size]; memset(array, 0, size * sizeof(T)); } Array(const Array& a) { size = a.size; array = new T[size]; memcpy_s(array, a.array, sizeof(T)); } T& operator[](int i) { return *(array + i); } int Length() { return size; } void print() { for (int i = 0; i < size; i++) { cout << *(array + i) << " "; } cout << endl; } // If you replace the friend declaration with the int-specific // version, only the int specialization will be a friend. // The code in the generic f will fail // with C2248: 'Array::size' : // cannot access private member declared in class 'Array'. //friend void f(Array& a); friend void f<>(Array& a); }; // f function template, friend of Array template void f(Array& a) { cout << a.size << " generic" << endl; } // Specialization of f for int arrays // will be a friend because the template f is a friend. template<> void f(Array& a) { cout << a.size << " int" << endl; } int main() { Array ac(10); f(ac); Array a(10); f(a); } ``` ```Output 10 generic 10 int ``` ## Example The next example shows a friend class template declared within a class template. The class template is then used as the template argument for the friend class. Friend class templates must be defined outside of the class template in which they are declared. Any specializations or partial specializations of the friend template are also friends of the original class template. ``` // template_friend3.cpp // compile with: /EHsc #include using namespace std; template class X { private: T* data; void InitData(int seed) { data = new T(seed); } public: void print() { cout << *data << endl; } template friend class Factory; }; template class Factory { public: U* GetNewObject(int seed) { U* pu = new U; pu->InitData(seed); return pu; } }; int main() { Factory< X > XintFactory; X* x1 = XintFactory.GetNewObject(65); X* x2 = XintFactory.GetNewObject(97); Factory< X > XcharFactory; X* x3 = XcharFactory.GetNewObject(65); X* x4 = XcharFactory.GetNewObject(97); x1->print(); x2->print(); x3->print(); x4->print(); } ``` ```Output 65 97 A a ``` ## See Also [Default Arguments](../cpp/default-arguments.md)