V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
corvus
V2EX  ›  问与答

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

  •  
  •   corvus · Jan 7, 2016 · 1596 views
    This topic created in 3763 days ago, the information mentioned may be changed or developed.
    // 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*元素的数组不能一起用么?

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3261 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 13:48 · PVG 21:48 · LAX 06:48 · JFK 09:48
    ♥ Do have faith in what you're doing.