Android 官方 code 中的这种用法:匿名命名空间的 static const 变量

2019-09-24 09:03:49 +08:00
 spark104

各位大佬,Android 官方 code 中有这种用法:匿名命名空间的 static const 变量

namespace {
	static const char* const kHello = "Hello";
	static const std::string kWorld = "World";
}

但是我看 cpp primer 5th 书上说匿名命名空间中定义的变量拥有静态生命周期,那 android code 中的 static 有何意义呢?而且 cpp primer 也推荐用匿名命名空间取代文件中的静态声明,不是很理解 android 的这个用法,有没有哪位能解惑下?

3791 次点击
所在节点    C
8 条回复
xbigfat
2019-09-24 09:23:03 +08:00
我怎么没看懂这是什么语言... 我猜测不是 java 也不是 kotlin
w3313003
2019-09-24 09:25:32 +08:00
@xbigfat C 艹
arrow8899
2019-09-24 09:29:24 +08:00
static is redundant, because anonymous namespace limits the visibility of definitions to a single translation unit
uxstone
2019-09-24 09:38:42 +08:00
@xbigfat C 艹
spark104
2019-09-24 09:42:19 +08:00
@arrow8899 好像真是多余的,估计是这个写代码的比较 low,只有一个文件夹下的几个 cpp 文件这样写,其他地方的匿名命名空间里面都没有 static 关键字了。感谢~
icris
2019-09-24 09:52:54 +08:00
Java 接口里不也都写 public static final String,三个关键字都是多余的。坏处没有,好处是确实只看一行就能知道 public static final
wysnylc
2019-09-24 10:14:11 +08:00
@icris #6 请问 public static final 不多余的情况怎么写?
icris
2019-09-24 10:22:29 +08:00
@wysnylc #7
接口:public interface I
webStorm 里提示是这样的
Modifier 'public' is redundant for interface fields more... (⌘F1)
Modifier 'static' is redundant for interface fields more... (⌘F1)
Modifier 'final' is redundant for interface fields more... (⌘F1)
你的问题如果是应该怎么写,那结论是应该不写

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/603564

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX