fork download
  1. #include <iostream>
  2. #include <typeinfo>
  3.  
  4. class B;
  5.  
  6. struct A {
  7. decltype(auto) func(B*);
  8. decltype(auto) func(B&);
  9. };
  10.  
  11. template<typename T>
  12. void size() {
  13. std::cout << "Size of " << typeid(T).name() << ": " << sizeof(T) << '\n';
  14. }
  15.  
  16. template<>
  17. void size<B*>() {
  18. std::cout << "Size of B*: " << sizeof(B*) << '\n';
  19. }
  20.  
  21. // Error if uncommented.
  22. //template<>
  23. //void size<B>() {
  24. // std::cout << "Size of B: " << sizeof(B) << '\n';
  25. //}
  26.  
  27. class B {
  28. friend class A;
  29.  
  30. void func() const { std::cout << "Be funky.\n"; }
  31. };
  32.  
  33. decltype(auto) A::func(B* b) { return b->func(); }
  34. decltype(auto) A::func(B& b) { return b.func(); }
  35.  
  36. int main() {
  37. A a;
  38. B b;
  39.  
  40. a.func(b);
  41. a.func(&b);
  42.  
  43. size<B*>();
  44. size<B>();
  45. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Be funky.
Be funky.
Size of B*: 4
Size of 1B: 1