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