15 ms·
New C++ features in GCC 12
- jgaa 5y ago> "Sorry, you need to enable JavaScript to visit this website." I was interested in C++, not javascript.
- jdrek1 5y agoWorks perfectly fine for me on FF with uBO and all scripts blocked. Apparently that "Sorry, ..." comes from a cookie popup which I don't even see so seems like you need stronger blocking in your browser.
- kevin_thibedeau 5y agoI have all scripts blocked with NoScript and saw it. I enabled redhat.com and the site rendered but took obscene liberties with polluting the back history after scrolling down.
- photochemsyn 5y agoThere is a tag ###seckit-noscript-tag which shows up with uBlock Origin under 'Cosmetic Filters' which lets you view it without enabling JS (disabling NoScript on Firefox).
- qalmakka 5y agoIt's nice to see how GCC developers have worked hard in recent years to keep their compiler up to the most current standard. It's sad the same can't be said about Clang which, despite the fact LLVM has basically EVERYONE behind, still struggles to implement the entirety of C++20 after more than 2 years. And this is without taking libcxx into account, which has been also struggling despite the fact is way newer and cleaner than libstdc++. It's basically as if Google and Apple don't really give a damn about keeping up with C++ standards, so they always get pushed on the back burner.
- pjmlp 5y agoThanks license, that is why. Most of those people behind, care about the LLVM infrastructure and not so much about upstreaming clang stuff or improving ISO C++ compliance.
- nikic 5y agoI don't see how the license is relevant here. It's not like companies have already implemented full C++20 support in their local forks and just haven't bothered upstreaming it. But yes, a lot of LLVM users don't care about C++ standard compliance at all, for example because they don't use C++.
- pjmlp 5y agoNot C++20 per se, but certainly language features.
- nikic 5y agoFor reference, here are the C++ support pages for GCC and Clang: https://gcc.gnu.org/projects/cxx-status.html https://gcc.gnu.org/projects/cxx-status.html https://clang.llvm.org/cxx_status.html https://clang.llvm.org/cxx_status.html
- zarzavat 5y agoC++20 is coming into a world with much more competition than C++11 did (the last comparably sized standard). When C++11 came out Rust, Go, Zig, Swift were all either in their infancy or didn’t exist yet. There was a huge desire for C++ to improve and improve fast given its centrality. That world is not today’s world. Projects that would have been written in C++ in the past are often written in a different language now. This leaves C++ being useful mostly for legacy codebases, of which adopting new standards quickly is not so important.
- pjmlp 5y agoNone of those languages have the ecosystems that make C++ still relevant today.
- Ne02ptzero 5y agoSide topic: Didn't we have a is_const_eval equivalent in C? I seem to remember a dark magic macro doing precisely that on the lkml somewhere, but can't find it.
- einpoklum 5y agoThere is no compilation-time evaluation of functions and statements in C. You have constants, which can be expressions, but that's about it. Oh, and macros of course. compile-time evaluation was introduced in C++11 and expanded in subsequent versions of the language standard.
- qalmakka 5y agoAlso, `const` values in C are not really evaluated at compile time, because they have by default external linkage so they are not really "constant", they are more like "readonly" or 'let' in Rust. They are always loaded from memory, and that's why #define is still king in C. Doing const int X = 33; // ... void something() { int arr[X] = ... } works in both C and C++, but with a catch: in C++, X is evaluated at compile time (it has internal linkage), while in C this works only after C99 because it's actually a VLA.
- uecker 5y agoAlthough many compilers allow const int X = 33; enum { Y = X };
- qalmakka 5y agoThat's a GNU extension that everyone relies so much on it's basically akin to a standard. You get a warning from Clang (or an error from GCC) if you ask for a more "standard" interpretation of the source: $ clang -o cs cs.c -Wall -std=c11 -pedantic cs.c:6:6: warning: expression is not an integer constant expression; folding it to a constant is a GNU extension [-Wgnu-folding-constant] A = X, $ gcc -o cs cs.c -Wall -std=c11 cs.c:6:9: error: enumerator value for ‘A’ is not an integer constant 6 | A = X, | ^ In general, that's illegal ISO C and should always be rejected, but as you see that's not usually the case.
- BruceEel 5y agostruct A { int a; int b; A() : b(1), a(b) { } }; . Here, the b field is used uninitialized because the order of member initializers in the member initializer list is irrelevant; what matters is the order of declarations in the class definition. (A related warning, -Wreorder, can be used to warn when the order of member initializers does not match the declaration order.) I don't get it. Why b? Do they mean a?
- dundarious 5y agoThe order of initialization is the order of definition, i.e., a then b (because the definition order is `int a; int b;`). So initialization happens like so: // implicitly, a = undefined; // implicitly, b = undefined; a = b; b = 1; So b is used when its value is undefined. The undefined value is assigned to a.
- BruceEel 5y agoWhat confused me in their wording is that they seem to imply that the problem is: b would be left uninitialized, rather than: a depends on uninitialized b. Your explanation, OTOH, is straightforward to read.
- dundarious 5y agoUnderstandable. The original is correct that b is used uninitialized, but they don't clarify that it is a that is left uninitialized.
- tialaramex 5y agoMmm? The consequence of reading the uninitialized variable is that your program's behaviour is Undefined. There's no point worrying about this other variable named 'a', it's already game over. It's not called "Temporarily unusual" behaviour or "Reliably crashing" behaviour or even "I bet it'll probably just skip that and do the next thing" behaviour, it's Undefined for a reason.
- boris 5y agoIt you are looking for news on C++20 modules support, then, sadly, there is not much progress since GCC 11. I've re-tested my modules examples[1] a few days ago with the GCC 12 snapshot, and while a few bugs have been fixed, the majority remain unfixed[2]. [1] https://github.com/build2/cxx20-modules-examples https://github.com/build2/cxx20-modules-examples [2] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103524 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103524
- suby 5y agoThanks for posting this information. Module support is the one feature I am really looking forward to.
- Kelteseth 5y agoThere is an active MR [1] to add modules support to CMake 3.24 (based on this comment [2]) [1] https://gitlab.kitware.com/cmake/cmake/-/merge_requests/7210 https://gitlab.kitware.com/cmake/cmake/-/merge_requests/7210 [2] https://gitlab.kitware.com/cmake/cmake/-/merge_requests/7210/diffs#5c353ab38e68944f55348abea692a55a27bc36a3_55_56 https://gitlab.kitware.com/cmake/cmake/-/merge_requests/7210...
- WalterBright 5y agoModules were added to C with 10 lines of code: https://nwcpp.org/April-2022.html https://nwcpp.org/April-2022.html https://dlang.org/spec/importc.html#__import https://dlang.org/spec/importc.html#__import The same technique could be used with C++.
- WalterBright 5y agoThe method is based on the idea that the product of the semantic pass of a compiler is a symbol table. Then, to make imports work: 1. add some syntax to import a module 2. create another instance of the compiler to compile that module and return the symbol table 3. have the importer, when it doesn't find a symbol in the symbol table, to search the symbol tables of the imports
- kevin_thibedeau 5y ago> Also note that `if consteval` requires { } Changing the fundamental grammar is a step too far. Compilers can warn or reject bare statements in control structures if you want.
- mhh__ 5y agoD does if(__ctfe), not sure why C++ couldn't have just done that rather than adding yet moar syntax.
- pooya72 5y agoI'm looking forward to the full implmentation of C++20. I'm just a junior programmer, but I've just started playing around with modern C++ and it feels all more safe than I was initially planning. I was expecting more undefinded behaviour and memory leaks. Working in C++, at least for small scripts, didn't feel much more burdensome than Python.
- the__alchemist 5y agoTangent: Is there a reason why C and C++ compilers are dev-PC-OS dependent for ARM targets? For example, if using Rust to compile for a Cortex-M etc, the workflow is the same for Windows and linux: Install the `thumbv7` target using a CLI command, then `cargo build`. I'm trying to build an OSS C lib now for the same platform, and the Windows instructions involve WSL or Cgywin.
- rfoo 5y agoNo. The most likely reason in your case is the build system of the library you want to build uses some mix of POSIX shell script and GNU Make, which is ... hard to get right on Windows. It's not C/C++ specific, through installing a cross C compiler on Windows might involve more work than rustup add target (or not, if someone did the work and packaged the toolchain as .exe installer).
- melissalobos 5y agoRust only uses LLVM currently. WSL or Cygwin is because the compiler situation on Windows is a bit rough(VS doesn't support C fully), so most people use GCC there.
- xvilka 5y ago> compiler situation on Windows is a bit rough(VS doesn't support C fully) clang-cl[1] does. [1] https://clang.llvm.org/docs/MSVCCompatibility.html https://clang.llvm.org/docs/MSVCCompatibility.html
- pjmlp 5y agoVS supports C11 and C17, to the extent required by the standard, minus optional annexes. The only thing missing are some atomic library calls.
- uecker 5y agoYes, almost. But it misses optional features and common extensions.
- rnikander 5y agoThis guy looks like he's implementing an entire new C++ compiler, but he seems unconcerned with making it open source or getting it to work on other platforms. I'm not sure what he's up to. https://twitter.com/seanbax https://twitter.com/seanbax
- WYepQ4dNnG 5y agoI worked on few projects in c++ many many years ago, so I don’t define myself as an expert in that language, but c++ nowadays scares me, looks like the language complexity have exploded and code looks like complex to me. All those templates … I have the feeling that languages like Rust have a chance long term to replace c++
- pjmlp 5y agoUntil they are 40 years old, with similar growth pain points. I look at Java 18, C# 11/.NET 7, Python 3.10, and the knowledge about latest features, language and ecosystem changes across all major libraries for the last 25 years is hardly less complex.
- nicoburns 5y agoI completely disagree. Java, C# and Python are all dramatically less complex than C++. And there is an additional issue: lack of knowledge about something in one of those languages might lead to a suboptimal implementation, but it is unlikely to lead to anything akin to undefined behaviour. As such, there is a sense in which you need to know everything about C++ in order to use it safely that isn't true of almost any other language.
- FpUser 5y ago>"As such, there is a sense in which you need to know everything about C++ in order to use it safely" Completely disagree.
- nicoburns 5y agoWell you at least need to know a whole plethora of things that can trigger undefined behaviour, many of which are triggered by code patterns that are idiomatic in others contexts. This is very unlike language like most other languages where you can play around with little language specific knowledge and the worst thing that's likely to happen is a runtime exception.
- 5y ago
- rnikander 5y agoSwift gets weirder and more difficult, C++ gets better. Will the two lines intersect? Probably not, but I'm starting to wonder. I wish C++ had a few more cosmetic and convenient things that I've gotten used to in Swift. 1. Properties with get/set/didSet blocks. 2. Non-nullable types, ie `T` and `T?`. There's probably no way to add those, even as a GCC-specific non-standard extension.
- pjmlp 5y agoWell, in spite its warts, I know which ecosystem I would rather invest on.
- gpderetta 5y agoWhat do you mean non nullable types? In C++ only pointers can be nullable.
- darknavi 5y agoAlso with `std::optional` "nullable" pod values are a LOT nicer.
- rnikander 5y agoYears ago I worked on some C++ and the code had a lot of pointers to objects. Maybe it was not well written C++. In Swift (and Kotlin and others) the compiler can prove that you never get a null pointer error at runtime. If you want something that can be null, the type is `T?` (short for `Optional<T>`) instead of `T`. Then there are various syntactically convenient ways of unwrapping it and dealing with the null case. Eg `foo ?? bar` means give me `foo` if it's not null, otherwise `bar`. Or `foo?.x` means: get the property x of foo, if foo is not null. I haven't used C++ much in recent years. If there's an equivalent, I'd love to know about it.
- jcelerier 5y agoC++ objects are values. E.g. in std::string str; str is never null, and always a valid object, because it's a value, not a pointer like in most GC'ed or dynamic languages
- arcticbull 5y ago`std::is_constant_evaluated` seems bananas to me - looks like a great way to end up with almost untraceable bugs, where the same function may or may not have bugs depending on whether its evaluated at runtime or at compile time. Are there good ways to run unit tests against both const and non-const evaluations?! But then again, most new C++ features seem bananas to me, so I wouldn't read into it.
- jcelerier 5y agoHow would you implement something like strlen so that you have the strict, hard guarantee that strlen("foo") is evaluated at compile-time?
- arcticbull 5y agoI'm saying a conditional branch that offers a different path of execution for a constant evaluation than for a runtime evaluation gives you a function that behaves differently in different contexts. In Rust, for instance, constant functions have to evaluate the same in both compile time and run time - you can't have a different code path depending on which. If you want that, make two functions. Trivially: constexpr bool is_even(int x) { if (std::is_constant_evaluated()) { return true; } else { return ((x % 2) == 0); } } How would you unit test this in a way that catches that only the constant evaluation is broken?
- StephanTLavavej 5y agoHere’s what we do in MSVC’s STL: constexpr bool test_is_even() { assert(is_even(0)); assert(!is_even(1)); // … more test coverage return true; } int main() { test_is_even(); // runtime coverage static_assert(test_is_even()); // compile-time coverage This lets us run all test cases at both runtime and compiletime. (The static_assert performs constant evaluation, and if an assert within test_is_even would fail, it won’t be a constant expression.)
- 5y ago
- gautamcgoel 5y agoEvery time I read an article about C++, the language seems to have gotten more complex and arcane than it was the last time I read about it.
- MauranKilom 5y agoArguably, the only way to add zero complexity is to never add a feature. It's unclear what the point of that would be, given that it is trivial to stay on an older C++ version if this is your goal.
- rich_sasha 5y agoThere are very few mainstream languages without GC. Really, I’m thinking C, C++ and Rust, plus no doubt other interesting but less popular ones. C++ is immensely complex. The amount you need to grok is very significant. I wonder if that is somehow a necessary feature of a performant, GC-free, optionally low level language. Could you, for example, create something with the simplicity of Go, (most of) the speed of C/C++, and no GC? What would it look like? I’ve never written a line of Rust so no idea there.
- jimbob45 5y agohttps://nim-lang.org/ https://nim-lang.org/ The C++ language designers have made a terrific language with the ergonomics of a four-fingered glove. Nim fits your criteria but doesn’t have the popularity of C++.
- rich_sasha 5y agoI like the look of Nim, but it is GC based so not quite what I’m musing about here.
- jimbob45 5y agoI’m sorry, I thought Nim didn’t have a GC. Now that I’m doing a bit of research, I’m wondering what ever gave me that thought.
- cb321 5y agoNim has a variety of automated memory management strategies (more or less optional depending upon how much the stdlib and other libs matter to you). It has even recently renamed its --gc compile option to --mm to try to reduce confusion.
- zozbot234 5y agoC++ and Rust are not so much "without GC" as "with optional, limited GC". Reference counting is a kind of GC, and is available out of the box in both C++ and Rust.