Visual C++ does not accept constexpr string while gcc can

zzzhhh 40 Reputation points
2024-09-27T19:44:15.4433333+00:00

Code:

constexpr std::string cs{ "hello" };

GCC is happy with it but Visual C++ does not accept the constexpr string with error:

error C2131: expression did not evaluate to a constant

std::string is a literal class since C++20. Why can't Visual C++ compile the above statement?

I am using VS2019 version 16.11.36. I have specified the C++ language standard to be C++20.

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,717 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Minxin Yu 11,586 Reputation points Microsoft Vendor
    2024-09-30T01:40:51.7533333+00:00

    Hi, @zzzhhh

    Regarding the debug mode not being able to compile, it is by design:
    https://github.com/microsoft/STL/issues/4427

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.