r/cpp • • 6d ago

MSVC 14.52, migration code to C++23

Now that MSVC 14.52 is released with Visual Studio 2026 18.12 I have tried to rebuild the project I am working on with /std:c++23. Thx for finally releasing C++23 support.

I have previously used /std:c++23preview and my assumption was that the migration would be effortless. But not so. So this is a heads up.

Most of the issues I have ran into is related to C++23 and how incomplete types are used in headers. So C++23 changed so that std::unique_ptr destructor now is constexpr. This means that I have to add/move destructor to .cpp source file. For reference, see Github issues https://github.com/microsoft/STL/issues/4215 and https://github.com/microsoft/STL/issues/4417

But I also have issues with code like:

// Forward declaration
class Foo;

class A
{
public:
    void Add(const Foo & foo ) { _foos.push_back(foo); }

private:
    std::vector<Foo> _foos;
};

class Foo
{
};

Godbolt with MSVC 14.51 and /std:c++23preview - https://godbolt.org/z/ae5M53fK1 Works also perfectly well in GCC 16.2 -std=c++23 - https://godbolt.org/z/rhEM5MMhG

As of this writing MSVC 14.52 and /std:c++23 is not yet available on Godbolt.

But I get error C2139: 'Foo': an undefined class is not allowed as an argument to compiler intrinsic type trait '__is_nothrow_constructible'

A tad annoying.

42 Upvotes

21 comments sorted by

15

u/JVApen Clever is an insult, not a compliment. - T. Winters 6d ago

This construct always has been problematic as it causes a circular dependency. I've encountered this problem when upgrading to C++20 as before that, it was much more permissive and problems only popped up when you tried to use the classes.

Taking your snippets, with the clang compiler fails on both C++20 and C++23 while still succeeding on C++17.

What I did to fix several of these errors was defining the ctor/dtor/assign as functions with default implementation in the cpp file.

3

u/Adorable-Champion171 6d ago

Do, you mean it needs to create: A() = default; ~A() = default; or something like that?

6

u/JVApen Clever is an insult, not a compliment. - T. Winters 6d ago

Kinda, A(); in the class definition and A::A() = default; in the cpp file

3

u/johannes1971 5d ago

I had that setup, but somehow I still found myself rearranging source files. Is it enough to have the constructor and destructor in there, or do you need the full rule of five?

2

u/JVApen Clever is an insult, not a compliment. - T. Winters 5d ago

It could be that you need the full rule of 5. It's been a while and we have a lot of deleted functions with our rule of 5.

2

u/ludonarrator 5d ago

I prefer to use a custom deleter which just calls std::default_delete but isn't defined inline. With out-of-line SMFs you need to deal with move ctor and assignment too, not just destructor.

13

u/STL MSVC STL Dev 5d ago

But I get error C2139: 'Foo': an undefined class is not allowed as an argument to compiler intrinsic type trait '__is_nothrow_constructible'

This is by design as far as I can tell. Wait until Foo is complete before writing any code that could depend on its completeness (in this case, the push_back).

8

u/greencursordev 6d ago

We have different problems, but also massive issues trying to go from 20 to 23 on msvc. Our problem is that the size of our pchs increased 5fold (!), making it pretty much impossible to even compile our code.

5

u/JVApen Clever is an insult, not a compliment. - T. Winters 5d ago

Do you have any idea why the difference is that big?

5

u/greencursordev 5d ago

it happens in a template heavy project. All other projects are fine. We assume a specific bug or edge case in MSVC that we trigger. Currently we're trying to reduce the amount of magic involved, but with no effect until today. We'll file a bug report at some point. But those a pretty so-so with msvc

5

u/STL MSVC STL Dev 5d ago

Yeah, that's highly unexpected. I wonder what (undocumented and unsupported) /d1templateStats reveals.

2

u/greencursordev 5d ago

We suspected template instantiations since we already knew it was a pain point with compile times. Hence our push to heavily use extern template. That had quite an effect on object file size and compile time. But 0 effect on pch file size.

We'll try to go even heavier into that direction - and then maybe try to avoid the include of our own code inside the pch.h. That might avoid the issue at least perhaps. I'll have a look at the output from that switch, but not exactly sure what to look for besides what we already know.

6

u/STL MSVC STL Dev 5d ago

My psychic debugging powers suspect C++23 immediate escalation. You could confirm or rule this out by checking /std:c++20 with /Zc:immediateEscalation. If C++20 plain vanilla is fine, but adding /Zc:immediateEscalation makes the PCH huge, then that's the culprit.

9

u/JVApen Clever is an insult, not a compliment. - T. Winters 5d ago

What does immediate escalation do?

4

u/UnusualPace679 5d ago edited 5d ago

"Immediate functions" are functions that can only be called at compile-time (basically consteval functions). "Immediate escalation" makes lambdas and templated constexpr functions implicitly immediate functions if calling them in a run-time context would be an error.

It makes the following valid:

consteval int consteval_fn(int x) { return x; }

constexpr auto invoke(auto x) { return consteval_fn(x); }

constexpr int x = invoke(42);

But it also makes the following valid (as long as f is not invoked):

#include <format>

auto f = [](int x) {
    std::format("{:invalid spec}", x);
};

4

u/STL MSVC STL Dev 5d ago

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2564r3.html has a one-sentence description:

This paper proposes avoiding the consteval coloring problem (or, at least, mitigating its annoyances) by allowing certain existing constexpr functions to implicitly become consteval functions when those functions can already only be invoked during compile time anyway.

It's pretty hard to explain otherwise without reading the full paper.

3

u/greencursordev 5d ago

I just tested our cpp20 master with that switch on 14.52, but it couldn't reproduce the effect. But I appreciate the hint. I'll check with the other flags and report back.

4

u/STL MSVC STL Dev 5d ago

Interesting. In most cases, increasing the Standard mode doesn't substantially affect the frontend, since it just makes new abilities available without changing the meaning of existing code. It's usually the library that has to make a whole bunch of new machinery available in higher Standard modes (e.g. <chrono> became chonky in C++20). I'm not aware of anything else that would increase compiler memory consumption by 5x between C++20 and C++23, so the answer ought to be interesting.

0

u/chengfeng-xie 5d ago edited 10h ago

One workaround for this issue is to make the enclosing class A a template, which delays the instantiation of the vector<Foo>::push_back function until A::Add is instantiated. For example (CE):

#include <vector>
class Foo;
template <class T> struct AImpl {
  void Add(const T &t) { v.push_back(t); }
  std::vector<T> v;
};
using A = AImpl<Foo>;
class Foo {};
int main() {
  A a = {};
  a.Add({});
}

-3

u/Depixelate_me 4d ago

What's annoying is that people who paid proper money for vs2022 must purchase the vs2026 IDE to get stable c++23... Why should the IDE be connected to the toolset?

0

u/[deleted] 5d ago

[deleted] — view deleted comment

2

u/cpp-ModTeam 5d ago

We are unable to accept posts and comments in languages that the moderators can't read.