best alternative to in-definition initialization of static class members? (for SVN keywords)

Posted by Jeff on Stack Overflow See other posts from Stack Overflow or by Jeff
Published on 2010-05-06T23:24:07Z Indexed on 2010/05/06 23:28 UTC
Read the original article Hit count: 127

I'm storing expanded SVN keyword literals for .cpp files in 'static char const *const' class members and want to store the .h descriptions as similarly as possible. In short, I need to guarantee single instantiation of a static member (presumably in a .cpp file) to an auto-generated non-integer literal living in a potentially shared .h file. Unfortunately the language makes no attempt to resolve multiple instantiations resulting from assignments made outside class definitions and explicitly forbids non-integer inits inside class definitions. My best attempt (using static-wrapping internal classes) is not too dirty, but I'd really like to do better. Does anyone have a way to template the wrapper below or have an altogether superior approach?

// Foo.h: class with .h/.cpp SVN info stored and logged statically
class Foo {
  static Logger      const verLog;
  struct hInfoWrap;
public:
  static hInfoWrap   const hInfo;
  static char const *const cInfo;
};

// Would like to eliminate this per-class boilerplate.
struct Foo::hInfoWrap {
  hInfoWrapper() : text("$Id$") { }
  char const *const text;
};

...

// Foo.cpp: static inits called here
Foo::hInfoWrap  const Foo::hInfo;
char const     *const Foo::cInfo = "$Id$";
Logger          const Foo::verLog(Foo::cInfo, Foo::hInfo.text);

...

// Helper.h: output on construction, with no subsequent activity or stored fields
class Logger {
  Logger(char const *info1, char const *info2) {
    cout << info0 << endl << info1 << endl;
  }
};

Is there a way to get around the static linkage address issue for templating the hInfoWrap class on string literals? Extern char pointers assigned outside class definitions are linguistically valid but fail in essentially the same manner as direct member initializations. I get why the language shirks the whole resolution issue, but it'd be very convenient if an inverted extern member qualifier were provided, where the definition code was visible in class definitions to any caller but only actually invoked at the point of a single special declaration elsewhere.

Anyway, I digress. What's the best solution for the language we've got, template or otherwise? Thanks!

© Stack Overflow or respective owner

Related posts about c++

Related posts about header