Skip to content
Related Articles

Related Articles

Improve Article

C++ | Templates | Question 6

  • Last Updated : 28 Jun, 2021

Output of following program? Assume that the size of int is 4 bytes and size of double is 8 bytes, and there is no alignment done by the compiler.

using namespace std;
template<class T, class U, class V=double>
class A  {
    T x;
    U y;
    V z;
    static int count;
int main()
   A<int, int> a;
   A<double, double> b;
   cout << sizeof(a) << endl;
   cout << sizeof(b) << endl;
   return 0;







(D) Compiler Error: template parameters cannot have default values.

Answer: (A)

Explanation: templates can also have default parameters. The rule is same all default values must be on the rightmost side.

Since count is static, it is not counted in sizeof.

Quiz of this Question

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.
My Personal Notes arrow_drop_up
Recommended Articles
Page :