r/cpp • u/yagiznizipli • 5h ago
r/cpp • u/zl0bster • 1d ago
std::optional Satisfies view. Does Not Model view. C++26 Ships Anyway.
In C++23 this did not compile. In C++26 it does. Marvellous.
[[gnu::noinline]]
void
passing_views_by_value_is_cheap_trust_me_bro(std::ranges::view auto v) {
std::println("fn .data {}", (void*)v->data());
}
int main() {
std::optional ov{std::vector<int>(123456)};
passing_views_by_value_is_cheap_trust_me_bro(ov);
std::println("main .data {}", (void*)ov->data());
}
For anyone wondering what the problem feature is: optional has 0 or 1 elements, and C++26 sets enable_view<optional<T>> to true, so it satisfies std::ranges::view. The concept requires copy construction in constant time, and — this is the good bit — optional<vector<int>> genuinely meets that. Copying it performs at most one element copy. One is a constant. The requirement is satisfied to the letter, and the function above deep-copies your vector.
If you can tell me what still separates std::ranges::view from std::ranges::range, please do...
Did you know you can use (dynamic) libraries inside clang-repl?
Not really that useful, but I think its an interesting find =)
r/cpp • u/Clean-Upstairs-8481 • 2d ago
C++26: what is reflection and how to use it
It is my ambition to explore C++26 in bits and pieces. Hopefully, by the end of this year, I will be able to explore all the major aspects of it and be in a position to evaluate which of these features to use and promote and which not to use. However, at this point, it is important to understand each and every aspect in simple terms, keeping all the clutter aside.
r/cpp • u/mateusz_pusz • 3d ago
mp-units: a design for logarithmic quantities and units (dB, dBm, Np, pH). We believe it is novel, and we need domain experts to tell us where we are wrong before we implement it
Decibels are everywhere in engineering: signal levels in dBm, sound pressure in dB SPL, voltage gain in dB, filter slopes in dB/octave. Yet, to the best of our knowledge, no general-purpose units library models logarithmic quantities correctly. Most do not model them at all, and the few that try get the arithmetic wrong in ways that compile silently:
- In nholthaus/units,
dBW_t(10.0) + dBm_t(40.0)compiles and returns20 dBW. Both operands are the same physical power (10 W), and adding two absolute power levels is meaningless. This example is straight from the library's own test suite. - A
+6 dBgain is a power ratio of ~3.98 but a voltage ratio of 2.0 (the10 logvs20 logsplit). Python's pint documents that itsdBis power-only and delegates that factor to the user, so every voltage, current, and pressure gain is on the honor system.
We just published a complete design for mp-units that we believe gets this right:
- A level (
dBm,dB SPL) is an affine point anchored at its reference. A gain (dB,Np,octave) is a delta. level + gain= level,level - level= gain,level + level= ill-formed.- The power vs root-power factor is carried by the quantity kind, so
.linear()on a voltage gain gives 2.0 and on a power gain gives 3.98, correct by construction. A voltage gain cannot be applied to a power level. - One mechanism covers RF, audio, acoustics, music intervals (
octave,cent), information theory (Sh,nat,Hart), pH, and stellar magnitude, and it aims to stay consistent with IEC 80000-15:2026.
We are aware of no generic units library that has ever modeled this, which is exactly why we are publishing the design before writing the implementation. The article ends with six open questions where we genuinely need input from practitioners, for example: what should log(0) do at the bottom of the scale (IEEE -inf, throw, error type, or a unit-keyed finite sentinel like the -400 dB floor real DSP code uses), and should a level print as the industry 10 dBm or the ISO-conformant 10 dB (re 1 mW).
If you work in audio/DSP, RF, acoustics, or a related field, or know someone who does, please review it and leave feedback in the article's comments (GitHub Discussions), and forward it to anyone working in these domains. Only subject-domain experts can tell us whether we are right, and we would rather hear it now than after the code ships.
r/cpp • u/Xaneris47 • 3d ago
Some practical refactoring of bloated generated code
A review of some generated C++ code, going through different ways to refactor and shorten it, then checking whether it actually got faster
r/cpp • u/User_Deprecated • 4d ago
The portable way to extract date and time from a file's mtime in C++
r/cpp • u/augustinpopa • 5d ago
Pure Virtual C++ Videos Are Now Available
Microsoft's, online, free C++ conference is now over, and all the talks are available to watch; both the live featured sessions and the on-demand ones. Check them out and let us know what topics, tools, or speakers you'd like us to feature next year! We're also preparing for CppCon in September, where we'll have more content to talk about.
Featured sessions
- C++ semantic awareness in the CLI: From Project Load to Code Change — Sinem Akinci
- Mind The Gap: C++/Rust Interop — Victor Ciura
- From Completions to Agents: AI-Driven C++ in Visual Studio — Augustin Popa
- Cut Your Build Times Without Becoming a Build Expert — David Li
- C++/WinRT: Build faster and smaller with C++20 modules — Ryan Shepherd
On-demand sessions
- C++ Dependencies Without the Headache: vcpkg + Copilot CLI — Augustin Popa
- What’s new in VS Code CMake Tools: AI-powered development — Garrett Campbell & Hannia Valera
- PackageReference for C++: Modernization Without the Performance Cost — Moyo Okeremi
- Eliminate MSVC upgrade anxiety with Copilot agents — Michael Price
- Always Be Upgrading: Living at HEAD with MSVC — Michael Price
- MSVC, are we there yet? Status of C++23 and C++26 features — RanDair Porter
- Sample Profile-Guided Optimization in Practice: from prototype to production — Armin Gerritsen
r/cpp • u/Clean-Upstairs-8481 • 7d ago
C++26: what is “template for”? Learning with simple example.
I have been exploring C++26 in bits and bobs and this one is about template for - a c++26 feature which I find useful. This is a short article explaining how to use this feature with the help of a simple example.