fork download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. struct A
  5. {
  6. template < typename T >
  7. typename std::enable_if< std::is_pointer<T>::value, void >::type
  8. foo( const T& v )
  9. { std::cout << "A::foo( const T& p ) - T is a pointer, *p == " << *v << '\n' ; }
  10.  
  11. template < typename T >
  12. typename std::enable_if< !std::is_pointer<T>::value, void >::type
  13. foo( const T& v )
  14. { std::cout << "A::foo( const T& v ) - T is not a pointer, v == " << v << '\n' ; }
  15.  
  16. template < typename T, std::size_t N >
  17. void foo( const T(&array)[N] )
  18. {
  19. std::cout << "A::foo( const T(&array)[" << N << "] ): " ;
  20. for( const auto& v : array ) std::cout << v << ' ' ;
  21. std::cout << '\n' ;
  22. }
  23.  
  24. template < typename T >
  25. void foo( const T array[], std::size_t n )
  26. {
  27. std::cout << "A::foo( const T array[], n ) - n == " << n << ": " ;
  28. for( std::size_t i = 0 ; i < n ; ++i ) std::cout << array[i] << ' ' ;
  29. std::cout << '\n' ;
  30. }
  31. };
  32.  
  33. int main()
  34. {
  35. double d[5] = { 1.1, 2.3, 3.3, 4.4, 5.5 } ;
  36. A a ;
  37.  
  38. a.foo( d[3] ) ; // A::foo( const T& v ) - T is not a pointer, v == 4.4
  39. a.foo(d) ; // A::foo( const T(&array)[5] ): 1.1 2.3 3.3 4.4 5.5
  40. a.foo( d, 5 ) ; // A::foo( const T array[], n ) - n == 5: 1.1 2.3 3.3 4.4 5.5
  41. a.foo( +d ) ; // A::foo( const T& p ) - T is a pointer, *p == 1.1
  42. }
  43.  
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
A::foo( const T& v ) - T is not a pointer, v == 4.4
A::foo( const T(&array)[5] ): 1.1 2.3 3.3 4.4 5.5 
A::foo( const T array[], n ) - n == 5: 1.1 2.3 3.3 4.4 5.5 
A::foo( const T& p ) - T is a pointer, *p == 1.1