C++ | Templates | Question 6

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<iostream>
#include<stdlib.h>
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;
}

chevron_right


(A)

16
24

(B)



8
16

(C)

20
28

(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



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.