5 ms·
C++20 Oxymoron: Constexpr Virtual
- sharmin123 5y agoSigns Of A Cheating Girlfriend: Gather Proof To Make A Decision: https://www.hackerslist.co/signs-of-a-cheating-girlfriend-gather-proof-to-make-a-decision/ https://www.hackerslist.co/signs-of-a-cheating-girlfriend-ga...
- joebaf 5y agoDo you have any better use cases for those constexpr techniques? maybe we could make simpler code to CRTP?
- adamrezich 5y agoit's been neat to see the trend of languages moving toward allowing more compile-time code execution stuff like this
- jeffbee 5y agoIt's only an oxymoron according to folk wisdom. The common shorthand definitions of constexpr and virtual are both too simple. People come to believe that constexpr means evaluated by the compiler, but it really means that it may be evaluated by the compiler. Similarly, programmers believe that virtual means vtable dispatch, but all it really means is the function supports dynamic dispatch. It may be de-virtualized at compile time, or there may be no overrides anywhere in the program. If you understand the true definitions `constexpr virtual` doesn't seem contradictory.
- mshockwave 5y agoI dunno why people downvote this comment, it seems legit and it's important to know that `inline`, `virtual`, and `constexpr` are actually just heuristics / suggestions to compilers.
- jeffbee 5y agoInline is probably the keyword with true definition furthest from popular belief. By the way, constexpr implies inline, for function declarations.
- jcelerier 5y ago> it seems legit and it's important to know that `inline`, `virtual`, and `constexpr` are actually just heuristics / suggestions to compilers. but they are not ? - inline relaxes ODR which generally changes the linkage of the symbol (weak linkage when inline & exported, strong when not) - adding virtual will always make your class have a vtable and your method overridable. - constexpr make it possible to use in e.g. template arguments: constexpr int foo() { return 123; } std::array<int, foo()> arr; that does not mean that the compiler cannot optimize - no matter how complex your program is, if the compiler has sufficiently advanced optimization passes, it should be able to precompute everything that is possible at compile time
- jeffbee 5y ago> adding virtual will always make your class have a vtable No? https://godbolt.org/z/qMzY1h6Gs https://godbolt.org/z/qMzY1h6Gs
- adamrezich 5y agowow, I never knew that, crazy
- jeffbee 5y agoThis example is trivial but compilers are generally very good at de-virtualizing at compile time even in large programs. People who bring up the supposed cost of vtable dispatch are usually either repeating bad folk wisdom, or haven't used the language since 20 years ago.
- adamrezich 5y ago
- teleforce 5y agoIt seems that C++ is moving in a full speed following trail-blazed by D language than Bjarne will care to admit: https://brevzin.github.io/c++/2019/01/15/if-constexpr-isnt-broken/ https://brevzin.github.io/c++/2019/01/15/if-constexpr-isnt-b...
- programmer_dude 5y agoNothing new. C++ has been stealing ideas left, right and center. And always without attribution.
- pjmlp 5y agoDepends if those ideas were already present in "Design and Evolution of C++" or not, and how much haters actually care to read C++ mailing papers.
- programmer_dude 5y agoRight, the plans were on display? https://www.goodreads.com/quotes/40705-but-the-plans-were-on-display-on-display-i-eventually https://www.goodreads.com/quotes/40705-but-the-plans-were-on...
- Kranar 5y agoI wish that were true, unfortunately C++ is copying many of the superficial aspects of D and making many mistakes along the way mostly because of the bitter pissing contest that exists between the two communities and can largely be traced back to this paper: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3613.pdf http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n361... Admittedly there are a ton of things I don't like about D and overall I find the language to be quite unprincipled and just a kitchen sink of poor mismatched features... but certainly there are some things D does exceptionally well and C++ should have learned from them to avoid about a decade worth of lost productivity. Things like modules, ranges, template constraints, static if, compile time execution are all much cleaner in D than what C++ has and with every new C++ standard it turns out that things are broken, defect reports have to be published, there are serious quality of implementation issues and the features just don't work as well in actual production grade projects compared to the tiny and artificial examples one reads in a blog post to show off the feature. But it's taboo to learn from D, or virtually any other language, with the mantra that "C++ is not like other languages, it's just soooo unique and special that what works for language Foo won't possibly work for C++." and so instead C++ has to reinvent everything in a half-assed manner and repeat many mistakes that others have already learned from.
- baybal2 5y agoI never knew it's possible. I been in the C/C++ world for more than a decade. The seeming simplicity of C++ spec in comparison to "modern" languages hides many unfathomable corner cases, and non-obvious interactions of language features
- tsimionescu 5y ago> seeming simplicity of C++ spec in comparison to "modern" languages This is the first time I have seen someone claim that the C++ spec seems simple. As far as I know, it is the most complex language ever designed (joke/esoteric languages notwithstanding).
- baybal2 5y agoThe basic spec, Stroustrup's c++ is simple. The insanity really began when attempt to further formalise, and narrowly specify the language revealed glaring gaps of the basic spec, and a decade+ period of haphazard fixes to the standard started.
- tsimionescu 5y agoBut even the basic, original spec was extremely complex compared to any other language I for one know. For example, there is no other language that has as many type qualifiers as even the original C++ - volatile, static, const, extern, inline (for function types), mutable (for members), pointer (*), fixed-size array ([NUM]), reference (&), throw (for function types), virtual, friend [we could add unsigned and long, but those at least only apply to a few built-in types]. C++98 had the same value categories (lvalue and rvalue) as C, but already more complex interactions between these and various other language features, because of references if nothing else. Then, C++ has not one but two kinds of pointers - C-style pointers and references, which each interact with other languages differently. C++ has the most complex function overloading rules of any language. Made all the more complex by the special interaction between single-parameter constructors and function overloading. Operator overloading adds yet another layer of complexity, being slightly different than function overloading rules. C++ has exceptions and destructors and special interactions between them. Exceptions also interact with the reference and type specifier system in complex ways. C++98 even had exception specifiers which also have interactions with the type system in general. C++ has templates, with the myriad complex rules for their syntax (e.g. the little known second meaning of the keyword "template"), for name resolution inside template definitions, for template interactions with the linker. SFINAE is also a famously hard to understand rule, that interacts so nicely with overload resolution that it turns the compilation process into a turing-complete language of its own. C++ has namespaces, which interact with name resolution and thus templates and overload resolution in several ways. In the same vein, it has private, public, protected, and friend, all interacting with name resolution. C++ supports multiple inheritance, with complex rules for how objects are constructed in the presence of multiple super classes. C++ has 3 different ways of initializing member variables, with defined interactions between them (in-place, constructor initiator list, constructor body). C++ inherits C's pre-processor, while adding new ways for it to interact with linkage rules (because of templates, default constructors/destructors).
- Matheus28 5y agoI dream of one day in which we can eliminate the need to declare functions as constexpr. It looks so ugly to have that sprinkled everywhere.
- not2b 5y agoNot really an oxymoron: virtual calls could always be replaced by direct calls if the identity of the class object can be determined, and if the compiler can determine both the identity of the class and that value returned by the virtual function call, either by inlining or link-time optimization, the call can be replaced by the value. That was true in the 1998 version of the standard. So constexpr just adds a mechanism to help the compiler do this early evaluation, marking where it should be done, plus adding the ability to use calls in context where the grammar needs a constant, by promising that the call can be reduced to a constant.
- dshpala 5y ago"constexpr Box getBox() const noexcept override" - this signature is just ridiculous. Can't compiler at least infer "const noexcept" from "constexpr"? In the meanwhile, how is C++ lifetime profile doing, is it useable yet? I know C++ pretty well, but I'd pick Rust (or Zig) for my next project.
- gumby 5y agoThe const and constexpr keywords mean different things. Declaring a method const tells the compiler that that method won’t change the object. Declaring something constexpr tells the compiler that if its arguments are known at compile time then it will always return the same result and may be a candidate for “invocation” at compile time. The fact that the keywords both contain the string “const” is a consequence of the English language.
- nybble41 5y agoUntil C++14 constexpr on a non-static member function did actually imply const, since mutation was (logically enough) not permitted in constexpr functions. C++14 relaxed this restriction, however, by allowing constexpr functions to mutate objects which only exist within the lifetime of the constexpr expression, so constexpr no longer implies const.
- aldanor 5y agoThe signature reminds me of dlang somewhy, with its "pure nothrow @safe @nogc" etc.
- pjmlp 5y agoLifetime static analysis is still pretty much experimental in clang and VC++. As for inference, maybe in the example, although in most code bases with binary libraries it would be useless anyway.
- slaymaker1907 5y agoI think a lot of the confusion is due to a lack of clear evaluation model for runtime vs compile time code. Racket actually has a clear model for which code is available at compile vs runtime using phases. While what can be done at compile time is really broad, the message passing between phases is only done through code generation (macros). There are no surprises about what happens at compile time vs runtime. Limiting compile time code to macros enables really powerful tooling for debugging macros since you can step through how each macro gets expanded (with it being common to return more macros through the macro code itself).
- scrubs 5y agoIf c++ charged by the keyword it'd be the most expensive language anywhere. Sort like 90-sylable orders at Starbucks: That'll 43.2 million please!
- mrlonglong 5y agoWhich GNU C++ version will compile the sources in the article? I couldn't get the sections thing to compile with 10.3.1 even with -std=c++20 or -std=c++2a