Re: Explicit specialization [ Template ]
On Jul 21, 3:57 pm, Bart van Ingen Schenau <b...@ingen.ddns.info>
wrote:
AY wrote:
Hi there,
I'm trying to Override a generic version of swapvalue for char. But it
still call's the generic version. Here is the sample code -
template<typename T>
void swapvalues(T& src, T& dest)
{
T temp;
temp = src;
src = dest;
dest = temp;
cout<<"Inside swapvalue [template]"<<endl;
}
// Override of generic version.
void swapvalues(char& src, char& dest)
{
/*
char* temp = NULL;
*temp = src;
src = dest;
dest = *temp;
*/
cout<<"Inside swapvalue [char]"<<endl;
cout<<"src = "<<src<<" : dest = "<<dest<<endl;
}
int main()
{
int i=5, j = 7;
char* srcCh = "ABC";
char* destCh = "XYZ";
swapvalues(srcCh, destCh); // Template version is called, despite
address being passed.
No, the template version is called *because* an address is passed. The
types 'char *' and 'char &' are NOT the same, nor can they be converted
into each other.
// Isn't this the correct way of passing char* to char& ?
//swapvalues(*srcCh, *destCh); // Rt way of calling
This will only try to swap the first characters of srcCh and destCh,
which will result in Undefined Behaviour, because both pointers refer to
(conceptually) read-only memory.
cout<<srcCh<<" : "<<destCh<<endl;
return 0;
}
I thought this was the correct way of assigning char* to char&.
char* srcCh = "ABC"
char& refCh = *srcCh; // OK ! Reference is pointing to "ABC";
The initialisation is OK, but your comment not. References are NOT
pointers, so references can not point to anything. refCh here refers to
the single character A.
This may look like word-games, but it is essential for a proper
understanding of C++ that you understand the fundamental difference
between a pointer an a reference.
A tiny hint [ or if time permits a little elaboration ] on why the
explicit specialization is ignored is greatly appreciated.
Last of all, you did not provide any specialisation. You provided a
regular overloaded function.
The reason it was not called is because the arguments did not match.
Thanks in advance,
- AY.
Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ:http://www.comeaucomputing.com/learn/faq
c.l.c FAQ:http://c-faq.com/
c.l.c++ FAQ:http://www.parashift.com/c++-faq-lite/
[ Seehttp://www.gotw.ca/resources/clcm.htmfor info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Thank You all.
I greatly appreciate your help.
A small confusion still remains.
As mentioned in the response - " A char * points to a single char and
char& refers to a single char ".
I have created a small fn and am trying to understand what happens. I
honestly confess that there could be flaws in my understanding. Anyway
below is code snippet, my assumption on what happens and the questions
I have relating to template.
Here is the code:
void simplecopy(char& strRef, size_t sz) // --- [ 2 ]
{
char* pch = new char[sz+1]; // --- [ 3 ]
strncpy(pch, &strRef, sz); // --- [ 4 ]
pch[sz] = '\0'; // --- [ 5 ]
cout<<"*pch = "<<pch<<endl; // --- [ 6 ]
delete pch;
}
int main()
{
char* str = "My String";
simplecopy(*str, strlen(str)); // --- [ 1 ]
return 0;
}
My Assumption:
[ 1 ] --- We are passing a " pointer to the first character of the
char* str = "My String" as ' *str ' and " ' strlen of str ' which is
9 char length i.e 9 bytes ".
[ 2 ] --- Function ' simplecopy ' has 2 arg's : a ) ' strRef '
referring to character 'M' and size_t sz has the length of char* str
[ 3 ] --- Allocating memory in the heap.
[ 4 ] --- Copy all the char's referred to by char& strRef to char*
pch, starting at the address specified by ' &strRef ' and copying each
character until it hits null char.
[ 5 ] --- Appending a null byte.
My Question:
1. The above call to simplecopy(...) wouldn't have succeeded if I had
argument like
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]