静态类成员定义内初始化的最佳替代方案? (对于 SVN 关键字)

发布于 2024-08-31 19:08:14 字数 1345 浏览 2 评论 0原文

我将 .cpp 文件的扩展 SVN 关键字文字存储在“static char const *const”类成员中,并希望尽可能类似地存储 .h 描述。简而言之,我需要保证静态成员(可能在 .cpp 文件中)的单个实例化为存在于潜在共享 .h 文件中的自动生成的非整数文字。不幸的是,该语言没有尝试解决由于在类定义之外进行赋值而产生的多个实例化,并且明确禁止类定义内部的非整数初始化。我的最佳尝试(使用静态包装内部类)并不太脏,但我真的想做得更好。有没有人有办法模板化下面的包装器或有完全优越的方法?

// 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;
  }
};

有没有办法解决在字符串文字上模板化 hInfoWrap 类的静态链接地址问题?在类定义之外分配的外部字符指针在语言上是有效的,但其失败方式与直接成员初始化基本相同。我明白为什么该语言回避了整个解析问题,但是如果提供反向外部成员限定符,那么定义代码在类定义中对任何调用者都是可见的,但实际上仅在单个特殊点处调用,这将非常方便在别处声明。

无论如何,我离题了。对于我们现有的语言来说,最好的解决方案是什么,模板还是其他?谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江心雾 2024-09-07 19:08:14

可能有一个静态函数?

// Foo.h: 
class Foo {
  static Logger      const verLog;
  static char const*const getHInfo() { return "$Id$"; }
public:
  static char const *const cInfo;
};

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

probably, with a static function?

// Foo.h: 
class Foo {
  static Logger      const verLog;
  static char const*const getHInfo() { return "$Id$"; }
public:
  static char const *const cInfo;
};

// Foo.cpp: static inits called here
char const     *const Foo::cInfo = "$Id$";
Logger          const Foo::verLog(Foo::cInfo, Foo::getHInfo());
触ぅ动初心 2024-09-07 19:08:14

也许你可以用宏做一些事情。

// Foo.h
#define FOO_HINFO "$Id$"

...

// Foo.cpp
char const     *const Foo::hInfo = FOO_HINFO;

我通常避免使用宏,但在这种情况下它可能是最干净的解决方案。

Maybe you could do something with macros.

// Foo.h
#define FOO_HINFO "$Id$"

...

// Foo.cpp
char const     *const Foo::hInfo = FOO_HINFO;

I usually avoid macros, but in this case it might be the cleanest solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文