V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
corvus
V2EX  ›  问与答

C++11 常量静态成员函数 constexpr 数组

  •  
  •   corvus · 2016-01-07 22:11:15 +08:00 · 1351 次点击
    这是一个创建于 3099 天前的主题,其中的信息可能已经有所发展或是发生改变。
    // xx.h
    class XX {
        ...
    public:
        static constexpr char *xarr[2] = {"Spring", "Fall"};
        static const char *arr[2];
       ...
    }
    // xx.cpp
    constexpr char* xarr[2];
    const char* arr[2]=["Spring", "Fall"];
    // main.cpp
    ...
    for (auto str : XX::arr)
        ...
    for (auto str : XX::xarr)
        ...
    ...
    

    上面的代码在 VS2015 中能够编译通过,但运行的话出现这两种情况:

    • 使用 arr 数组时,程序正常;
    • 使用 xarr 数组时就会直接崩溃。

    然后我又试了一下

    static constexpr char carr[2] = {'X', 'Y'};
    

    这个正常运行。。。
    话说这是肿么回事, constexpr 与char*元素的数组不能一起用么?

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1306 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:20 · PVG 07:20 · LAX 16:20 · JFK 19:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.