Overload function template with a nontemplate version - C++ template

C++ examples for template:template function

Description

Overload function template with a nontemplate version

Demo Code

#include <iomanip>
#include <iostream>

// function template printArray definition
template <typename T>
void printArray(const T* const array, int count) {
    for (int i = 0; i < count; ++i) {
        std::cout << array[i] << " ";
    }/*from   w ww  .j  a v  a  2  s  .co  m*/
    std::cout << std::endl;
}
// non template printArray char definition
void printArray(const char* array, int count) {
    for (int i = 0; i < count; ++i) {
        std::cout << std::setw(2) << array[i];

        if (array[i] == ' ') std::cout << "\n";
    }
    std::cout << std::endl;
}
int main(int argc, const char* argv[]) {
    const int CSIZE = 10;

    char charArr[CSIZE] = {'T', 'H', 'I', 'S', ' ', 'I', 'S', ' ', 'A', 'N'};

    printArray(charArr, CSIZE);

    return 0;
}

Result


Related Tutorials