function template question

S

subramanian100in

consider the following program:

#include <iostream>
#include <cstdlib>

using namespace std;

template<typename T> void fcn(T arg)
{
cout << "from fcn(T arg)" << endl;
return;
}

template<typename T> void fcn(const T & arg)
{
cout << "from fcn(const T& arg)" << endl;
return;
}

int main()
{
int a = 100;
int* b = &a;

fcn(b);

return EXIT_SUCCESS;
}

The above program when compiled under g++, gives the following
compilation error:

x.cpp: In function `int main()':
x.cpp:23: error: call of overloaded `fcn(int*&)' is ambiguous
x.cpp:7: note: candidates are: void fcn(T) [with T = int*]
x.cpp:13: note: void fcn(const T&) [with T = int*]

Even if I replace the line
template<typename T> void fcn(const T & arg)
with
template<typename T> void fcn(T const & arg)
I get the same compilation error.

My question is NOT related to the compilation error.
I get to understand that when we specify fcn(const T & arg), the
compiler treats it, as if we had specified fcn(T const & arg) - that
is, 'const T &' is treated as 'T const &' in function template. Is my
understanding correct.

Kindly clarify

Thanks
V.Subramanian
 
D

Daniel T.

consider the following program:

#include <iostream>
#include <cstdlib>

using namespace std;

template<typename T> void fcn(T arg)
{
cout << "from fcn(T arg)" << endl;
return;
}

template<typename T> void fcn(const T & arg)
{
cout << "from fcn(const T& arg)" << endl;
return;
}

int main()
{
int a = 100;
int* b = &a;

fcn(b);

return EXIT_SUCCESS;
}

The above program when compiled under g++, gives the following
compilation error:

x.cpp: In function `int main()':
x.cpp:23: error: call of overloaded `fcn(int*&)' is ambiguous
x.cpp:7: note: candidates are: void fcn(T) [with T = int*]
x.cpp:13: note: void fcn(const T&) [with T = int*]

Even if I replace the line
template<typename T> void fcn(const T & arg)
with
template<typename T> void fcn(T const & arg)
I get the same compilation error.

My question is NOT related to the compilation error.
I get to understand that when we specify fcn(const T & arg), the
compiler treats it, as if we had specified fcn(T const & arg) - that
is, 'const T &' is treated as 'T const &' in function template. Is my
understanding correct.

Yes. "const T &" and "T const &" mean the same thing. As do "const T *"
and "T const *".

More interestingly (and related to the error) "const T&" and "T" are
interchangeable as parameters. They do different things, but from
outside the function, they are indistinguishable.
 
R

Rahul

More interestingly (and related to the error) "const T&" and "T" are
interchangeable as parameters. They do different things, but from
outside the function, they are indistinguishable.

Why do you say it is indistinguishable?

void fun(int &sam)
{
printf("first fun\n");
}

void fun(const int &sam)
{
printf("second fun\n");
}

int main()
{
int a = 5;
fun(5); // Invokes the second fun
fun(a); // Invokes the first fun
const int aa = 5;
fun(aa); // Invokes the second fun
return(0);
}
 
J

jkherciueh

Rahul said:
Why do you say it is indistinguishable?

void fun(int &sam)
{
printf("first fun\n");
}

void fun(const int &sam)
{
printf("second fun\n");
}

int main()
{
int a = 5;
fun(5); // Invokes the second fun
fun(a); // Invokes the first fun
const int aa = 5;
fun(aa); // Invokes the second fun
return(0);
}

Your code compares T & to T const &
not T to T const &.



Best

Kai-Uwe Bux
 
D

Daniel T.

Rahul said:
Why do you say it is indistinguishable?

Because it is.

void fun( int sam )
{
cout << "first fun\n";
}

void fun( const int& sam )
{
cout << "second fun\n";
}

int main()
{
int a = 5;
fun( 5 ); // error: call of overloaded 'fun(int)' is ambiguous
fun( a ); // error: call of overloaded 'fun(int&)' is ambiguous
}
 
D

Dave Rahardja

Why do you say it is indistinguishable?

void fun(int &sam)
{
printf("first fun\n");
}

void fun(const int &sam)
{
printf("second fun\n");
}

int main()
{
int a = 5;
fun(5); // Invokes the second fun
fun(a); // Invokes the first fun
const int aa = 5;
fun(aa); // Invokes the second fun
return(0);
}

Notice Daniel said const T& and T are interchangeable from the user's
point of view, NOT const T& and T&.

-dr
 
¡

¡ÚÒàÁµ¡ÛÃ÷ÖÇ

consider the following program:

#include <iostream>
#include <cstdlib>

using namespace std;

template<typename T> void fcn(T arg)
{
cout << "from fcn(T arg)" << endl;
return;

}

template<typename T> void fcn(const T & arg)
{
cout << "from fcn(const T& arg)" << endl;
return;

}

int main()
{
int a = 100;
int* b = &a;

fcn(b);

return EXIT_SUCCESS;

}

The above program when compiled under g++, gives the following
compilation error:

x.cpp: In function `int main()':
x.cpp:23: error: call of overloaded `fcn(int*&)' is ambiguous
x.cpp:7: note: candidates are: void fcn(T) [with T = int*]
x.cpp:13: note: void fcn(const T&) [with T = int*]

Even if I replace the line
template<typename T> void fcn(const T & arg)
with
template<typename T> void fcn(T const & arg)
I get the same compilation error.

My question is NOT related to the compilation error.
I get to understand that when we specify fcn(const T & arg), the
compiler treats it, as if we had specified fcn(T const & arg) - that
is, 'const T &' is treated as 'T const &' in function template. Is my
understanding correct.

Kindly clarify

Thanks
V.Subramanian

I think the T const equal with the const T .You can try it and you
will find out that the consequence is the same.
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top