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
#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