r/cpp • • May 18 '26

A two phase to-string API? First compute the total size of the single allocation then populate the bytes?

1 Upvotes

I'm trying to see if there is any prior art in the to-string space that accomplishes a two phase approach. In theory, you could design an API that first asks the data "How many bytes would it take to make you into a string?". From there it could allocate memory with that capacity. Then it could provide that allocation to the same data to populate the bytes.

I want it to be very light weight and easy to add to a type. The `AbslHashValue(...)` API really nails the ergonomics of an extension point in C++, imo. But when it comes to to-string, it gets pretty hairy pretty fast.

`AbslHashValue(...)` benefits from the fact that the resulting hash has a fixed bit width. You just combine/combine_contiguous recursively and you're done.

Some hypothetical `MyToString(...)` would need to likely be split into two functions. `MyToStringSize(...)` and `MyToStringValue(...)` which already makes it more obnoxious to add support for in your type.

But it gets worse. What if inclusion of the type names is important? I can imagine wanting a lever at the top level that says do or do not include them. So for the case where you do include them, how do you succinctly compute the length of namespace + scope-resolution-operator + type name.

And what about templates? Do you also include the angle brackets? Do you recursively include type names between them? And what about potential line noise like allocator types? I can see wanting to include them and not wanting to include them.

Further, what about hashtables? If you store the keys and values in separate ranges for a more data oriented design, how do you model the fact that each K-V pair goes together? You don't want to copy them because that might be expensive. So do you supply a proxy object where it has two pointers? Now that means you have to build an entire TYPE inside your type just to support to-string. Not very ergonomic.

Anyway, wanted to discuss this to see if anyone has ideas in this space. It seems to me that to-string as an operation should be unambiguously single allocation. But unless I'm mistaken, `absl::StrCat(...)` and other such APIs only "limit" the number of allocations and cannot put the upper bound at exactly 1.


r/cpp • • May 18 '26

CppCon CppCon 2026 Hudson River Trading Scholarship

0 Upvotes

Hi all, unfortunately got rejected for the above, but the denial email mentions a "priority waitlist." Supposedly, if someone who got it ends up not going I might have another chance, but I have a feeling that's cope and they send that out to everyone they deny.

Posting to ask if any other student who applied and was denied to the scholarship got a similar rejection or if they got something different.

If you got accepted, congratulations, and make the best of it!


r/cpp • • May 17 '26

std::is_heap could be faster - Arthur O'Dwyer

Thumbnail quuxplusone.github.io
104 Upvotes

r/cpp • • May 16 '26

What do you use for logging in your C++ codebase? What are the pros/cons?

70 Upvotes

I've used Google's logging library in the past and I would give it a solid B- grade. It gets the job done. I really dislike the streaming operator, but meh it's not that bad. It just feels weird if I concatenate the string beforehand: LOG(INFO) << absl::StrCat(strs...);

As I embark on my new project, I want to make intelligent decisions about what idioms to favor. Logging is a big one, so I'd love to hear what people out there use.

In particular, if there is a library that uses reflection to give named arguments, that would be perfect! My ideal callsite would look like this:

my::LogInfo("Hello, {person}!", {
  .person = GetPersonObject(),
});

It would also be cool to get assertions baked into it, though that complicates the design space.

Anyone wanna chime in?


r/cpp • • May 15 '26

C++26 Shipped a SIMD Library Nobody Asked For

Thumbnail lucisqr.substack.com
164 Upvotes

r/cpp • • May 14 '26

C++: The Documentary TRAILER│COMING JUNE 4th

Thumbnail youtu.be
226 Upvotes

r/cpp • • May 14 '26 Galaxy Brain

Encountered a `#pragma once` failure in the wild

160 Upvotes

A header-only C++ library, headers guarded by #pragma once. We use relative includes internally. We also ship compiled WASM bindings for TypeScript.

A dockerised emscripten build on Apple Silicon under linux/amd64 emulation, the repo mounted in as a bind volume. Every translation unit reports redefinition errors: the same headers included multiple times in a single TU.

#pragma once dedupes by file identity; inode and device, in practice. On a bind mount under amd64 emulation on macOS, the same file reached through different ..-laden relative paths is not always recognised as the same file. The pragma never fires.

The problem goes away if you copy the source files into the container.


r/cpp • • May 14 '26

MSVC Build Tools Preview updates - May 2026

Thumbnail devblogs.microsoft.com
56 Upvotes

Hi, one of the MSVC dev leads here.

Here's what's new in the MSVC Build Tools Preview since mid-April.

As a reminder, the MSVC Build Tools Preview is an optional Visual Studio installer component. Instructions to install & use the latest preview bits are at https://aka.ms/msvc/preview .

