C++ | Templates | Question 5

Output of following program? Assume that the size of char is 1 byte and size of int is 4 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 A  {
    T x;
    U y;
    static int count;
};
  
int main()  {
   A<char, char> a;
   A<int, int> b;
   cout << sizeof(a) << endl;
   cout << sizeof(b) << endl;
   return 0;
}

chevron_right


(A)

6
12

(B)

2
8

(C) Compiler Error: There can not be more than one template arguments.
(D)

8
8


Answer: (B)

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

Quiz of this Question

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

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.