c++constexpr

Works in GCC, MSVC error C3615: constexpr function 'GPSinfo_T::GPSinfo_T' cannot result in a constant expression


I've got a really simple C++ 20 struct with a constexpr ctor. Compiles and runs fine with GCC recent versions, but not MSVC 2022 with latest patches. Is the MSVC compiler incorrect, or have I missed something?

struct GPSinfo_T {
    char fixSources[6];
    constexpr GPSinfo_T() :
        fixSources("NNNNN")
    {};
};

Solution

  • The only member causing the MSVC error is the char array fixSources, as you can see in this minimum reproducable example:

    struct GPSinfo_T {
        char fixSources[6];
        constexpr GPSinfo_T() :
            fixSources("NNNNN")
        {};
    };
    
    int main() {
    }
    

    Live demo 1 (with error on MSVC).

    I am not a language-lawyer and unsure which compiler is correct here.
    But as @MarekR suggested, a possible workaround for MSVC is to use default Member initialization, as shown below (corresponds to the MRE above):

    struct GPSinfo_T {
        char fixSources[6] = "NNNNN";
        constexpr GPSinfo_T() = default;
    };
    
    int main() {
    }
    

    Live demo 2 (all compilers accept).

    Alternatively change fixSources to be a std::array<char, 6> and you'll be able to use member initialization list as in your original code.