fork download
  1. #include <iostream>
  2.  
  3. template <class T>
  4. class B
  5. {
  6. public:
  7.  
  8. B()
  9. {
  10. std::cout << "Constructing with default ctor\n";
  11. }
  12.  
  13. B(int)
  14. {
  15. std::cout << "Initializing with int param\n";
  16. }
  17.  
  18. B(double)
  19. {
  20. std::cout << "Initializing with double param\n";
  21. }
  22. };
  23.  
  24. template <class T>
  25. class A
  26. {
  27. public:
  28.  
  29. static const B<T> member;
  30. };
  31.  
  32. // for an int
  33. template<>
  34. const B<int> A<int>::member(3);
  35.  
  36. // for a double
  37. template<>
  38. const B<double> A<double>::member(50.3);
  39.  
  40. // use no-arg constructur with A<void>
  41. template<>
  42. const B<void> A<void>::member{}; // = B<void>();
  43.  
  44.  
  45. int main(int argc, char* argv[])
  46. {
  47. // (printing so I don't get a warning, which will be treated as an error)
  48. std::cout << &A<int>::member;
  49. std::cout << &A<double>::member;
  50. std::cout << &A<void>::member;
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
Initializing with int param
Initializing with double param
Constructing with default ctor
0x8049b520x8049b510x8049b50