8 ms·
C++26: more constexpr in the core language
- psyclobe 1y agoThat’s super cool; c++ is always the sharpest tool in the drawer (and by virtue the funnest!).. It’s too bad you still can’t cast a char to a uint8_t though in a constexpr expression.
- vinkelhake 1y ago> It’s too bad you still can’t cast a char to a uint8_t though in a constexpr expression. Uh, what? That has worked fine since the introduction of constexpr in C++11.
- TuxSH 1y agoMaybe they meant reinterpret_cast from/to char to u8, which in this case isn't possible in constexpr.
- epcoa 1y agoWhy would you need to do that though if you can static_cast?
- TuxSH 1y agoYou can't static_cast in this case; https://godbolt.org/z/a1bMbPcaj https://godbolt.org/z/a1bMbPcaj
- lbhdc 1y agoYou can use `std::bit_cast` to do that in constexpr contexts. constexpr auto f(uint8_t *x) { return std::bit_cast<char *>(x); } https://godbolt.org/z/K3f9b9GGs https://godbolt.org/z/K3f9b9GGs
- psyclobe 1y agoAh this was my case! Was trying to constexpr a uint8_t ptr to char * in a constexpr constructor for a string class. Ah that’s what bitcast is for, neat!
- TuxSH 1y agoNo, you can't do that either: https://godbolt.org/z/vzdTMazx7 https://godbolt.org/z/vzdTMazx7 : error: '__builtin_bit_cast' is not a constant expression because 'char' is a pointer type Here the `constexpr` keyword means the function might be called in a constant-evaluated context. f doesn't need to have all its statements be able to be evaluated in constexpr, only those which are actually used are. You need to explicitly instantiate a constexpr variable to test this. cppreference is very clear* about this, regarding bit_cast: https://en.cppreference.com/w/cpp/numeric/bit_cast https://en.cppreference.com/w/cpp/numeric/bit_cast
- lbhdc 1y agoGood catch. Its weird that it compiles without error as a consteval func.
- TuxSH 1y agoHmm, looking at cppreference: The consteval specifier declares a function or function template to be an immediate function, that is, every potentially-evaluated call to the function must (directly or indirectly) produce a compile time constant expression. It's possible that the compiler just doesn't bother as long as you aren't actually calling the function.
- lbhdc 1y agoThis compiles in clang 21.1.2 with c++26. #include <cstdint> #include <print> constexpr uint8_t f(char ch) { return static_cast<uint8_t>(ch); } int main() { constexpr uint8_t r = f('a'); std::print("{}", r); }
- psyclobe 1y agoTry consteval , constexpr is a hint. No static casts allowed in constexpr scopes that evaluate as constexpr in c++17 and above (works in c11). Could be wrong I am no expert…
- pjmlp 1y agoIt works perfectly fine with consteval, https://godbolt.org/z/sjGcsh3KP https://godbolt.org/z/sjGcsh3KP What is possible in constexpr contexts has been improving in each revision, the end goal is to support the whole language, eventually. Naturally introducing everything at once would be too hard in a language with such a big ecosystem like C++.
- deleted 1y ago[deleted]
- psyclobe 1y agoGood to know thanks for the correction
- Quitschquat 1y agoIt would be cool to have the entire language and runtime available at compile-time like in Lisp
- kazinator 1y agoIt would be cool, except for the entire language that is available at compile-time being C++, and thus entirely unsuitable for manipulating C++ programs.
- gpderetta 1y agoYes. It is not like C++ compilers are written in C++.
- klipt 1y agoCircle C++ does that
- dataflow 1y agoCould you show some examples?
- chrisrodrigue 1y ago...isn't that what templates were made for? Template metaprogramming in C++ is Turing-complete.
- pjmlp 1y agoNot at all, originally template metaprogramming was discovered by accident. Cannot recall any longer if the original article on the matter appeared on The C/C++ Users Journal or Dr. Dobbs. Eventually it started to get abused and the Turing completeness has been discovered. Since C++11, the approach to a more sane way to do metaprogramming with templates has been improving. Instead of tag dispatch, ADL and SFINAE, we can make use of concepts, if constexpr/eval/init, type traits, and eventually reflection, instead of the old clunky ways.
- veilrap 1y agoI always like these new comile time features getting into the C++ spec. I'm actually looking forward to the related reflection features that I think are currently in scope for C++26. I've run into a number of places where the combination of reflection and constexpr could be really valuable... the current workarounds often involving macros, runtime tricks, or both.
- steveklabnik 1y ago> I'm actually looking forward to the related reflection features that I think are currently in scope for C++26. The core of reflection should be in C++26, yes. In my understanding, there's more to do after that as well. We'll see when the final meeting is done.
- worik 1y agoAre they flogging a dead horse? C++ is, should be, like COBOL. A very important language because of the installed base. But why the continual enhancements? Surely there are better uses of all those resources?
- MrRadar 1y agoCOBOL is being actively developed as a language, the latest standard was published in 2023.
- arjonagelhout 1y agoAlthough there are excellent alternatives to C++ such as Rust, C++ is still widely used as many open-source and commercial codebases are built with it. Adding features to a language that is still actively used does not seem like a bad thing.
- bitexploder 1y agoThe amount of C++ written at my company every day is… a lot. We are slowly fighting away from it towards memory safety, but it is hard. It will take a decade.
- arjonagelhout 1y agoAt the company I currently work for we also use C++, and I am quite proficient in it. But the amount of times I have slowed myself down by simple lifetime issues makes me want to switch to a more memory safe language. Whether that is C++ with profiles or a whole new language such as Rust. For example, a week back I lost a few hours finding a segfault bug in C++ code, which ended up being a trivial lifetime error: I used a reference after it was invalidated due to a std::vector resize. These kind of errors should be compile time errors, rather than hard to trace runtime errors. How does your company go about changing to memory safety? Are new projects / libraries written in Rust for example? Do projects / libraries get (partially) rewritten?
- 1y ago
- ratmeadow 1y agoAnyone else getting concerned about the rate of development of the C++ Standard vs compiler implementation? We don't even have feature complete C++20 on the major compilers yet. C++23 is even less implemented. How will the committee handle this? Will they reduce feature additions at some point in the future? https://en.cppreference.com/w/cpp/compiler_support https://en.cppreference.com/w/cpp/compiler_support
- Pet_Ant 1y agoMaybe knowing where a language is going will help them implement older features? Also, some things are technically easy once all the conceptual wrinkles are ironed out. There is no reason some of these can't be added before C++20 is 100% supported.
- mkoubaa 1y agoI can't imagine why someone would care about that, rather than about the specific changes they actually use or want to use.
- OneDeuxTriSeiGo 1y agoIt is worth noting that one of the main reasons why C++ standard evolution is faster nowadays is because the "bare minimum" for consideration of acceptance in the standard is working examples on a fully functioned compiler. This tends to make it a lot easier for other compilers to implement those features as there is at minimum a working reference to compare against (vs older still unimplemented features like modules where nobody really ironed out how to properly implement them until after they were shoehorned into the standard)
- almostgotcaught 1y agothe only people that complain about this don't actually write C++
- pjmlp 1y agoI write C++, and complain about still not having a portable C++17 implemenation that fully supports parallel STL, not having a full compliant C++20 compiler, embedded compilers kind of still catching up with C++14, and I could rant on.
- waltercool 1y ago[dead]
- 112233 1y agoRecent C++ changes seem like polishing your firewood before burning it. Yes, they make perfect sense, but often they are a fix to a problem that committee introduced by cutting down previous proposals (e.g. forcing lambdas to be single return statement, then relaxing it). Half of new features feel like "how to make STL implementation less embarassing". Meanwhile there still is no language support for e.g. debugging constexpr, or printing internal private state of objects in 3rd party code.
- pjmlp 1y ago> printing internal private state of objects in 3rd party code That is more a dynamic monkey patching programming language kind of thing.
- greesil 1y agoCompile time debugging? Is #warning not good enough?
- HelloNurse 1y agoHow could a debugger make sense of "internal private state of objects in 3rd party code"? Only a portion of the stack frames of linked functions (input parameters coming from known code, maybe expected return values) has a presumable type.
- gpderetta 1y ago> forcing lambdas to be single return statement, then relaxing it At no point were lambdas single return statement. You might be confusing it with some other language feature. Or language.
- einpoklum 1y ago> "how to make STL implementation less embarassing". The "STL" part of the standard library - containers especially but not just that - has an outdated interface, and suffers from ABI being stuck: https://cor3ntin.github.io/posts/abi/ https://cor3ntin.github.io/posts/abi/ so it's embarrassing regardless. > there still is no language support for e.g. debugging constexpr, or printing internal private state of objects in 3rd party code. Actually, reflection might make it easier to do that. Supposedly, you should be able to get a member pointer to the private member you're interested in (or even do it dynamically by iterating over all members, and figuring out which one you like), from that and an actual object obtain a regular pointer, and finally dereference it.
- gitroom 1y agoevery release adding more constexpr stuff honestly helps me - been burned by old template hacks enough lol. you think we ever get to a real point where all this new power just makes stuff easier, or is it always tradeoffs?