静态类成员定义内初始化的最佳替代方案? (对于 SVN 关键字)
我将 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能有一个静态函数?
probably, with a static function?
也许你可以用宏做一些事情。
...
我通常避免使用宏,但在这种情况下它可能是最干净的解决方案。
Maybe you could do something with macros.
...
I usually avoid macros, but in this case it might be the cleanest solution.