Guest User

Untitled

a guest
Jan 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. template <int I> struct A {};
  2.  
  3. char xxx(int);
  4. char xxx(float);
  5.  
  6. template <class T> A<sizeof(xxx((T)0))> f(T){}
  7.  
  8. int main()
  9. {
  10. f(1);
  11. }
  12.  
  13. struct X {};
  14. struct Y
  15. {
  16. Y(X){}
  17. };
  18.  
  19. template <class T> auto f(T t1, T t2) -> decltype(t1 + t2); // #1
  20. X f(Y, Y); // #2
  21.  
  22. X x1, x2;
  23. X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
  24.  
  25. #include <iostream>
  26.  
  27. struct not_a_type{};
  28.  
  29. // catch-all case
  30. void test(...)
  31. {
  32. std::cout << "Couldn't calln";
  33. }
  34.  
  35. // catch when C is a reference-to-class type and F is a member function pointer
  36. template<class C, class F>
  37. auto test(C c, F f) -> decltype((c.*f)(), void()) // 'C' is reference type
  38. {
  39. std::cout << "Could call on referencen";
  40. }
  41.  
  42. // catch when C is a pointer-to-class type and F is a member function pointer
  43. template<class C, class F>
  44. auto test(C c, F f) -> decltype((c->*f)(), void()) // 'C' is pointer type
  45. {
  46. std::cout << "Could call on pointern";
  47. }
  48.  
  49. struct X{
  50. void f(){}
  51. };
  52.  
  53. int main(){
  54. X x;
  55. test(x, &X::f);
  56. test(&x, &X::f);
  57. test(42, 1337);
  58. }
  59.  
  60. struct has_member_begin_test{
  61. template<class U>
  62. static auto test(U* p) -> decltype(p->begin(), std::true_type());
  63. template<class>
  64. static auto test(...) -> std::false_type;
  65. };
  66.  
  67. template<class T>
  68. struct has_member_begin
  69. : decltype(has_member_begin_test::test<T>(0)) {};
  70.  
  71. struct has_member_begin_test{
  72. template<class U>
  73. static auto test(U* p) -> decltype(p->begin(), std::true_type());
  74. template<class>
  75. static auto test(...) -> std::false_type;
  76. };
  77.  
  78. template<class T>
  79. struct has_member_begin
  80. : decltype(has_member_begin_test::test<T>(0)) {};
  81.  
  82. struct has_member_begin_test{
  83. template<class U>
  84. static auto test(U* p) -> decltype(p->begin(), std::true_type());
  85. template<class>
  86. static auto test(...) -> std::false_type;
  87. };
  88.  
  89. template<class T>
  90. struct has_member_begin
  91. : decltype(has_member_begin_test::test<T>(0)) {};
Add Comment
Please, Sign In to add comment