473,322 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

function template question

consider the following program:

#include <iostream>
#include <cstdlib>

using namespace std;

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

template<typename Tvoid 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 Tvoid fcn(const T & arg)
with
template<typename Tvoid 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
Dec 30 '07 #1
6 1662
"su**************@yahoo.com, India" <su**************@yahoo.comwrote:
consider the following program:

#include <iostream>
#include <cstdlib>

using namespace std;

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

template<typename Tvoid 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 Tvoid fcn(const T & arg)
with
template<typename Tvoid 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.
Dec 30 '07 #2
>
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);
}
Dec 30 '07 #3
Rahul wrote:
>>
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);
}
Your code compares T & to T const &
not T to T const &.

Best

Kai-Uwe Bux
Dec 30 '07 #4
Rahul <sa*****@yahoo.co.inwrote:

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?
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
}
Dec 30 '07 #5
On 2007-12-30 11:39:42 -0600, Rahul <sa*****@yahoo.co.insaid:
>>
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);
}
Notice Daniel said const T& and T are interchangeable from the user's
point of view, NOT const T& and T&.

-dr

Dec 30 '07 #6
On 2007Äê12ÔÂ30ÈÕ, ÏÂÎç8ʱ52·Ö, "subramanian10....@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
consider the following program:

#include <iostream>
#include <cstdlib>

using namespace std;

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

}

template<typename Tvoid 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 Tvoid fcn(const T & arg)
with
template<typename Tvoid 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.
Jan 1 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: 胡岳偉(Yueh-Wei Hu) | last post by:
Hi all, I have 2 questions about template function as friends in template classes. I don't know why, and hope someone could help me. ...
0
by: Yueh-Wei Hu | last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news: ============================================================== > > Question 1: > >...
2
by: baumann | last post by:
hi all, i am reading the C++ templates complete guide, i have question on the following statement. We must therefore make sure the template parameters of the class template appear in the type...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
22
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be...
1
by: sebastian | last post by:
Hi, I'd like to specialize a template function that contains a template parameter. In Example i have the following function declared: .... template < int i > static stupid_object&...
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
9
by: Mike | last post by:
Hi, Just a simple question: why the compiler doesn't report error when accessing a private member function inside a function having template type ? For example: #include<iostream> using...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
21
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination with mingw because this code (or at least code...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.