fork download
  1. #include <iostream>
  2.  
  3. class IBase
  4. {
  5. public:
  6. virtual ~IBase() = default;
  7.  
  8. //a static member identifying IBase (e.g. "iid.base")
  9. static const char* const IID; //initialize in implementation file
  10. //[...]
  11. };//class IBase
  12.  
  13. class IDerived : public IBase
  14. {
  15. public:
  16. virtual ~IDerived() = default;
  17.  
  18. //a static member identifying IDerived (e.g. "iid.derived")
  19. static const char* const IID; //initialize in implementation file
  20. //[...]
  21. };//class IDerived
  22.  
  23. class IEvenMoreDerived : public IDerived
  24. {
  25. public:
  26. virtual ~IEvenMoreDerived() = default;
  27.  
  28. //missing static const member IID!
  29. //[...]
  30. };//class IEvenMoreDerived
  31.  
  32. const char* const IBase::IID = "IBase";
  33. const char* const IDerived::IID = "IDerived";
  34.  
  35. template <typename T>
  36. struct IdName
  37. {
  38. static const char* const IID;
  39. };
  40.  
  41. template <> const char* const IdName<IBase>::IID = "IBase";
  42. template <> const char* const IdName<IDerived>::IID = "IDerived";
  43.  
  44. template<typename T>
  45. void queryIID()
  46. {
  47. std::cout << T::IID << std::endl; // IEvenMoreDerived::IID is resolved as IDerived::IID
  48. std::cout << IdName<T>::IID << std::endl; // link error for IEvenMoreDerived::IID.
  49. }
  50.  
  51. int main()
  52. {
  53. queryIID<IBase>();
  54. queryIID<IDerived>();
  55. queryIID<IEvenMoreDerived>();
  56. }
  57.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/home/i48Knd/ccoAhRio.o: In function `main':
prog.cpp:(.text.startup+0x1b5): undefined reference to `IdName<IEvenMoreDerived>::IID'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty