template with function-style parameter

This is a discussion on template with function-style parameter within the C++ Programming forums, part of the General Programming Boards category; I have been experimenting with a simple array class template two ways: one using only a class parameter, and the ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    595

    template with function-style parameter

    I have been experimenting with a simple array class template two ways: one using only a class parameter, and the other using a function-style parameter. Everything else is the same.

    The version that uses only the class parameter tests out fine. In my test driver I was able to use a variable "arraySize", prompt the user for a value for that variable, and use it to allocate arrays with no errors.

    But in the version that uses a function-style parameter (using "arraySize" as the function-style template parameter), it would not compile until I changed the declaration of arraySize to a constant, for example:
    const int arraySize = 5;
    Of course, I can no longer prompt the user to designate the size.

    Is this normal? Is this a limitation of function-style parameters for templates? Or did I do something else wrong?
    Last edited by R.Stiltskin; 05-21-2003 at 06:51 AM.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The capabilities of templates are derived from the fact that they are 'interpreted' (for lack of a better term) by the compiler at compile time, based on what the parameters are. Essentially, it generates a new copy of all of your code for each unique set of parameters you feed it, and of course, the program has no way of generating this code by itself at runtime (which is what the compiler is for).

    Aside from static size limits for the array class, the 'function-style' version will also cause your program to be significantly larger than necessary since a new copy of the Array class' code will exist for every size array you use.

    Hope thats understandable.
    Cheers.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Thanks, Zach. Your comments are perfectly understandable, but give rise to another question.

    Given that a function template's nontype parameters are required to be constant values that are known at compile time, why would a programmer ever use that format?

    Using my code as an example, is there ever a good reason to do this:
    Code:
    template<class Type, int s>
    Array<Type, s>::Array(){
        a = new Type[size = s];
        }
    ...
    };
    
    int main(){
        const int size=10;
        ...
        Array<char,size> a1;
        ...
    }
    rather than this
    Code:
    template<class Type>
    Array<Type>::Array(int s=10){
        a = new Type[size = s];
        }
    ...
    };
    
    int main(){
        const int csize=5;
        int vsize;
        ...
        cout << "Enter array size: " << endl;
        cin >> vsize;
        Array<char> ca(csize);   // any of these can be used to make an array
        Array<string> sa;        //   "    "     "
        Array<int> ia(vsize);    //   "    "     "
        ...
    }
    since the latter version allows for compile-time instantiation using a constant OR a default parameter OR run-time instantiation?

    By the way, I love your signature line.
    Last edited by R.Stiltskin; 05-21-2003 at 06:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. function formal parameter pointer style for arrays
    By curlious in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2003, 06:18 PM

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