Thread: Function template specialization.

  1. #1
    Novice
    Join Date
    Jul 2009
    Posts
    568

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Novice
    Join Date
    Jul 2009
    Posts
    568
    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.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    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"
    };

  6. #6
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    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;
    }

  7. #7
    Novice
    Join Date
    Jul 2009
    Posts
    568
    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;
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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[]).

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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*.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Novice
    Join Date
    Jul 2009
    Posts
    568
    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.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  13. #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?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM

Tags for this Thread