TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Const vs. constexpr vs. consteval vs. constinit in C++20

95 点作者 joebaf超过 2 年前

13 条评论

slashdev超过 2 年前
C++ is a great real world example of ignoring the advice "if you find yourself in a hole, stop digging". They have this problem with complexity and the only strategy they have to tackle it is by adding more complexity. Maybe they're going to overflow back to 0. Or they'll come out in China. Who knows. I've moved on.
评论 #33778658 未加载
评论 #33778982 未加载
评论 #33778090 未加载
评论 #33775886 未加载
评论 #33775911 未加载
评论 #33783315 未加载
评论 #33775729 未加载
评论 #33777934 未加载
评论 #33784381 未加载
评论 #33778709 未加载
323超过 2 年前
Less known fact is that it&#x27;s called C++23 because there are 23 ways to initialize a variable.<p>Of course, it&#x27;s a joke. In reality C++23 has only 20 ways to initialize a variable:<p><a href="https:&#x2F;&#x2F;www.cppstories.com&#x2F;2022&#x2F;init-string-options&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.cppstories.com&#x2F;2022&#x2F;init-string-options&#x2F;</a><p><a href="https:&#x2F;&#x2F;blog.tartanllama.xyz&#x2F;initialization-is-bonkers&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.tartanllama.xyz&#x2F;initialization-is-bonkers&#x2F;</a>
wvenable超过 2 年前
A lot of negative comments here but these keywords seem perfectly reasonable to me. If I actually have a place where I&#x27;d need them, I&#x27;d be able to understand which one to use and why.<p>I like to be able to do more compile-time evaluation and it&#x27;s definitely a lot less confusing now than abusing templates for that.
评论 #33777184 未加载
评论 #33778458 未加载
评论 #33783455 未加载
maldev超过 2 年前
Sadly with all of these, it was needed. Constexpr was an monstrosity. Whoever on the comittee board made it &quot;CAN BE&quot; has caused untold horrors on some projects i&#x27;ve worked on. consteval is nice, it&#x27;s just constexpr, but forced on the compiler, which is how it should&#x27;ve been. There should never be &quot;CAN BE&quot;, it&#x27;s my least favorite thing about C&#x2F;C++. The compiler shouldn&#x27;t override me, and consteval is a step in the right direction and has made me target all my projects to c++20(Which is luckily easy since I don&#x27;t use STL functions).
评论 #33777003 未加载
评论 #33777250 未加载
评论 #33784226 未加载
评论 #33776472 未加载
评论 #33779122 未加载
jokabrink超过 2 年前
Whenever I see that C++ added some language extension, I can&#x27;t help but think about Bjarne Stroustrup&#x27;s &quot;Remember the Vasa&quot; Paper [1] and wonder if he had meant this type of complexity he warned against...<p>- [1](<a href="https:&#x2F;&#x2F;www.stroustrup.com&#x2F;P0977-remember-the-vasa.pdf" rel="nofollow">https:&#x2F;&#x2F;www.stroustrup.com&#x2F;P0977-remember-the-vasa.pdf</a>)
评论 #33777858 未加载
ergonaught超过 2 年前
This is the sort of thing that pushed me away from C++ a couple of decades ago. I remain perplexed that the &quot;solutions&quot; to C++&#x27;s &quot;problems&quot; have largely been to continue overcomplicating the language.
评论 #33779175 未加载
评论 #33778577 未加载
orange_joe超过 2 年前
I hate C++. I work with it professionally and have for years. The entire language feels like an anti pattern at this point. I’m repeatedly told how incredibly powerful this language is if I only use this preferred set of language sub features. But this whole approach violates the entire premise of a type based language, which is to offload as much complexity as possible onto a compiler so that you can get compile time warnings &amp; errors. What’s the point of a compiled language if half the language constitutes a foot gun.
评论 #33779411 未加载
评论 #33777075 未加载
brianjacobs超过 2 年前
In summary:<p><pre><code> constexpr variables are constant and usable in constant expressions constinit variables are not constant and cannot be used in constant expressions constexpr can be applied on local automatic variables; this is not possible with constinit, which can only work on static or thread_local objects you can have a constexpr function, but it’s not possible to have a constinit function declaration</code></pre>
fractalb超过 2 年前
I didn&#x27;t understand the difference between constinit and consteval. It appears that constinit is just consteval applied to global&#x2F;static variables. Why two keywords then?
评论 #33786460 未加载
jgaa超过 2 年前
C++ is starting to show that it&#x27;s Designed By Committee ;)<p>I&#x27;m a big fan of C++. It&#x27;s been my primary programming language since 1996. C++11 was awesome! I&#x27;m in the minority of the C++ developers that occasionally take the language (and the compilers - just like Internet Explorer was a great pain for &quot;front end&quot; developers for a decade, MSVC was a pain in the a* for C++ developers) for a ride to it&#x27;s limits.<p>However, the complexity of the language in C++ 23 is mind mind-boggling.<p>The complexity in C++ 11 was quite frankly already overwhelming. I&#x27;ve worked as a senior and principle C++ developer in small startups and big established software companies. In the last decade, I&#x27;ve only met a handful of really qualified C++ developers (people who actually know the language, and how to use it effectively). Most of the &quot;Senior C++&quot; developers I&#x27;ve worked with can use basic things in the same way that average Java developers can use Java - but they know nothing about for example template meta-programming or how to do concurrency correctly. Many of them were just C programmers who at some point were thrown into a &quot;C++ project&quot; and learned how to &quot;use classes&quot;, but still use C functions for starting new threads. In other words, In 2022 they have not even updated themselves to C++ 11.<p>It&#x27;s hard to learn C++ to an advanced level. It&#x27;s &#x2F;really&#x2F; hard. Most people don&#x27;t have the personal drive to do it. At least not the majority of the C++ developers I&#x27;ve worked with over the years. They learn the basics, and then just use the knowledge they already have to do their job.<p>Even I, and friends of mine that are even more enthusiastic about C++ than I am, are starting to feel exhausted by the ever increasing complexity of the language (and the libraries). One of them suggested to me to take a look at Rust. It&#x27;s a lot simpler to learn Java&#x2F;Kotlin (applications) or Rust (system programming) than it is even to maintain a good knowledge about C++.<p>I still think C++ is a lot of fun, and programming exiting things in the language gets me into flow in a way few other activities can. It&#x27;s also rewarding - I can do incredibly cool things, and I can incrementally improve on the way I solve the same problem each time I run into it in a new project.<p>But that&#x27;s me. Most of the C++ code I&#x27;ve seen in small and large companies stinks. The developers have the tool to do wonderful things - but like the Wizard&#x27;s Apprentice - they don&#x27;t know the tool very well, and they don&#x27;t know how to use it. I think most of the projects started today in C++ would be better off written in a simpler language that the developers actually comprehend.
daniel-thompson超过 2 年前
I&#x27;m not a language designer and don&#x27;t want to be one - I&#x27;m open to the idea that these new keywords are actually the simplest, most composable way to address these use cases - but good grief! The burden of complexity weighs so heavily on this language that understanding how&#x2F;when to use its features to solve your problem can easily be as or more difficult than the problem itself.
gpderetta超过 2 年前
It is possible that constexpr keyword for functions will soon go the way of the dodo^Wregister keyword. It is was a pointless keyword added to just to get comp-time evaluation proposal get through the approval process and the the authors have already apologized for it multiple times.<p>GCC has already added -fconstexpr to implicitly mark all functions as constexpr.
评论 #33776458 未加载
评论 #33776368 未加载
chris_wot超过 2 年前
constinit allows for constant initialization. Why, how enlightening! Now... what is constant initialization?