What will be the output of the following C++ code? [code] #include <iostream> using namespace std; template <class T> inline

What will be the output of the following C++ code?
Code:
    #include <iostream>
    using namespace std;
    template <class T>
    inline T square(T x)
    {
        T result;
        result = x * x;
        return result;
    };
    template <>
    string square<string>(string ss)
    {
        return (ss+ss);
    };
    int main()
    {
        int i = 2, ii;
        string ww("A");
        ii = square<int>(i);
        cout << i << ": " << ii;
        cout << square<string>(ww) << ":" << endl;
    }
A. 2: 4AA:
B. 2:4
C. AA
D. 2:4A
 
Answer: a
Explanation: 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.
Output:
Code:
$ g++ spec.cpp
$ a.out
2: 4AA:
 

Tìm kiếm

Back
Top