V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
spark104
V2EX  ›  C

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

  •  
  •   spark104 · 2019-09-24 09:03:49 +08:00 · 3754 次点击
    这是一个创建于 1649 天前的主题,其中的信息可能已经有所发展或是发生改变。

    各位大佬,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 的这个用法,有没有哪位能解惑下?

    8 条回复    2019-09-24 10:22:29 +08:00
    xbigfat
        1
    xbigfat  
       2019-09-24 09:23:03 +08:00
    我怎么没看懂这是什么语言... 我猜测不是 java 也不是 kotlin
    w3313003
        2
    w3313003  
       2019-09-24 09:25:32 +08:00
    @xbigfat C 艹
    arrow8899
        3
    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
        4
    uxstone  
       2019-09-24 09:38:42 +08:00
    @xbigfat C 艹
    spark104
        5
    spark104  
    OP
       2019-09-24 09:42:19 +08:00
    @arrow8899 好像真是多余的,估计是这个写代码的比较 low,只有一个文件夹下的几个 cpp 文件这样写,其他地方的匿名命名空间里面都没有 static 关键字了。感谢~
    icris
        6
    icris  
       2019-09-24 09:52:54 +08:00
    Java 接口里不也都写 public static final String,三个关键字都是多余的。坏处没有,好处是确实只看一行就能知道 public static final
    wysnylc
        7
    wysnylc  
       2019-09-24 10:14:11 +08:00
    @icris #6 请问 public static final 不多余的情况怎么写?
    icris
        8
    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)
    你的问题如果是应该怎么写,那结论是应该不写
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2967 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 06:32 · PVG 14:32 · LAX 23:32 · JFK 02:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.