Index
Week 4
AUTOAuto is done via deduction.
template <typename T>
void foo(T haha);
int x[4][10]; //x is array of 4 arrays of 10 ints
auto y = x; //Type of y is deduced
auto p = &x; //p is a pointer to the int[4][10]
//p is a pointer to an array of 4 arrays of 10 ints.
// int (*p) [4][10];
foo(x) //What is the type of foo(x)?
/*
x is an array of 4 arrays of 10 integers.
Let u be an array of T.
T u[10];
u is an array of 10 T objects.
foo(u);
- Cannot pass arrays directly to other functions
- Pass the pointer to the first element over to foo.
- The type of argument for foo is deduced to be T *
What if T itself is an array?
int p[20][30];
- How many elements does p have? 20.
- p is an array of 20 (arrays of 30 ints). The stuff in the bracket is the element type of the array!
- When p is passed into a function, the argument to the first element is passed.
- Argument to the first element is a pointer to the array of 30 ints.
- Possible to write that in C/C++?
→ int [30] * s; //Of course does not work - array have to be on the right.
→ int *s[30]; //Again, this won't work for obvious reasons (right-left)
→ int (*s) [30] //WORKS
*/
//void foo(int (*g)[]);
//GG is an array of 10 pointers to an array of 20 function pointers to functions that takes in a double, a short and returns a pointer to array of 10 floats.
float (* (*(*GG [10]) [20]) (double,short)) [10];
//If it returns an array instead of pointer to array, NC.
//No functions of functions, no functions of arrays, no arrays of functions
//pointers to arrays of unspecified sizes is ok surprisingly
void foo(T haha);
int x[4][10]; //x is array of 4 arrays of 10 ints
auto y = x; //Type of y is deduced
auto p = &x; //p is a pointer to the int[4][10]
//p is a pointer to an array of 4 arrays of 10 ints.
// int (*p) [4][10];
foo(x) //What is the type of foo(x)?
/*
x is an array of 4 arrays of 10 integers.
Let u be an array of T.
T u[10];
u is an array of 10 T objects.
foo(u);
- Cannot pass arrays directly to other functions
- Pass the pointer to the first element over to foo.
- The type of argument for foo is deduced to be T *
What if T itself is an array?
int p[20][30];
- How many elements does p have? 20.
- p is an array of 20 (arrays of 30 ints). The stuff in the bracket is the element type of the array!
- When p is passed into a function, the argument to the first element is passed.
- Argument to the first element is a pointer to the array of 30 ints.
- Possible to write that in C/C++?
→ int [30] * s; //Of course does not work - array have to be on the right.
→ int *s[30]; //Again, this won't work for obvious reasons (right-left)
→ int (*s) [30] //WORKS
*/
//void foo(int (*g)[]);
//GG is an array of 10 pointers to an array of 20 function pointers to functions that takes in a double, a short and returns a pointer to array of 10 floats.
float (* (*(*GG [10]) [20]) (double,short)) [10];
//If it returns an array instead of pointer to array, NC.
//No functions of functions, no functions of arrays, no arrays of functions
//pointers to arrays of unspecified sizes is ok surprisingly
The only thing you can pass over to a function that is bigger than 8 bytes is a struct/class in C/C++.