Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. // selector.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. #include <iostream>
  7.  
  8. struct S1 {
  9. void some_func() {
  10. std::cout << __func__ << std::endl;
  11. }
  12. };
  13.  
  14. struct S2 {
  15. void some_func() {
  16. std::cout << __func__ << std::endl;
  17. }
  18. };
  19.  
  20. struct S3 {
  21. void some_another_func() const {
  22. std::cout << __func__ << std::endl;
  23. }
  24. };
  25.  
  26.  
  27. template <typename T>
  28. struct is_S3 {
  29. static const bool value = false;
  30. };
  31.  
  32.  
  33. template <>
  34. struct is_S3<S3> {
  35. static const bool value = true;
  36. };
  37.  
  38. template <bool BOOL>
  39. struct struct_selector {
  40. template<typename T>
  41. static void select(T & t) {
  42. // fake
  43. }
  44. };
  45.  
  46. template <>
  47. struct struct_selector<true> {
  48. template<typename T>
  49. static void select(T & t) {
  50. t.some_another_func();
  51. }
  52. };
  53.  
  54. template <>
  55. struct struct_selector<false> {
  56. template<typename T>
  57. static void select(T & t) {
  58. // fake
  59. t.some_func();
  60. }
  61. };
  62.  
  63. struct lsf : S3 {
  64. void some_func() {
  65. S3::some_another_func();
  66. }
  67.  
  68. void some_another_func() {
  69. std::cout << "caveat" << std::endl;
  70. }
  71. };
  72.  
  73. template <typename T>
  74. void select(T & t) {
  75. struct_selector<is_S3<T>::value>::select(t);
  76. }
  77.  
  78. int main(int argc, char** argv) {
  79. S1 s1;
  80. S2 s2;
  81. S3 s3;
  82. lsf ls;
  83.  
  84. select(s1);
  85. select(s2);
  86. select(s3);
  87. select(ls);
  88.  
  89. return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement