Home » C++ Programming » Templates » Question
  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    template <class T>
    inline T square(T n1)
    {
    T Output;
    Output = n1 * n1;
    return Output;
    };
    template <>
    string square<string>(string Str)
    {
    return (Str + Str);
    };
    int main()
    {
    int k = 4, kk;
    string bb("B");
    kk = square<int>(k);
    cout << k << ": " << kk;
    cout << square<string>(bb) << ":" << endl;
    }
    1. 4:
    2. 4: 16BB:
    3. 16BB:
    4. 16BB: 4:
    5. None of these
Correct Option: B

Template specialization is used when a different and specific implementation is to be used for a specific data type. In this program, We are using integer and character.



Your comments will be displayed only after manual approval.