If you need a primer on MSVC versioning, see https://www.reddit.com/r/cpp/comments/1smfgdu/demystifying_msvc_versioning_for_1450_later/ or https://learn.microsoft.com/en-us/cpp/overview/compiler-versions , but essentially:

  • 14.52.* is the latest preview, updated regularly with bits from our development branch.
  • 14.51.* is the latest default toolset, which is now GA, and in support for 9 months.
  • 14.50.* & older releases are still available as side-by-side installation components.

r/cpp • • May 14 '26

LWG 2839 inquiry

20 Upvotes

I’m working on a string library that wraps std::basic_string and uses a “rvalue-aware” API pattern:

  • const& overload returns a new string
  • && overload tries to reuse the existing buffer and returns by value

The intended usage is this kind of chaining:

my_string s = "...";
s = std::move(s).trim();
s = std::move(s).to_lowercase();

I have many cases in which I found performance gains by defining a "create new object" version of a transformation alongside and inplace one and this pattern allows me to reuse method identifiers.

A simplified example looks like this:

struct my_string {
    std::string data;

    my_string trim() const& {
        my_string result;
        copy_trimmed_part_to(result);  // determine subrange after trim and only copy that part
        return result;
    }

    my_string trim() && {
        trim_inplace();                // mutate the current object / reuse buffer
        return std::move(*this);       // return by value
    }

    // ...

    my_string& operator=(my_string&&) = default;
};

My question is about LWG 2839, which talks about self-move-assignment.

At first glance, s = std::move(s).trim() feels like it might be related, because the && overload operates on s and then the result is assigned back into s. But the returned object is still a separate prvalue result, even if it reuses the original buffer internally.

So:

  1. Is s = std::move(s).trim() actually covered by the self-move-assignment concerns from LWG 2839?
  2. Or is LWG 2839 only relevant to direct cases like s = std::move(s)?
  3. If the wrapper is built on top of std::basic_string, is there any subtle standards issue with this API pattern itself?
  4. Is the real risk instead overlap/aliasing in APIs like replace, insert, etc. when additional arguments may refer into the same string?

I’m interested both in the strict standards view and in whether this pattern is considered sound library design in practice.


r/cpp • • May 14 '26

Reverse Dependency Ordering for C++ Includes

Thumbnail nukethebees.com
39 Upvotes

r/cpp • • May 14 '26

Your C++ struct is the schema: a proto3 serializer in C++26 reflection

67 Upvotes

I built a header-only proto3 wire-format library with one constraint: no .proto files, no codegen, no descriptor runtime. The user writes a plain C++ struct, and that struct is the schema:

#include "proto3.hpp"
struct SearchRequest {
std::string query; // field 1
std::int32_t page_number; // field 2
std::int32_t results_per_page; // field 3
};
SearchRequest req{"hello", 1, 10};
std::string bytes = proto3::serialize(req); // -> proto3 wire bytes
SearchRequest back = proto3::deserialize<SearchRequest>(bytes);

blog: Your C++ struct is the schema: a proto3 serializer in C++26 reflection

github: struct_proto26


r/cpp • • May 14 '26

Manipulating URLs with Boost.URL

Thumbnail youtube.com
16 Upvotes

Utah C++ Programmers has released a new video: Manipulating URLs with Boost.URL

URLs! They're everywhere. You've tried extracting pieces from them with std::string methods and found it cumbersome. You tried parsing them with regular expressions and found them failing. You tried building them with string concatenation and found weird edge cases that required adding more and more complicated string processing.

How can something seem so simple yet be so complicated?

This month, Richard Thomson will give us a tutorial on building and decomposing URLs using the Boost.URL library.

Resources:


r/cpp • • May 13 '26

What's the highest C++ standard you can use with Boost.Graph today?

36 Upvotes

Dear Boost.Graph user,

During our Boost Graph Modernization Workshop in Paris, one of the most frequent discussion points was the C++ standard version the Boost Graph Library should require. Modernization is intuitively in tension with C++14 😉

Some context worth knowing:

  • Boost.Graph is already C++14
  • A lot of API modernization can be done without bumping the minimum.
  • Some modernization changes would land cleaner on a newer standard
  • We don't yet have a clear enough picture of our users' constraint space to inform our discussion about a potential baseline bump

