fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <typename T>
  5. struct MyTest { // generic implementation for all types
  6. MyTest() {
  7. static_assert(is_pointer<T>::value , "this class must be used with a pointer");
  8. }
  9. };
  10.  
  11. template <typename T>
  12. struct MyTest<T*> { // partial specialisation for pointer types
  13. MyTest() {
  14. cout <<"generic impmplementation"<<endl;
  15. }
  16. };
  17.  
  18. struct Base {};
  19. struct Derived : Base {};
  20.  
  21. template <>
  22. struct MyTest<Base*> { // specialisation for Base
  23. MyTest() {
  24. cout <<"specialized implementation for Base"<<endl;
  25. }
  26. };
  27.  
  28. template <>
  29. struct MyTest<Derived*> { // specialisation for Derived
  30. MyTest() {
  31. cout <<"specialized implementation for Derived"<<endl;
  32. }
  33. };
  34.  
  35. //struct Base{} b;
  36. //template<Base* B> struct Test{};
  37. //template<> struct Test<&b>{};
  38.  
  39. //struct Derived : Base{} d;
  40. //template<> struct Test<&d>{};
  41.  
  42.  
  43. int main() {
  44. // MyTest<Base> mt1; //<== faills because Base is not a poiner
  45. MyTest<int*> mt0;
  46. MyTest<Base*> mt2;
  47. MyTest<Derived*> mt3;
  48.  
  49. // your code goes here
  50. return 0;
  51. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
generic impmplementation
specialized implementation for Base
specialized implementation for Derived