Guest User

Untitled

a guest
Nov 25th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. template <typename T> struct Class
  2. {
  3. int i;
  4.  
  5. void foo()
  6. {
  7. i = 5;
  8. std::cout << "Generic implementation" << std::endl;
  9. }
  10. };
  11.  
  12. template <> void Class<int>::foo()
  13. {
  14. i = 42;
  15. std::cout << "Specialized implementation" << std::endl;
  16. }
  17.  
  18. int main()
  19. {
  20. Class<double> d;
  21. d.foo();
  22. std::cout << d.i << std::endl;
  23.  
  24. Class<int> i;
  25. i.foo();
  26. std::cout << i.i << std::endl;
  27. }
  28.  
  29. #include <iostream>
  30. #include <string>
  31.  
  32. using namespace std;
  33.  
  34. template <class T>
  35. struct A
  36. {
  37. A(T x) : x(x) {}
  38. void foo() = delete; // { cout << "default: " << x << endl; }
  39. void bar() { cout << "bar: " << x << endl; }
  40.  
  41. T x;
  42. };
  43.  
  44. template<>
  45. void A<int>::foo()
  46. {
  47. cout << "int: " << x << endl;
  48. }
  49.  
  50.  
  51. int main()
  52. {
  53. A<int> a(0);
  54. a.foo();
  55. a.bar();
  56.  
  57. A<double> b(0);
  58. // b.foo(); // - не компилируется
  59. b.bar();
  60.  
  61. A<string> c("hello");
  62. // c.foo(); // - не компилируется
  63. c.bar();
  64. return 0;
  65. }
Add Comment
Please, Sign In to add comment