fork download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. template<typename T>
  5. void foo(T) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
  6.  
  7. template<typename T>
  8. struct pair {
  9. pair(T v)
  10. :key("")
  11. ,val(v)
  12. {}
  13. pair(const char *k, T v)
  14. :key(k)
  15. ,val(v)
  16. {}
  17.  
  18. const char *key;
  19. T val;
  20. };
  21.  
  22. template<typename T>
  23. auto make_pair(const char *key, T &&val) {
  24. using R = typename std::conditional<
  25. std::is_rvalue_reference<decltype(val)>::value || std::is_const<decltype(val)>::value
  26. ,const T&
  27. ,T&
  28. >::type;
  29. return pair<R>{key, val};
  30. }
  31.  
  32. int main() {
  33. int i=0;
  34. auto p0 = make_pair("i", 33);
  35. foo(p0);
  36. auto p1 = make_pair("i", i);
  37. foo(p1);
  38.  
  39. foo(make_pair("i", 33));
  40. foo(make_pair("i", i));
  41. }
  42.  
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
void foo(T) [with T = pair<const int&>]
void foo(T) [with T = pair<int&>]
void foo(T) [with T = pair<const int&>]
void foo(T) [with T = pair<int&>]