Array Sizes in Template Functions

From codegorilla

Jump to: navigation, search

In template functions it is possible to determine the size of an array passed in using template parameters for the size.

This is just a simple example of how to use an array and a size as a parameter to a function and it only applies to compile time sized arrays.

template< typename T, size_t size >
size_t get_size( T const (&var_array)[size] )
{
    return size;
}

void main()
{
    char array[100];
    std::cout << "size is: " << get_size(array) << std::endl;
}
Personal tools