S
subramanian100in
consider the following program:
#include <iostream>
#include <cstdlib>
using namespace std;
template<typename T> void fn(T const & arg)
{
cout << arg << endl;
return;
}
int main()
{
const int x = 100;
const int* const y = &x;
fn(y);
typedef const int* const Type;
Type const & ref = y;
cout << "from main(): " << ref << endl;
return EXIT_SUCCESS;
}
Question 1:
From the function template call 'fn(y)', the type of the template
parameter deduced is 'const int* const'. However, the function
template definition already has a const - that is, void fn(T const&
arg). In this, T will get substituted by 'const int* const'. Am I
correct ? If so, then won't we end up with void fn(const int* const
const & arg) ? ie won't the instance of fn() have a duplicate const ?
Question 2:
consider
typedef const int* const Type;
Type const & ref = y;
Here also, Type is defined to be 'const int* const' and so in the
second line since we have Type const & ref, won't we end up with
duplicate const - that is
const int* const const & ref = y ?
Kindly clarify.
Thanks
V.Subramanian
#include <iostream>
#include <cstdlib>
using namespace std;
template<typename T> void fn(T const & arg)
{
cout << arg << endl;
return;
}
int main()
{
const int x = 100;
const int* const y = &x;
fn(y);
typedef const int* const Type;
Type const & ref = y;
cout << "from main(): " << ref << endl;
return EXIT_SUCCESS;
}
Question 1:
From the function template call 'fn(y)', the type of the template
parameter deduced is 'const int* const'. However, the function
template definition already has a const - that is, void fn(T const&
arg). In this, T will get substituted by 'const int* const'. Am I
correct ? If so, then won't we end up with void fn(const int* const
const & arg) ? ie won't the instance of fn() have a duplicate const ?
Question 2:
consider
typedef const int* const Type;
Type const & ref = y;
Here also, Type is defined to be 'const int* const' and so in the
second line since we have Type const & ref, won't we end up with
duplicate const - that is
const int* const const & ref = y ?
Kindly clarify.
Thanks
V.Subramanian