We would deeply appreciate if you could find time to either answer this simple Github survey ( https://github.com/boostorg/graph/discussions/490 ), or to just drop a comment with your answer (C++14/17/20/23) in this reddit discussion !

Also please indicate if you’re a BGL active user or an observer eager to give feedback (both are welcome!)

Thank you for your time !

Edit: To clarify we're asking what's the oldest C++ standard your project needs BGL to keep supporting (i.e. the version past which a bump would break you or force you to pin a version). Answers: C++14 / 17 / 20 / 23.


r/cpp • • May 13 '26

C++26: Standard library hardening

Thumbnail sandordargo.com
94 Upvotes

r/cpp • • May 12 '26

cost of enum-to-string: C++26 reflection vs the old ways

Thumbnail vittorioromeo.com
179 Upvotes

r/cpp • • May 12 '26

MSVC Build Tools v14.51 now generally available

Thumbnail devblogs.microsoft.com
78 Upvotes

Microsoft released MSVC Build Tools v14.51 as the default compiler in Visual Studio 2026 18.6.

Notable additions include:

  • additional C++23 language features
  • performance improvements in code generation
  • preview support for Intel APX

VS 2026 + Copilot users can use the @Modernize agent (currently in public preview) to help resolve upgrade-related build issues.


r/cpp • • May 12 '26

WG21 mailing for first feature meeting of C++29

Thumbnail open-std.org
58 Upvotes

r/cpp • • May 12 '26

I benchmarked duplicate detection strategies in C++ across every workload i could think of

Thumbnail dubeykartikay.com
29 Upvotes

My earlier post received a lot of feedback about it being too naive, narrow, clickbait. So i made a benchmark suite targeted towards de-duplication and wrote about that in this post!


r/cpp • • May 12 '26

What the heck is Reflection?

Thumbnail murathepeyiler.com
107 Upvotes

r/cpp • • May 12 '26

Pre-Brno Mailing

26 Upvotes

The Pre-Brno "enhanced mailing" is here
https://wg21.org/mailing/2026-05/


r/cpp • • May 11 '26

The Heap: Compile-Time Map and Compile-Time Mutable Variable with C++26 Reflection

Thumbnail stackoverflow.blog
82 Upvotes

Stack Overflow is introducing The Heap, a place for community articles. I had the privilege of having my article one of the first to be published on it.

It is about how you can use the new reflection features to create compile-time maps and a trick I call the compile-time mutable variable. I hope you can learn something new from it!

If you have an interesting article, I encourage you to try submitting it to The Heap!


r/cpp • • May 11 '26

std::optional equality comparison operator seems broken for nested optionals

49 Upvotes

While chasing a bug in a program I found out that equality comparison operator T vs optional<T> is broken if T is optional<U>. It is broken in worst possible way - it compiles but for some values it returns wrong results!

Here is an example:
https://godbolt.org/z/v3bcTodGj

Since both sides of eq operator are specialization of optional then following overload is used:

operator==(const optional<T>& lhs, const optional<U>& rhs);

This operator is specified to return
lhs.has_value() != rhs.has_value() ? false :
(lhs.has_value() == false ? true : *lhs == *rhs)

This falls apart for above scenario since lhs has value while rhs no value.
Since this is a case of optional<T>{} == T{} comparison, such scenario should be handled separately. Here is my attempt at fixing it:
https://godbolt.org/z/Yq3nM4xn4

This is rather not a proper fix, since it will still break for cases like optional<optional<short>>{} == optional<int>{}, and the internal if-constexpr instead should probably compare the nestedness of lhs and rhs optional.

I didn't check but most likely the same problem applies to relational operators.

Edit:
std::expected seems to be affected as well:
While developer may expect following comparison to work std::expected<T, E>{} == T{} (thanks to the operator==(const expected&, const T2&) - it falls apart when T is expected<U> with unexpected value - because operator==(const expected&, const expected<T2, E2>&) overload is actually selected in that scenario.
https://godbolt.org/z/h4jYfjYco

Edit2:
I believe the proper fix for std::optional and std::expected should be to constrain following operators
operator==(const expected& lhs, const expected<T2, E2>& rhs)
operator==(const optional<T>& lhs, const optional<U>& rhs)
to be applicable only when lhs and rhs have same nestedness levels.


r/cpp • • May 11 '26

C++26 reflection-based dependency injection

71 Upvotes

I was playing with reflection and made this (messy) implementation of Guice-like dependency injection. I worked in a codebase a few years back that was fully Guice-based and it was one of the cleanest and easiest to maintain architectures I've seen. Having it in C++ now is awesome.

https://godbolt.org/z/qrzdrarvr

I'm excited to see all the other interesting things that'll come out of reflection.


r/cpp • • May 11 '26

New C++ Conference Videos Released This Month - May 2026 (Updated To Include Videos Released 2026-05-04 - 2026-05-10)

14 Upvotes

CppCon

2026-05-04 - 2026-05-10

2026-04-27 - 2026-05-03

C++Online

2026-05-04 - 2026-05-10

2026-04-27 - 2026-05-03

Audio Developer Conference

2026-05-04 - 2026-05-10

  • Continuous QA Testing for Plugins Using AI and Python - Ryan Wardell - https://youtu.be/w1hLmNPxOV4
  • Using Kotlin/Compose Multiplatform to Revive a Historic Multiplayer Online Drum Machine - How To Write An Audio App That Runs Almost Everywhere - Phil Burk - https://youtu.be/8jA6Dg5iqfw
  • Converting Source Separation Models to ONNX for Real Time Usage in DJ Software - Anmol Mishra - ADC 2025 - https://youtu.be/CNs9EgMBocI

2026-04-27 - 2026-05-03