Explicit vs. Implicit Template Specialization - C++ template

C++ examples for template:template function

Description

Explicit vs. Implicit Template Specialization

Demo Code

#include <iostream>

using namespace std;

template <typename T>
T Add(const T& a, const T& b)
{
    return a + b;
}

template <typename T>
void Print(const T& value1, const T& value2, const T& result)
{
    cout << "The result of adding" << endl;
    cout << value1 << endl;//from  w  w w  .  j av a2s .  co m
    cout << "to" << endl;
    cout << value2 << endl;
    cout << "is" << endl;
    cout << result;

    cout << endl << endl;
}

int main(int argc, char* argv[])
{
    const int number1{ 1 };
    const int number2{ 2 };
    const int intResult{ Add(number1, number2) };
    Print(number1, number2, intResult);

    const float floatResult{ Add(static_cast<float>(number1), static_cast<float>(number2))
};
    Print<float>(number1, number2, floatResult);

    return 0;
}

Result


Related Tutorials