question on typedef and function template

  • Thread starter subramanian100in
  • Start date
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
 
S

Salt_Peter

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 ?

no sir, the cv-qualifier introduced in the template type arguement is
ignored in cases like these.
the standard [8.3.2] 'References' says so specifically.

If you still have doubts, change the function to:
void fn(const int& arg) { ... }

and you'll get something like:
In function 'int main()':
error: invalid initialization of reference of type 'const int&' from
expression of type 'const int* const'
error: in passing argument 1 of 'void fn(const int&)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top