| re: const parameters to template class member functions
Kenneth Massey wrote:[color=blue]
> ...[/color]
Please don't top-post. I rearranged it.
[color=blue][color=green][color=darkred]
>>> int check(const T y) { return x==y; }[/color]
>>
>>For T = int*, y is a constant pointer to a non-constant int.
>>
>>[color=darkred]
>>> // compiles fine with this line: T is explicitly replaced by int*
>>> // int check(const int* y) { return x==y; }[/color]
>>
>>In this case, y is a non-constant pointer to constant int.[/color]
>
> Makes sense.
>
> Is there any way to get the latter behavior, definint the function with the
> template parameter instead of hard coding int* ?[/color]
If you don't mind hard-coding T*, then
int check(T const *y) ...
and substitute 'T' with 'int' (the pointer bit is already accounted for).
Otherwise, consider a simple fact that 'const' and types should be always
placed in a particular order to understand how type substitution works:
int check(T const y) // const goes _after_ the type
Now, even if you replace 'T' with 'int*', you get
int check(int* const y) // easier to understand, isn't it?
.. To get the same thing as
int check(int const *y)
you need to make your 'T' 'int const*'.
Also remember that top-level consts do not really count (or do anything
useful):
int check(int* const y)
is really quite equivalent to
int check(int* y)
V |