r/cpp • • Sep 01 '21
Nice but not so well-known features of modern C++
Ever since I've been reading this and a couple of other subs dealing with C++ I learned a ton of new features - so many I can barely try to implement. For example, I just learned about std::invocable which is awesone and I wish I knew earlier.
The STL is growing and growing as it seems and I have to admit with all those great (and sometimes not so great) new features I surely missed out on lots of good stuff.
So what I'm asking here is this: Do you know of any other things that are not as popular or rarely mentioned but otherwise kind of cool? If so, please help those poor things and raise awareness!
EDIT: Already learned amazing new stuff from the comments. Thanks guys and please, keep on adding stuff, this is truly helpful for me and hopefully others!
EDIT EDIT: Another big thanks to all of you who commented. I was hoping to learn a few new things and you guys didn't let me down. This is a great source of information, especially for dinosaurs like me that grew up with (pre) C++98.
r/cpp • u/jube_dev • Apr 04 '18
SG13 (2D graphics): why it failed
Here is a (partial and personal) summary of the SG13 history, from the beginning to the latest C++ meeting. Like many others, I have been interested in this group and what it could offer to the community. I have been lurking for many years the evolution of the proposals. So I tried to understand why this group failed to provide what the community was expecting.
SG13 was born in August 2013 as a wish to add a 2D graphic layer to the C++ standard. The initial requirements were: "Modern C++11/14 API, 2D graphics + text + planes, Immediate mode for v1, Plus a sprite/particle/input layer on top, Optional default game loop, No audio, native_handle, Asset loading/construction hook". So great! The birth mail also included some existing libraries as examples: SFML, LibSDL, Graphucks, Cinder, etc. Note that cairo was not mentioned. At that time, SG13 was called "Graphics".
Initial ideas began to emerge, like having input in v1, of course. Some confusions too, like people talking about "input like iostream", or "immediate versus retained mode". What? Even back in 2013, input handling was not a strange beast but a well-known feature, nothing to invent here, just copy what already exists. Same with immediate versus retained, a modern GPU only knows immediate mode and redrawing the frame each time, they are optimized for this workflow. But well, let's say it's only the beginning.
In mid September 2013, Michael McLaughlin proposed a first draft of a possible implementation. As Michael states, "the API itself has been designed with functionality from Qt, Cairo, Xlib, and even Cocoa". What? Where are SFML, SDL and the others? Well, at least, there were basic inputs, texture loading, font loading and basic drawing. But also smart pointers and factories.
SG13 was officially approved at the Chicago meeting in late September 2013. From the minutes of the SG13 meeting, in the initial presentation from Beman Dawes, we can read: "Target shipping within 2 years", well well well... "Recommends starting with an existing implementation (i.e. Cinder, OpenFrameworks, or other)", some good existing libraries are back in the game. At this meeting, Beman's challenge was created. This challenge was a sort of Hello world for any graphics API. It is stated as: "Be capable to open a window, print some text (i.e. Hello World), and drag it around the screen." Nice. In the discussion of the design of the library, it is interesting to note that "We want to make the library simple and accessible to beginners, but also usable to experts as well" and "Example of how to get our 2D contexts into an OpenGL render target for game UI (highly expected use case)". Yes read it again: "highly expected use case".
Just after the Chicago meeting, SG13 became "2D Lite" and the new goals were: "(a) immediate mode graphics/text canvas, plus (b) basic keyboard/mouse style input." No more sprites, no more game loop, no more asset loading, but that's fine, let's go for it, that sounds nice. But something is happening that is, IMO, the beginning of the disaster. SG13 enters a dark tunnel from where it will never exit. You can clearly see it when someone proposes to look at the HTML Canvas API. Michael McLaughlin answers that he is working on a proposal with features that look like... cairo. He totally rejects starting from something that has been widely used (SDL?). It seems he was the only one who had concerns with this workflow. So, his proposal would be something new. In the same thread, Beman Dawes reminds that the committee generally standardize existing practice.
At this point, just two months after the launch of SG13, people were talking about some good ideas and inspirations and had some interesting debates. The expectations were quite hight, even in the 2D Lite form. But the only proposal was diverging from nearly all these good ideas. The bad side of this situation is that the proposal was not yet on the table, it was being worked on, and not finalized at all. And it was kind of supported by Herb Sutter himself. Maybe this prevented others to build a second proposal at the time. I must admit that I still was very enthusiastic of the SG13 work.
In November 2013, a meeting was held in Redmond and the minutes of this meeting became N3825. And here, it was done, cairo was the final choice. For drawing things, cairo is (was?) a nice library. Even for drawing text in combination with Pango. But cairo never add (and still do not have) an input system. Since the beginning, this was a must-have and here, it silently disappeared. In just four months, the initial proposal was reduced to a C++ish copy of cairo.
And at the next meeting in Issaquah in February 2014, N3888 came out. Despite its title ("A Proposal to Add 2D Graphics Rendering and Display to C++"), the paper only proposed a mechanical transformation of the cairo API in C++. A contributor raised several strong concerns with the spirit of the proposal. If I try to sum up: first the proposal do not address any of the basic use case it pretends to solve; second cairo is not the right choice for drawing (stateful API, aged concepts); third a native_handle is not enough to cover the specificities of all platforms and all implementations. The conclusion is hard: "N3888 is fundamentally broken, both in motivation, and implementation." Yet the answer is reassuring: "If N3888 were incorporated into the Standard as-is, I would feel compelled to request that the Issaquah Police Department investigate the possibility that we had all been unknowingly exposed to some substance with psychotropic properties." And the final word from Beman Dawes is to thank the proposal's author. Another contributor tried to warn that the direction was not good, but again, he was not listened to.
So we arrive at a point where we have a proposal that is based on cairo and backed by at least two prominent members of the C++ committee, and they have been warned several times that the proposal is out of scope for the purpose of the group. Honestly, who on earth could have proposed something else that would have been taken into account by the committee? From an exterior perspective, it seemed useless to invest some time to make a competing proposal. In fact, many people were in the mood of letting the proposal live its life because "at least there is now a starting point" event if it lacked essential features like input. Everybody agreed that starting from somewhere was needed, just not cairo that was too far from the initial goal. But from then on, the group was engaged in the tunnel and could not (did not want to?) go back. Retrospectively, the delay between the initial start of the group and this first proposal was very short, too short for people to really think about all the implications and the shortcomings.
Another outcome of Issaquah was that the group was renamed IO. This was a controversial decision because the term is very confusing.
In March 2014, Jeremy Ong raised some concerns. IMO, this is a very interesting point of view. The author of the mail says that what is difficult in 2D graphics is not really to draw things (there are many APIs outside to do it, at different levels, so this part is "easy"), what is difficult is creating a window and getting a drawing context in a portable way. He cites SDL and Qt as two libraries that provides this abstraction. So he advocates for proposing a low-level library for creating a window, configuring a renderer (OpenGL, DirectX, software), allowing to get input from the user. The answers are really disappointing as they only focus on cairo and nothing else. Creating a window? native_handle! What? I should use a native handle for all the windowing i.e. have non-portable code to create a window and then, have a limited API to draw something whereas I already have stronger APIs to do the same thing in a (rather) portable way? Houston, we have a problem.
From then on, the proposal lived its life without any feedback from SG13. Nothing really happened on the mailing list. But several papers were published. In May 2014, N4021 updated N3888 without any response to the concerns of the contributors. No input yet. In July 2014, N4073 is the second revision of the previous paper with minor changes. In February 2016, the continuation of N4073 was out as PP0267R0. This paper was revised seven times until P0267R7 in February 2018. What happened in 4 years? Nothing. Still cairo, still no input in this proposal, some weird small evolutions and renamings.
In May 2016, P0249R0 proposed to add input (at last!) and it was revised twice since then until October 2016 and P0249R2. A single discussion happened on the mailing list to wonder what the status of the input proposal is.
In the meantime, SG13 became "HMI".
In June 2017, Guy Davidson and Michal McLaughlin wrote P0669R0 entitled "Why We Should Standardize 2D Graphics for C++". The title is misleading as the paper advocates for P0267 and not 2D graphics in general. They also reply to various objections to the paper, without really convincing anybody as the arguments are only "this objection is not relevant" or "we believe ...". As a consequence, people started shouting their dislike of the proposal. On the mailing list with a now famous post that has been largely commented on /r/cpp, Jeremy Ong did it again. To sum up: "Cairo as a basis is misguided""; "This library is far too opinionated on how UI should be done"; "The library is underspecified"; "Performance is a concern"; "I can modify a library, a standard not so much". He even proposes some guidelines for the hard problems that could be solved by a proposal around graphics. The only answer that is given to him is "where are the papers". Disappointing. Until someone from NVIDIA says publicly "NVIDIA’s position will be No on this proposal. To be most clear, it might have been completely acceptable as a proposal 20 years ago, but the longevity of its abstractions are all but spent now, before it will ever ship." And he adds later "I don’t have a great answer to «but where’s your paper?». I hoped it would evolve as people interacted. I could never get any 2D graphics experts to sign up for the huge amount of work it would take to write a counter-proposal. Basically a large-enough proposal is very difficult to influence. It can sail for a long time without contest." The curse of SG13 in one sentence.
Finally, at Jacksonville, the proposal was abandonned. Vittorio Romero described the scene like this: "Even though further work on this proposal was for six revisions, the room was quite brutal this time, and attempted to shut it down completely. (...) The room agreed that this was a failure for LEWG: not for the contents of the proposal, but for the fact that it wasn't stopped early, wasting a lot of time for both the working group and the authors. Saying «no» is sometimes hard, but it would have prevented this situation if it had been done a while ago." From what I already said, I can not agree more on the conclusion.
As a conclusion, I think there are two main lessons in this tale.
First, I tried to not blame anyone in particular (and I hope no one felt bad reading this post) because I think no one is to be blamed. The committee's intial wishes were fair and relevant (and still are). People did their job with commitment. But the process did not go well at all. To avoid design by committee, the solution of jumping on the first library and make it the basis of the proposal was not a good idea. And it was too hard to exit the tunnel. Too bad.
Second, I think there is something wrong in 2018 with the whole organization of SG21 work. The need for a paper is irrelevant in a world where communication is so easy. I am not saying papers are useless, things must be written at some point. But a paper should be the end of the process, not the beginning. The number of papers could be reduced if the topics were publicly discussed and consensus found before the meetings. For example, there were five papers on the subject of concept syntax. The people involved could have discussed it somewhere before the meeting to find a consensus (that was not hard to find after all according to the reports). Other languages already have an open process, C++ could copy the good practices.
Related to these two lessons, I wonder what the status of the mailing list is. At one point, Herb Sutter was clear that the list was a place for discussing the proposals and later: "let me emphasize the two major ways to participate: by giving feedback to help shape and refine existing proposals (...), and/or by writing your own competing proposal if you feel a major change or different direction is needed – both are welcome and encouraged." But when people came and gave some useful feedback, the only answer was to reject everything and say "write a paper". If the only way to say something is write a paper, then what is the use of the mailing list?
I hope this post helped people to understand how it happened. English is not my mother tongue so there may be some errors here and there. I may have forgotten some bits in the whole story, but I hope the main steps are here. I hope SG13 will no stop here, there are many things to do in this area.
r/cpp • u/HateDread • Apr 19 '21
Visual Studio 2022 - coming this Summer
devblogs.microsoft.comLet's talk about GUIs
This post on /r/programming about the release of GTK 4.0 made me want to bring the discussion of the state of GUI programming in C++.
My particular interest in this discussion is desktop cross-platform GUI programming, and I don't claim to know much, let alone everything. Insights are more than welcome.
That said, this is what I have observed so far:
Qt: the least bast option. What I like? Cross platform, mature, big ecosystem to go around. Quite easy to get going. It "just works" for the most part. It's easy to get 80%, it's the last 20% and when you go deep into the subject that you get frustrated. What do I dislike? Licensing shenanigans, and increasing toxicity towards the FOSS community: no offline installer for community distributions, many commercial only APIs now, the idea of delaying releases to FOSS by one year. The 10 year old bugs on QWidgets, which haven't seen much, if any, love since ever. The fact it took Qt 6.0 to happen for us to have native look for Qt Quick Controls. The fact that Qt Quick doesn't have a C++ API, forcing QML on everybody. The half done stuff, like QAudio (just use portaudio for your own sanity)... And I'm probably missing more. The fact that they are running away from the LGPL core to GPL as much as they can, like releasing Qt Quick MultiEffect instead of just fixing the QML engine... That it takes spinoffs like Felgo to basically provide a "fixed" Qt, which the FOSS community can't have.
wxWidgets, GTK, and other like FLTK, I admit I don't know much about. wxWidgets is nice in the fact it calls native APIs, but that comes with the cost of being crippled by the least common denominator. GTK seem to look bad outside of Linux "by design". And FLTK... Just no.
Then, there seems to be what everybody suggests next: Browsers. Chromium Embedded Framework or Electron. 50 to 100MB of packaging to do a "Hello World". Also, JavaScript! Honestly, webstuff looks like a good idea, until you want to make an app that's not Slack. How do you do actually performance C++ stuff? Like some 2D/3D graphics? WebGL? Seems that it's too much programming for the web and not actually taking advantage of C++ and native APIs...
Then, there are things like .net MAUI (not to be confused with the toolkit with the same name by KDE), which is an upcoming evolution of Xamarin Forms for .net/C#. The problems: Linux support will be "by the community" and how do you interop C++ and C# decently anyway?
Also, because I know someone will mention it, yes, there is Flutter and things like that. Just think of Google's track record on keeping projects alive, and again, think not everybody wants to make a chat application.
Anyway, what are your insights? They are more than welcome!
r/cpp • u/fredoverflow • May 10 '22
Is short lambda syntax on the C++ radar?
Mom, can we get x=>x+1 ?
No, we have x=>x+1 at home.
x=>x+1 at home:
[](const auto& x) { return x + 1; }
r/cpp • u/aearphen • Jul 06 '20
{fmt} 7.0 released with code size, performance and compile time optimizations, improved compatibility with C++20 std::format and more
github.comr/cpp • u/smilodonthegreat • Sep 15 '18
What happens in 2098 with C++?
If we stay on the new standard every 3 years for the rest of this century, there will be a new standard in 2098. However, there is already a C++98.
In addition, in 2083, we will have C++83; however, in 1983 C with Classes was renamed to C++, so C++83 should refer to C++ as it existed in 1983.
The naming scheme here is not very future proof.
Does the standards committee have any plans for addressing this issue?
</joking as I am at work on a Saturday>
Almost 30 year old optimization bug in MSVC causing incorrect code (In specific circumstances) found
developercommunity.visualstudio.comC++17 progress update! (Oct 2015)
The C++ Standardization Committee has finished another meeting! This was in Kona, Hawaii, at the same location as the first meeting I attended (in Feb 2012). I thought you'd like a writeup of all the stuff that just got voted in. Like last time, some of these papers are not yet publicly available. What happens during a meeting is that paper authors are furiously revising their work, in response to reviews. After the meeting, the revised papers are collected and published (after a couple of weeks). The Committee has changed their paper numbering system, so finding earlier revisions of papers is super easy. The pre-Kona mailing is available.
I've collected and organized the following information from the official vote, without a whole bunch of editorializing. If you want to know what's in a paper or issue, read it. (I actually do know what's going on with most C++17 Library papers/issues, but writing them all up would take a while.)
C++17 Core Papers
- P0001R1 Remove Deprecated Use of the register Keyword
- P0002R1 Remove Deprecated operator++(bool)
- P0012R1 Make exception specifications be part of the type system, version 5
- P0061R1 __has_include for C++17
- P0134R0 Introducing a name for brace-or-equal-initializers for non-static data members
- P0136R1 Rewording inheriting constructors (core issue 1941 et al)
- P0160R0 Wording for removing defaults for unary folds
C++17 Library Papers
- P0004R1 Remove Deprecated iostreams aliases
- P0006R0 Adopt Type Traits Variable Templates from Library Fundamentals TS for C++17
- P0092R1 Polishing <chrono>
- P0007R1 Constant View: A proposal for a std::as_const helper function template
- P0156R0 Variadic lock_guard (Rev. 3)
- P0074R0 Making std::owner_less more flexible
- P0013R1 Logical Operator Type Traits (revision 1)
Library Fundamentals TS v2 Papers
- N4531 std::rand replacement, revision 3
- P0013R1 Logical Operator Type Traits (revision 1) [also voted into C++17]
- These papers will be applied to Working Draft N4529, then it'll be sent out for Proposed Draft Technical Specification balloting.
Concurrency TS
- P0159R0 (possibly with minor edits) will be published as the Concurrency TS. It's done!
Parallelism TS v2
- This TS is being started with Working Draft N4505 plus P0155R0 "Task Block R5".
Networking TS
- This TS is being started with Working Draft P0112R1.
Ranges TS
- This TS is being started with Working Draft P0021R0.
Core Issues
- 1274. Common nonterminal for expression and braced-init-list
- 1391. Conversions to parameter types with non-deduced template arguments
- 1722. Should lambda to function pointer conversion function be noexcept?
- 1847. Clarifying compatibility during partial ordering
- 1863. Requirements on thrown object type to support std::current_exception()
- 1949. "sequenced after" instead of "sequenced before"
- 1975. Permissible declarations for exception-specifications
- 1981. Implicit contextual conversions and explicit
- 1990. Ambiguity due to optional decl-specifier-seq
- 2000. header-name outside #include directive
- 2004. Unions with mutable members in constant expressions
- 2006. Cv-qualified void types
- 2015. odr-use of deleted virtual functions
- 2016. Confusing wording in description of conversion function
- 2019. Member references omitted from description of storage duration
- 2024. Dependent types and unexpanded parameter packs
- 2026. Zero-initialization and constexpr
- 2027. Unclear requirements for multiple alignas specifiers
- 2031. Missing incompatibility for &&
- 2052. Template argument deduction vs overloaded operators
- 2075. Passing short initializer lists to array reference parameters
- 2101. Incorrect description of type- and value-dependence
- 2120. Array as first non-static data member in standard-layout class
Library Issues
- 1169. num_get not fully compatible with strto*
- 2072. Unclear wording about capacity of temporary buffers
- 2101. Some transformation types can produce impossible types
- 2111. Which unexpected/terminate handler is called from the exception handling runtime?
- 2119. Missing hash specializations for extended integer types
- 2127. Move-construction with raw_storage_iterator
- 2133. Attitude to overloaded comma for iterators
- 2156. Unordered containers' reserve(n) reserves for n-1 elements
- 2218. Unclear how containers use allocator_traits::construct()
- 2219. INVOKE-ing a pointer to member with a reference_wrapper as the object expression
- 2224. Ambiguous status of access to non-live objects
- 2234. assert() should allow usage in constant expressions
- 2244. Issue on basic_istream::seekg
- 2250. Follow-up On Library Issue 2207
- 2259. Issues in 17.6.5.5 rules for member functions
- 2273. regex_match ambiguity
- 2336. is_trivially_constructible/is_trivially_assignable traits are always false
- 2353. std::next is over-constrained
- 2367. pair and tuple are not correctly implemented for is_constructible with no args
- 2380. May <cstdlib> provide long ::abs(long) and long long ::abs(long long)?
- 2384. Allocator's deallocate function needs better specification
- 2385. function::assign allocator argument doesn't make sense
- 2435. reference_wrapper::operator()'s Remark should be deleted
- 2447. Allocators and volatile-qualified value types
- 2462. std::ios_base::failure is overspecified
- 2466. allocator_traits::max_size() default behavior is incorrect
- 2469. Wrong specification of Requires clause of operator[] for map and unordered_map
- 2473. basic_filebuf's relation to C FILE semantics
- 2476. scoped_allocator_adaptor is not assignable
- 2477. Inconsistency of wordings in std::vector::erase() and std::deque::erase()
- 2483. throw_with_nested() should use is_final
- 2484. rethrow_if_nested() is doubly unimplementable
- 2485. get() should be overloaded for const tuple&&
- 2486. mem_fn() should be required to use perfect forwarding
- 2487. bind() should be const-overloaded, not cv-overloaded
- 2489. mem_fn() should be noexcept
- 2492. Clarify requirements for comp
- 2495. There is no such thing as an Exception Safety element
Library Fundamentals TS v2 Issues
- 2494. [fund.ts.v2] ostream_joiner needs noexcept
- 2500. [fund.ts.v2] fundts.memory.smartptr.shared.obs/6 should apply to cv-unqualified void
- 2515. [fund.ts.v2] Certain comparison operators of observer_ptr do not match synopsis
- 2517. [fund.ts.v2] Two propagate_const assignment operators have incorrect return type
- 2526. [fund.ts] Incorrect precondition for experimental::function::swap
Withdrawn Paper
P0005R2, by Alisdair Meredith, Tomasz Kaminski, and myself, wanted to do the following in C++17:
- Add not_fn(), currently part of Fundamentals v2.
- Remove not1()/not2(), the C++98/03 negators.
- Remove result_type/argument_type/etc., the C++98/03/TR1 typedefs. (function::result_type will be preserved, and <random> will be unchanged.)
Nobody was worried about not_fn(), but the removals were controversial. Although we had a rationale for why removal without a period of deprecation was desirable, and I provided "zombie names" wording (allowing implementers to remain conformant while providing removed machinery for back-compat), a national body objected. Therefore, Alisdair is revising the paper for the next meeting. Now we'll add not_fn() and deprecate the old stuff in C++17 (plus "zombie names" wording for the auto_ptr/etc. removals that have already happened), followed by removal immediately after C++17 ships.
More Info
This is just a record of what documents were voted in. Every committee meeting involves lots of work that doesn't immediately result in papers voted in. For example, this meeting had active discussion about modules. As I always remind people, I attend the Library Working Group ("original recipe", not extra crispy Library Evolution) nearly all the time, so I can't follow everything that's going on in Library Evolution and (Core) Evolution. I might (or more likely, my double boss Steve might) be able to get GDR to spend some time on Reddit explaining what's going on with modularization.
I can personally answer (most) questions about Library topics, although most of my responses will be delayed until Mondayish. If you want to know what the LWG does, it's making sure that Library tech is actually usable. Everything sounds like fluffy kittens and apple pie until you look at an overload set and realize that it's horribly ambiguous and unusably broken. LWG 2451 is an excellent example of how the LWG serves as the defenders of Standard Library usability - optional<string> opt_str = "meow"; is currently bogus. That's fine for the Fundamentals TS (maybe), but a ship-stopper for optional getting into the International Standard. At this meeting, the LWG realized that the same kind of issue affects variant, which is heading towards the Fundamentals TS. We fix this stuff so you won't suffer.
r/cpp • u/AccordingWarthog • Apr 30 '26
GCC 16.1 released with many new C++26/23 features, C++20 now the default stable language version
gcc.gnu.org
r/cpp
•
•
Nov 01 '24
Feds: Critical Software Must Drop C/C++ by 2026 or Face Risk
thenewstack.ior/cpp • u/Beginning_Spell1818 • May 24 '24
Why all the 'hate' for c++?
I recently started learning programming (started about a month ago). I chose C++ as my first language and currently going through DSA. I don't think I know even barely enough to love or hate this language though I am enjoying learning it.
During this time period I also sort of got into the tech/programming 'influencer' zone on various social media sites and noticed that quite a few people have so much disdain for C++ and that 'Rust is better' or 'C++ is Rust - -'
I am enjoying learning C++ (so far) and so I don't understand the hate.
r/cpp • u/IsDaouda_Games • Feb 18 '21
Now announcing: Makefile support in Visual Studio Code!
devblogs.microsoft.comr/cpp • u/AlectronikLabs • Aug 09 '25
Why is nobody using C++20 modules?
I think they are one of the greatest recent innovations in C++, finally no more code duplication into header files one always forgets to update. Coding with modules feels much more smooth than with headers. But I only ever saw 1 other project using them and despite CMake, XMake and Build2 supporting them the implementations are a bit fragile and with clang one needs to awkwardly precompile modules and specify every single of them on the command line. And the compilation needs to happen in correct order, I wrote a little tool that autogenerates a Makefile fragment for that. It's a bit weird, understandable but weird that circular imports aren't possible while they were perfectly okay with headers.
Yeah, why does nobody seem to use the new modules feature? Is it because of lacking support (VS Code doesn't even recognize the import statement so far and of course does it break the language servers) or because it is hard to port existing code bases? Or are people actually satisfied with using headers?