fork download
  1. #include <iostream>
  2. #include <deque>
  3.  
  4. // Really want to build this list before main() started!
  5. struct ProfilePoint;
  6. static std::deque<ProfilePoint *> pps;
  7.  
  8. // Costly construction, but only ever with literal/constexpr params.
  9. // Templating, etc., also discourages non-local building in reality.
  10. struct ProfilePoint {
  11. ProfilePoint(int id, char const *i) : id_(id), inf_(i) { pps.push_back(this); }
  12. void doStuff() { /* ... */ }
  13. int id_;
  14. char const *const inf_;
  15. };
  16.  
  17. template<class IdDescription>
  18. struct ProfilePoint_{
  19. static ProfilePoint p;
  20.  
  21.  
  22. };
  23.  
  24. template<class IdDescription>
  25. ProfilePoint ProfilePoint_<IdDescription>::p( IdDescription::id(), IdDescription::description() );
  26.  
  27. #define PROFILE_POINT(theid,thedescription) \
  28. struct ppdef_static_class{ \
  29.   static int id(){ return theid; } \
  30.   static const char* description(){ return thedescription; } \
  31.   };\
  32.   static ProfilePoint_<ppdef_static_class>
  33.  
  34. // Functions like this will be called concurrently in reality.
  35. void bar(int cnt) {
  36. for (int i = 0; i < cnt; ++i) {
  37. // Dropping in a local definition/call should be enough to hook in to system
  38. PROFILE_POINT(2, "description is a string literal") pp;
  39.  
  40. pp.p.doStuff();
  41. /* ... */
  42. }
  43. }
  44.  
  45. void dump() {
  46. std::cout << "[";
  47. for (ProfilePoint *pp : pps) { std::cout << " " << pp->id_ << ":" << pp->inf_; }
  48. std::cout << " ]" << std::endl;
  49. }
  50.  
  51. int main() { dump(); bar(5); dump(); } // "[ ]" then "[ 2 ]" in gcc/icc
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
[ 2:description is a string literal ]
[ 2:description is a string literal ]