Pointers to Functions

Hello All,

I was hoping someone could help me out with a knowledge check here. Let's use the following as an example declaration:

 
const double * (*pa[3])(const double *, int) = {f1,f2,f3};


I am going to try and define every version of pa now. Am I getting this right?:

*pa = used for array values. Example: (*pa)[0] is first element. (*pa)[1] is second element, ect.

pa = array name. Is a pointer to the address of the first array element.

&pa = address of the pointer which points to the address of the first array element.

The second statement is correct.
*pa is equivalent to pa[0].
I think &pa should be rejected by the compiler.

Normally, typedefs are used to make things clearer.
1
2
const double * (*function_pointer_t)(const double *, int);
function_pointer_t pa[3] = {f1,f2,f3};
> (*pa)[0] is first element. (*pa)[1] is second element

pa[0] yields a reference to first element, pa[1] yields a reference to second element


> pa = array name. Is a pointer to the address of the first array element.

pa = array name. pa is an array, not a pointer. Type of pa is 'array of 3 pointers to function'


> &pa = address of the pointer which points to the address of the first array element.

&pa is the address of the array. Type of &pa is 'pointer to array of 3 pointers to function'

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
#include <type_traits>

int main()
{
    using fn_type = const double* ( const double*, int ) ;
    typedef const double* fn_type( const double*, int ) ; // same as above
    using ptr_to_fn_type = fn_type* ;
    using ref_to_ptr_to_fn_type = ptr_to_fn_type& ;
    using array_of_3_ptr_to_fn_type = ptr_to_fn_type[3] ;
    using ptr_to_array_of_3_ptr_to_fn_type = array_of_3_ptr_to_fn_type* ;

    const double * (*pa[3])(const double *, int) = { /* ... */ };

    // type of pa is 'array of 3 pointers to function'
    static_assert( std::is_same< array_of_3_ptr_to_fn_type, decltype(pa) >::value, "must be same type" ) ;

    // pa[0] yields a reference to first element. pa[1] yields a reference to second element,etc.
    static_assert( std::is_same< ref_to_ptr_to_fn_type, decltype(pa[0]) >::value, "must be same type" ) ;

    // array is implicitly converted to pointer to first element, *pa is reference to first element
    static_assert( std::is_same< ref_to_ptr_to_fn_type, decltype(*pa) >::value, "must be same type" ) ;

    // value category of +pa[0] and +*pa is 'prvalue'. +pa[0] is a prvalue of type 'pointer to function'
    static_assert( std::is_same< ptr_to_fn_type, decltype( +pa[0] ) >::value, "must be same type" ) ;
    static_assert( std::is_same< ptr_to_fn_type, decltype( +*pa ) >::value, "must be same type" ) ;

    // &pa is the address of the array; type of &pa is 'pointer to array of 3 pointers to function'
    static_assert( std::is_same< ptr_to_array_of_3_ptr_to_fn_type, decltype(&pa) >::value, "must be same type" ) ;
}

http://coliru.stacked-crooked.com/a/1c806c451005f8f7
http://rextester.com/QCEM35367
Topic archived. No new replies allowed.