Giter VIP home page Giter VIP logo

Comments (14)

Mooophy avatar Mooophy commented on July 19, 2024

1.exercise 4.12

@pezy 's answer is correct.
This exercise is talking about Operator precedence rather than Order of Evaluation of Operands.

auto result = i!=j<k

Because Operator precedence is defined in C++ standard, the code above is equivalent to

auto result = i!=(j<k)

But because Order of Evaluation of Operands is undefined in standard ,as said in ex4.3. We can't tell whether j or k is evaluated first in the expression j < k.

from cppprimer.

Mooophy avatar Mooophy commented on July 19, 2024

Take the following code as an example :

int i = 0;
auto product = ++i * i;

The value of variable product is undefined, because it totally depends on compiler implementation.
It could be 0, if the right hand side is evaluated first;
It could be 1, if the left hand side is evaluated first;
It could be anything else, depending on how compiler is implemented.

from cppprimer.

pezy avatar pezy commented on July 19, 2024

@Ocxs 抱歉有点忙,你问题又比较多,原谅我一点点的回答哈。

4.12 的确有点问题。j < k 的结果是一个 bool 值,与 int 型的 i 比较,是会有警告的。本书也不推荐这样的写法:

Warning
It is usually a bad idea to use the boolean literals true and false as operands in a comparison. These literals should be used only to compare to an object of type bool.

我的答案至少应该把这个写进去。


你说没有明确求职顺序,这点我不敢苟同。我截取书上的顺序表如下:

unnamed qq screenshot20150225173644

可以看到,这是有明确顺序的, < 优先于 !=

@Mooophy 还扩展了这个问题,加了一个 =. 但我们也可以发现 = 比上述两个都靠后。优先级是非常低的。所以其实也不存在 UB 的问题。唯一的问题就在于我最开始提到的 bool 值与 int 值比较的问题。这里会发生自动类型转换,这么写也没什么错。但不推荐。


@Mooophy 举得第二个例子,是很典型的 UB,因为 ++* 是同一个优先级,这与上面的情况有所不同:

image

在此,非常感谢 @Mooophy 参与讨论。


若这道题无异议的话,我再看看下一个问题哈。 😸

from cppprimer.

Mooophy avatar Mooophy commented on July 19, 2024

@pezy no problem

from cppprimer.

Ocxs avatar Ocxs commented on July 19, 2024

@Mooophy get it ! Thanks very much !

from cppprimer.

Ocxs avatar Ocxs commented on July 19, 2024

@pezy 您说的是对的,我弄错了。谢谢,这些问题有时间抽空回答一下就好,麻烦你了,不好意思!

from cppprimer.

pezy avatar pezy commented on July 19, 2024

4.25

siginifies应该是signifies ,negatation应该是negation

我也认为有拼写错误。其实这个答案是 Mooophy/Cpp-Primer#140 中修改的,当时我看的不仔细。望 @Mooophy@alexandermladenovic 有空可以帮忙确认。

@Ocxs 感谢你这么认真!

from cppprimer.

pezy avatar pezy commented on July 19, 2024

3.exercise 4.28

这个结果好像是编译器不同,输出结果不一样,long double 在vs2013分配的是8字节.

请参考:http://en.wikipedia.org/wiki/Long_double

long double 是兼容 C 而来的,原则上它应当比 double 更加精细,但 C 标准并未规定其实现。

On the x86 architecture, most C compilers implement long double as the 80-bit extended precision type supported by x86 hardware (sometimes stored as 12 or 16 bytes to maintain data structure alignment), as specified in the C99 / C11 standards (IEC 60559 floating-point arithmetic (Annex F)). An exception is Microsoft Visual C++ for x86, which makes long double a synonym for double.

注意,加重的那个例外就是你说的 VC 编译器。

这都是要看具体实现的。

Compilers may also use long double for a 128-bit quadruple precision format, which is currently implemented in software.

from cppprimer.

pezy avatar pezy commented on July 19, 2024

这里的machine指的是编译器还是用的电脑呢?

指的是平台,具体到 VS 中,如下图所示:

image

默认新建的项目是 Win32,可以改为 x64 试一试。

from cppprimer.

pezy avatar pezy commented on July 19, 2024

我这样理解对吗?

对于 4.31, 我之所以更正了答案,是因为看了 http://stackoverflow.com/questions/22591387/usage-of-the-built-in-comma-operator 这个问题和其解答。

简单说,你理解的对。

我仅仅觉得开始的答案有点文不对题,故而扩充了一下,在这个例子里,根本不存在 return 的问题,所以用 prefix 还是 postfix,没有什么区别,仅仅是习惯问题。

from cppprimer.

pezy avatar pezy commented on July 19, 2024

int-->double ,是不是用convert更加确切一点呢?毕竟double是浮点型。

yes, you are right. I will fix it.

from cppprimer.

pezy avatar pezy commented on July 19, 2024

我是初读c++primer,如有错误,请见谅!

不要客气,下次如果碰到拼写错误,或是有更好的答案,欢迎直接 pull request.
再次感谢~ 👍

from cppprimer.

Ocxs avatar Ocxs commented on July 19, 2024

Thanks very much for your explanation !

from cppprimer.

Ocxs avatar Ocxs commented on July 19, 2024

不敢轻易使用pull request,怕弄错了,发issue可以讨论一下

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.