kalabukdima

Untitled

Oct 28th, 2020
852
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. template <class T>
  5. class Foo;
  6.  
  7. template <class T, class U>
  8. void Swap(Foo<T>& lhs, Foo<U>& rhs) {
  9.     std::swap(lhs.a, rhs.a);
  10. }
  11.  
  12. template <class T>
  13. class Foo {
  14. private:
  15.     int a{0};
  16.  
  17. public:
  18.     friend void Swap<>(Foo&, Foo&);
  19. };
  20.  
  21. int main() {
  22.     Foo<int> a;
  23.     Foo<std::string>  b;
  24.     Swap(a, b);
  25. }
  26.  
  27. // t.cpp: In instantiation of ‘void Swap(Foo<T>&, Foo<U>&) [with T = int; U = std::__cxx11::basic_string<char>]’:
  28. // t.cpp:24:14:   required from here
  29. // t.cpp:9:19: error: ‘int Foo<int>::a’ is private within this context
  30. //     9 |     std::swap(lhs.a, rhs.a);
  31. //       |               ~~~~^
  32. // t.cpp:15:9: note: declared private here
  33. //    15 |     int a{0};
  34. //       |         ^
  35. // t.cpp:9:26: error: ‘int Foo<std::__cxx11::basic_string<char> >::a’ is private within this context
  36. //     9 |     std::swap(lhs.a, rhs.a);
  37. //       |                      ~~~~^
  38. // t.cpp:15:9: note: declared private here
  39. //    15 |     int a{0};
  40. //       |         ^
RAW Paste Data