473,386 Members | 1,706 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,386 software developers and data experts.

template specialization problem, but only with char*

I'm trying to write a template specialization, and the char*
specialization is giving me some trouble. Here's a simplified example:

/**************************/
/* test.cpp */
#include "try.hpp"

int main(){}
/**************************/

/**************************/
/* try.hpp */

template <typename T> struct foo
{
void f(const T& param) const;
};

template <typename T>
void
foo<T>::f(const T& param) const
{}

template <> // works fine
void
foo<int>::f(const int& param) const
{}

/*
The following results in:

template-id `f<>' for `void foo<char*>::f(const char*&) const' does not
match any template declaration

template <>
void
foo<char*>::f(const char*& param) const
{}
*/

/*
The following results in:
template-id `f<>' for `void foo<char*>::f(const char*) const' does not
match any template declaration

template <>
void
foo<char*>::f(const char* param) const
{}
*/

/*
But the following works fine. That really confuses me....
*/
typedef char* Char;

template <>
void
foo<Char>::f(const Char& param) const
{}

/**************************/
Thanks for reading. Any help would be greatly appreciated.

- Adam

Jul 22 '05 #1
3 2052
Adam wrote:
I'm trying to write a template specialization, and the char*
specialization is giving me some trouble. Here's a simplified example:

/**************************/
/* test.cpp */
#include "try.hpp"

int main(){}
/**************************/

/**************************/
/* try.hpp */

template <typename T> struct foo
{
void f(const T& param) const;
};

template <typename T>
void
foo<T>::f(const T& param) const
{}

template <> // works fine
void
foo<int>::f(const int& param) const
{}

/*
The following results in:

template-id `f<>' for `void foo<char*>::f(const char*&) const' does not
match any template declaration

template <>
void
foo<char*>::f(const char*& param) const
{}
*/

/*
The following results in:
template-id `f<>' for `void foo<char*>::f(const char*) const' does not
match any template declaration

template <>
void
foo<char*>::f(const char* param) const
{}
*/

/*
But the following works fine. That really confuses me....
*/
typedef char* Char;

template <>
void
foo<Char>::f(const Char& param) const
{}

/**************************/


'const' relates to the object, not to the type. In all fairness, it
should be placed to the right of the type. In your case, this is what
your [working] declaration should be

template<> void foo<char*>::f(char* const & param);

and you tried [unsuccessfully] to declare it as

template<> void foo<char*>::f(char const *& param);

Do you see the difference?

In your template, it's the object who is const. So, the reference that
'param' designates _refers_ to a constant something. In your 'char*'
specialisation, that something is a _pointer_to_char_. So, you have to
make your _pointer_ constant. You've been trying to make the _char_
constant and leave the pointer non-const. That's the mistake.

V
Jul 22 '05 #2

Victor Bazarov wrote:
Adam wrote:
<snip> 'const' relates to the object, not to the type. In all fairness, it
should be placed to the right of the type. In your case, this is what your [working] declaration should be

template<> void foo<char*>::f(char* const & param);

and you tried [unsuccessfully] to declare it as

template<> void foo<char*>::f(char const *& param);

Do you see the difference?


I do.

Thanks, Victor.

It is no longer relevant to this particular problem, but I am curious
as to why the following worked:

typedef char* Char;
template<> void foo<Char>::f(const Char& param);

How is 'const Char &' different from 'const char* &' in this case? I'm
guessing that 'Char' is now seen only as a pointer, and that 'const
Char' is equivalent to 'Char const'. Is that correct?

- Adam

Jul 22 '05 #3
Adam wrote:
[...]
It is no longer relevant to this particular problem, but I am curious
as to why the following worked:

typedef char* Char;
template<> void foo<Char>::f(const Char& param);

How is 'const Char &' different from 'const char* &' in this case? I'm
guessing that 'Char' is now seen only as a pointer, and that 'const
Char' is equivalent to 'Char const'. Is that correct?


Absolutely. You always should position the 'const' specifier to the right
of the type-id. In case of 'char*', the type-id is 'char', so the 'const'
moves between 'char' and '*'. In case of 'Char', the type-id is 'Char',
so the 'const' skips to the right. Once expanded again, it becomes this
peculiar thing 'char * const', which is not the same as 'char const *'.

It's not the clearest part of the language, but once you learn it, it will
serve you well.

V
Jul 22 '05 #4

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

Similar topics

7
by: CoolPint | last post by:
While I was testing my understanding of Functioin Template features by playing with simple function templates, I got into a problem which I cannot understand. I would be very grateful if someone...
2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
5
by: Niklas Norrthon | last post by:
I've been banging my head in the wall for some time now over a little problem having to do with partial specialization of function templates. The real problem is more complex than this, but...
2
by: Thomas Kowalski | last post by:
Hi, I would like to write a template class Polygon<VertexTypthere vertex typ can be eigther a pointer or a value typ. It has an attribute: std::vector<VertexTypvertices; And a methode:...
2
by: Nick | last post by:
I'm learning C++ and ran into a compile error using Visual C++ 2005 Express on the following example program (located at http://www.cplusplus.com/doc/tutorial/templates.html): // template...
13
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm...
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
1
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
5
by: chgans | last post by:
Hi all, I'm having difficulties with some template static member, especially when this member is a template instance, for example: ---- template<typename T> class BaseT { public: static...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.