fork download
  1. #include <iostream>
  2.  
  3. /**************************************************************************/
  4.  
  5. template<typename T>
  6. void foo(T v) { std::cout << v.key << ": " << __PRETTY_FUNCTION__ << std::endl; }
  7.  
  8. template<typename T>
  9. struct pair {
  10. pair(T v)
  11. :key("")
  12. ,val(v)
  13. {}
  14. pair(const char *k, T v)
  15. :key(k)
  16. ,val(v)
  17. {}
  18.  
  19. const char *key;
  20. T val;
  21. };
  22.  
  23. template<typename T>
  24. auto make_pair(const char *key, T &val) {
  25. return pair<T &>{key, val};
  26. }
  27.  
  28. template<typename T>
  29. auto make_pair(const char *key, const T &val) {
  30. return pair<const T &>{key, val};
  31. }
  32.  
  33. /**************************************************************************/
  34.  
  35. struct type {
  36. int a;
  37. int b;
  38.  
  39. type()
  40. :a{}
  41. ,b{}
  42. {}
  43.  
  44. void m0() const {
  45. const auto p0 = make_pair("type.a", a);
  46. foo(p0);
  47. }
  48. void m1() {
  49. const auto p0 = make_pair("type.b", b);
  50. foo(p0);
  51. }
  52. };
  53.  
  54. /**************************************************************************/
  55.  
  56. int main() {
  57. type t;
  58. t.m0();
  59. t.m1();
  60.  
  61. int c=0;
  62. const int d=0;
  63. auto p0 = make_pair("b", 33);
  64. foo(p0);
  65. auto p1 = make_pair("c", c);
  66. foo(p1);
  67. auto p2 = make_pair("d", d);
  68. foo(p2);
  69.  
  70. std::cout << "**********************************" << std::endl;
  71. int f=0;
  72. const int g=0;
  73. foo(make_pair("i", 33));
  74. foo(make_pair("f", f));
  75. foo(make_pair("g", g));
  76. }
  77.  
  78. /**************************************************************************/
  79.  
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
type.a: void foo(T) [with T = pair<const int&>]
type.b: void foo(T) [with T = pair<int&>]
b: void foo(T) [with T = pair<const int&>]
c: void foo(T) [with T = pair<int&>]
d: void foo(T) [with T = pair<const int&>]
**********************************
i: void foo(T) [with T = pair<const int&>]
f: void foo(T) [with T = pair<int&>]
g: void foo(T) [with T = pair<const int&>]