2

I was reading this answer about constexpr vector in C++20, where the poster gave this code

constexpr int f() {
    std::vector<int> v = {1, 2, 3};
    return v.size();
}

static_assert(f() == 3);

which he asserts is correct under C++20. He also gave this successfully compiled (in MSVC v19) Godbolt snippet.

However as I change the compiler to gcc 10 (which is supposed to support C++20 and constexpr container), compiler emits error messages complaining roughly about vector<int> being non-literal type because it does not have constexpr destructor.

This kind of compile error persist to gcc 11, though. In gcc 12.1, it compiles successfully.

Having browsed the gcc 12.1 changes history, I found out this seemingly relevant change

Several C++23 features have been implemented

  • ...
  • P2242R3, Non-literal variables (and labels and gotos) in constexpr functions (PR102612)

My questions are:

  1. vector do have a constexpr destructor since C++20 right? (see cppref) Why does gcc before 12 complains as not?

  2. Whether the above code is strictly legal under C++20? Which one of MSVC or gcc is not standard conformant?

  3. TBH I'm not in the stage to fully understand the proposal. Can you explains what it is about? Which problems does it try to solve?

Masquue
  • 166
  • 8
  • The relevant change is [this](https://github.com/gcc-mirror/gcc/commit/1ae8edf5f73ca5c3bf132cc52825dc1f709499dd). That is, only gcc12 supports `constexpr` vector. The P2242R3 you mentioned does not make much sense here. – 康桓瑋 Jul 11 '22 at 16:53
  • @康桓瑋 Does it mean the compiler support for `constexpr` container at [cppref](https://en.cppreference.com/w/cpp/compiler_support#:~:text=constexpr%20container%20operations) is wrong? Cause I was using it as the reference. – Masquue Jul 11 '22 at 16:58
  • 1
    P2242R3 is only relevant with `if (std::is_constant_evaluated()) {`, when you have variables conditionally depending on whether something is constant evaluated or not. That is not the case in your example. – Goswin von Brederlow Jul 11 '22 at 16:58
  • Support for C++20 is ongoing in compilers and you might find missing or incomplete things, especially the older the compiler you test is. If something isn't working in gcc 11 but works in gcc 12.1 then assume it has been implemented or fixed since gcc 11 was released and move on. – Goswin von Brederlow Jul 11 '22 at 16:59
  • 2
    I think the link to cppref says support for constexpr std::vector is in GCC 12. – Eljay Jul 11 '22 at 17:02
  • @Masquue You are wrong, because the "`constexpr` container operations" proposal P0784R7 mentioned in your link does not make changes to `std::vector`. [P1004R2](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1004r2.pdf) is the correct paper. – 康桓瑋 Jul 11 '22 at 17:03
  • @Eljay Yes, sir. You are right! Damn it, there are just so many entries in that table and I was searching using the wrong term. – Masquue Jul 11 '22 at 17:04
  • I 100% concur. There are a lot of entries, and it is not always clear what the brief description refers to. Compiler support for C++20 for many compilers is still a "work in progress", with plenty of gaps. – Eljay Jul 11 '22 at 17:07

0 Answers0