C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-21-2009, 11:08 AM   #1
msh
Novice
 
Join Date: Jul 2009
Posts: 32
Function template specialization.

I'm trying to get my head around function template specializations and I just can't understand what am I doing wrong here.

Prototypes and error below.

Can someone explain to me what I'm doing wrong?

Code:
// template
template <typename T>
T findMax(const T arr[], int n);

// specializations for char**
template <>
char* findMax(const char* arr[], int n);
I get the following error from GCC:
Code:
error: template-id 'findMax<>' for 'char* findMax(const char**, int)' does not match any template declaration
msh is offline   Reply With Quote
Old 07-21-2009, 11:19 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
It should be:
Code:
// specializations for char*
template <>
char* findMax(char* const arr[], int n);
since the const is supposed to be applied to T, hence it is applied to char*, not char. If you really want to specialise for char**, then it should be:
Code:
// specializations for char**
template <>
char** findMax(char** const arr[], int n);
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 07-21-2009, 11:30 AM   #3
msh
Novice
 
Join Date: Jul 2009
Posts: 32
Ah. I see!

It's different from regular functions, e.g:
Code:
void f(const char arr[]);
Could you elaborate some more why it is so? It seems somewhat unintuitive.
msh is offline   Reply With Quote
Old 07-21-2009, 11:33 AM   #4
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
Quote:
Originally Posted by msh
It's different from regular functions
Different? If you are talking about what the const in the parameter applies to, then no, that is not true. For the parameter of f, the const applies to the char, not the pointer. Perhaps it would be easier to see if we rewrote it as:
Code:
void f(const char* arr);
or:
Code:
void f(char const* arr);
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 07-21-2009, 12:56 PM   #5
msh
Novice
 
Join Date: Jul 2009
Posts: 32
Okay. I'm lost now.

That still does not work as I want it to, that is, I want the specialization to process something like this:
Code:
const char* s[5] =
{
    "London",
    "Berlin",
    "Paris",
    "Madrid",
    "Lisbon"
};
msh is offline   Reply With Quote
Old 07-21-2009, 01:29 PM   #6
Registered User
 
Join Date: Jun 2006
Posts: 72
You are declaring a pointer array in which the elements are 5 characters(?) long.

Code:
#include<stdlib.h>

int main(){

  const char* cities[10] = { "London", "Paris", "NYC" };

  printf("%s", *(cities + 1)); // prints Paris

  system("PAUSE");
  return 0;
}
Kudose is offline   Reply With Quote
Old 07-21-2009, 01:37 PM   #7
msh
Novice
 
Join Date: Jul 2009
Posts: 32
I was under the impression that I'm declaring an array of 5 pointers to string constants.

Code:
int main(void) {
    
    const char* s[5] = 
    {
        "London",
        "Berlin",
        "Paris",
        "Madrid",
        "Lisbon"
    };
    
    for (int i = 0; i < 5; i++) {
        std::cout << s[i] << std::endl;
    }
    
    return 0;
}
msh is offline   Reply With Quote
Old 07-21-2009, 01:48 PM   #8
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by msh View Post
I was under the impression that I'm declaring an array of 5 pointers to string constants.

Code:
int main(void) {
    
    const char* s[5] = 
    {
        "London",
        "Berlin",
        "Paris",
        "Madrid",
        "Lisbon"
    };
    
    for (int i = 0; i < 5; i++) {
        std::cout << s[i] << std::endl;
    }
    
    return 0;
}
I'm with you -- five pointers to string constants.

If this is supposed to match your function definition thing, well, it doesn't. The const here refers to the fact that your strings are constants; the const in your function declaration says that the array won't be changed (i.e., it's a char * const arr[]).
tabstop is offline   Reply With Quote
Old 07-21-2009, 02:58 PM   #9
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by msh View Post
I was under the impression that I'm declaring an array of 5 pointers to string constants.
That's right. You're defining an array of 5 elements of type const char* (pointer to const char, not const pointer to char).
(Although your function wants a constant POINTER to char*.)
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-22-2009, 04:59 AM   #10
msh
Novice
 
Join Date: Jul 2009
Posts: 32
Well. I managed to sort it out. Apparently, I was, as they say on the Internet, doing it wrong.

Code:
template <typename T> T findMax(T arr[], int n);

template <> const char* findMax(const char* arr[], int n);
This works as I want it to, albeit I'm still unsure about the whole specialization thing.
msh is offline   Reply With Quote
Old 07-22-2009, 05:06 AM   #11
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
As explained, it has nothing to do with specialization and everything to do with you using the improper types.
const char* arr[] -> Arrays of pointers to const char.
const char** arr -> Pointers to pointers to const char (can also be seen as an array of pointers to const char).
char* const arr[] -> Constant array of pointers to char.
char** const arr -> Constant pointers to pointers to char (can also be seen as a constant array of pointers to char).
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-22-2009, 05:25 AM   #12
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,439
By the way, usually overloading functions is better than specializing them.
__________________
All the buzzt!
CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
CornedBee is offline   Reply With Quote
Old 07-22-2009, 10:44 AM   #13
Registered User
 
linuxdude's Avatar
 
Join Date: Mar 2003
Location: Louisiana
Posts: 926
Quote:
Originally Posted by CornedBee View Post
By the way, usually overloading functions is better than specializing them.
I'm confused by what you mean here? If you have a template, then you will specialize functions. If you don't then you can overload them. Could you be more elaborate?
linuxdude is offline   Reply With Quote
Old 07-22-2009, 10:47 AM   #14
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
Quote:
Originally Posted by linuxdude
I'm confused by what you mean here? If you have a template, then you will specialize functions. If you don't then you can overload them. Could you be more elaborate?
You can overload even when a function template is involved.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 07-22-2009, 10:54 AM   #15
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Code:
template<typename T> T func();
template<typename T> T func(T);
template<typename T> T func(T, T);
template<typename T, typename T2> T func();
template<typename T, typename T2> T func(T2);
template<typename T, typename T2> T func(T2, T2);
Overloading...
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Tags
function, specialization, template

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Game Pointer Trouble? Drahcir C Programming 8 02-04-2006 02:53 AM
<Gulp> kryptkat Windows Programming 7 01-14-2006 01:03 PM
Post... maxorator C++ Programming 12 10-11-2005 08:39 AM
Dikumud maxorator C++ Programming 1 10-01-2005 06:39 AM
C++ compilation issues Rupan C++ Programming 1 08-22-2005 05:45 AM


All times are GMT -6. The time now is 08:27 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22