Git Product home page Git Product logo

Comments (11)

pezy avatar pezy commented on May 22, 2024

其实我一直以为LiteralType就是1,“hello”这样的

请翻回第二章节:

2.4.4. constexpr and Constant Expressions
Literal Types:
Of the types we have used so far, the arithmetic, reference, and pointer types are
literal types. Our Sales_item class and the library IO and string types are not
literal types.

书上给 Literal Type 有非常清楚简洁的定义,Mike Seymour大神说的一点没错,size_t 就是 arithmetic 的一种,肯定是 Literal Type 的。(我觉得你肯定又被中文版坑了。。。)


然后看了constant expression这个概念,直接把我看晕了

这个怎么能晕!还是第二章,(每一章节后面都有名词解释,不知道中文版是不是)

constant expression: Expression that can be evaluated at compile time.

这倒是有点接近你说的 1,“hello” 之类的了。。。 :neckbeard: 。目前你初学,建议立足书本,记住 constant expression 就是编译期间就能确定值的表达式。

举几个例子:

constexpr unsigned sz = 42;
string bad[sz]; // constant expression 可以作为数组的下标
sizeof (type); // sizeof 的返回值是 constant expression,所以 size_t 也就是 Literal Type 了。
constexpr size_t sz = sizeof(ia)/sizeof(*ia);
switch(ch) {
    case 3.14: // 这里 case 后面跟的,一定是 integral constant expressions

以上都是之前章节的例子,到这一节,又多了一个 constexpr function 返回的也是 constant expression 了。


概念搞清楚了,就会恍然大悟这里一点也不矛盾。

constexpr size_t scale(size_t cnt) { return new_sz() * cnt; }

new_sz() 前面定义了,是一个 constexpr function,肯定返回 constant expression,那么只需要 cnt 也是一个 constant expression,那么 new_sz() * cnt 也必然是一个 constant expression 了吧?

所以要注意看它的注释:

// scale(arg) is a constant expression if arg is a constant expression

可能你想问,为啥做了乘法还是 constant expression,打个比方:

constexpr size_t sz = 42;
sz * 4; <== 你说这个 expression 在编译期间能否确定值?口算都出来了吧(168)。。。

from cppprimer.

pezy avatar pezy commented on May 22, 2024

说句题外话:

你如此善于提问(提问水平可与 @Mooophy 相提并论,在此敬佩一下),能否将你 post 到 SO 上的问题,尽量嵌入目前的习题答案里(只需要相关即可),然后可以给我发 pull request. 我觉得可以帮助更多的人。毕竟答案不是目的,好的问题带来好的讨论,能产生更深的理解。

Thx a lot~ 🙇

是不是一个人在对同一个repository一次最多只能开一个issue?

这个貌似是 Github 的防御机制,避免恶意灌水。解决办法如下:

  1. 你可以在 @Mooophy 那个 repo 里提问,这样起码可以错开。
  2. 你可以在你自己 Fork 的 repo 里提问,那里完全不限制 issue 的个数,你还可以打标签做整理。方便日后的温习。(我已经 Watch 了你的 repo)

from cppprimer.

Mooophy avatar Mooophy commented on May 22, 2024

haha , reading this question on SO, I thought it was post by my self...
Agree with @pezy . Such discussion on So is quite meaningful. It's gonnba be great, if added into repo .

from cppprimer.

Mooophy avatar Mooophy commented on May 22, 2024

C++ is a huge language with quite a lot of features. IMHO, it's quite important to make use of time wisely. So for the feature constexpr, I'd like to talk about it in another point of view. That's opportunity cost.

We can compare constexpr with another c++11 feature lambda expression to see what I'm trying to say.

To my knowledge,lambda expression have been used a lot in other languages, such as C#, Python, Haskell, Scheme, etc,etc...(I can make this list quite long).It's quite useful as well for other parts of C++, like STL, multi-threading, etc.

Whereas constexpr was seldom found in other mainstream languages. Also it is not very useful for later C++ studying. Big concepts like STL, OO, Template have nothing to do with it.

See? If same time payed on both, one would gain a lot from lambda expression. You can grasp similar concepts in other languages more easily. You can use <algorithm> easily. You can be more comfortable with things like std::thread.

Well, you would gain a little from constexpr as well. Say, you have a code that needs 10 sec to compile, 1 sec to run. constexpr perhaps could make it just 11 sec to compile, 0.9 sec to run. That's it...

As another evidence, Visual Studio has't implemented it at all. Even the vs2015 has only implemented it partially.

As a personal suggestion, if @Ocxs has problem following constexpr, no worries. Please feel free to go for next part.

from cppprimer.

Ocxs avatar Ocxs commented on May 22, 2024

@pezy 我比之前更加理解了,但是总感觉没有悟透,所以这个准备暂时放一下。您说的将问题post 到 目前的习题答案里,没问题,那我之前在知乎还有segmentfault上面的提问用不用也贴上去?谢谢您的讲解!

from cppprimer.

Ocxs avatar Ocxs commented on May 22, 2024

@Mooophy Thx for your advice !I'll take it. BTW, are you a student or worker ? And @pezy ?

from cppprimer.

Mooophy avatar Mooophy commented on May 22, 2024

@Ocxs I'm a student...

from cppprimer.

Ocxs avatar Ocxs commented on May 22, 2024

@Mooophy Wow, wonderful 👍 同样都是学生,咋差距就这么大哩!刚才@pezy 说:

你可以在你自己 Fork 的 repo 里提问,那里完全不限制 issue 的个数

为什么我在自己Fork 的 repo里没有看见issue的按钮,但是一点击你们的repo,右边就有issue的按钮?是我没找到吗?

from cppprimer.

pezy avatar pezy commented on May 22, 2024

@Ocxs I'm a "worker"....... 😭 🏭 🏭 🏭 🏭

我之前在知乎还有segmentfault上面的提问用不用也贴上去

If possible, I'd like to merge it.

为什么我在自己Fork 的 repo里没有看见issue的按钮,但是一点击你们的repo,右边就有issue的按钮?是我没找到吗?

Can you see the Setting at the right side? Click on it, and see following settings:

image

from cppprimer.

Ocxs avatar Ocxs commented on May 22, 2024

@pezy Thx a lot!

from cppprimer.

Mooophy avatar Mooophy commented on May 22, 2024

@pezy Awesome! 👍

from cppprimer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.