If i understood correctly, we have 3 cases in type deduction:
1) param is not reference nor pointer.
int x = 5;
template< typename T>
func( T param )
template< typename T>
func1( T & param )
func(x)
func1(x)
in both cases T is deduced to be int
2) param is pointer or reference , in this case we ignore reference and pointer -ness
template< typename T>
func( T & param )
int x = 5;
const int y =x;
const int &z =x;
func(x) // param is type int & and T is type int
func(y) // param is type const int & and T is type const int
func(z) // param is type const int & and T is type const int
3) param is "universal reference".
template< typename T>
func( T && param )
int x = 5;
const int y = x;
const int & z = x;
func(x) // param is type int & and T is type int &
func(y) // param is type const int & T is type const int &
func(z) // param is type const int & and T is type const int &
func(5) // param is type int && and T is type int &&
The auto
keyword decides type like template deduction with the exception of
auto x = {1 , 2 , 3}
where type of auto x
is initalizer_list
However how does this work with constness? Having
struct Test{ int x };
Test t;
const Test & t1 = t;
auto t2 = t1; // t2 is type of Test not const Test &
For example if we passed
const int* const x = ...
In what cases would be constness ignored and what consntess would prevail?