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 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.
>
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);
}
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
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
}
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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.
...
|
by: Yueh-Wei Hu |
last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news:
==============================================================
> > Question 1:
> >...
|
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...
|
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 ]
|
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...
|
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&...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
|
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...
| |