Comments (14)
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.
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.
@Ocxs 抱歉有点忙,你问题又比较多,原谅我一点点的回答哈。
4.12 的确有点问题。j < k
的结果是一个 bool 值,与 int 型的 i
比较,是会有警告的。本书也不推荐这样的写法:
Warning
It is usually a bad idea to use the boolean literalstrue
andfalse
as operands in a comparison. These literals should be used only to compare to an object of typebool
.
我的答案至少应该把这个写进去。
你说没有明确求职顺序,这点我不敢苟同。我截取书上的顺序表如下:
可以看到,这是有明确顺序的, <
优先于 !=
。
@Mooophy 还扩展了这个问题,加了一个 =
. 但我们也可以发现 =
比上述两个都靠后。优先级是非常低的。所以其实也不存在 UB 的问题。唯一的问题就在于我最开始提到的 bool 值与 int 值比较的问题。这里会发生自动类型转换,这么写也没什么错。但不推荐。
@Mooophy 举得第二个例子,是很典型的 UB,因为 ++
与 *
是同一个优先级,这与上面的情况有所不同:
在此,非常感谢 @Mooophy 参与讨论。
若这道题无异议的话,我再看看下一个问题哈。 😸
from cppprimer.
@pezy no problem
from cppprimer.
@Mooophy get it ! Thanks very much !
from cppprimer.
@pezy 您说的是对的,我弄错了。谢谢,这些问题有时间抽空回答一下就好,麻烦你了,不好意思!
from cppprimer.
4.25
siginifies应该是signifies ,negatation应该是negation
我也认为有拼写错误。其实这个答案是 Mooophy/Cpp-Primer#140 中修改的,当时我看的不仔细。望 @Mooophy 和 @alexandermladenovic 有空可以帮忙确认。
@Ocxs 感谢你这么认真!
from cppprimer.
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.
这里的machine指的是编译器还是用的电脑呢?
指的是平台,具体到 VS 中,如下图所示:
默认新建的项目是 Win32,可以改为 x64 试一试。
from cppprimer.
我这样理解对吗?
对于 4.31, 我之所以更正了答案,是因为看了 http://stackoverflow.com/questions/22591387/usage-of-the-built-in-comma-operator 这个问题和其解答。
简单说,你理解的对。
我仅仅觉得开始的答案有点文不对题,故而扩充了一下,在这个例子里,根本不存在 return 的问题,所以用 prefix 还是 postfix,没有什么区别,仅仅是习惯问题。
from cppprimer.
int-->double ,是不是用convert更加确切一点呢?毕竟double是浮点型。
yes, you are right. I will fix it.
from cppprimer.
我是初读c++primer,如有错误,请见谅!
不要客气,下次如果碰到拼写错误,或是有更好的答案,欢迎直接 pull request.
再次感谢~ 👍
from cppprimer.
Thanks very much for your explanation !
from cppprimer.
不敢轻易使用pull request,怕弄错了,发issue可以讨论一下
from cppprimer.
Related Issues (20)
- ex16_29 include error file,It compile failed HOT 2
- about 16.2.7 HOT 2
- Exercise 7.36
- Exercise 6.53
- Exercise 5.3 correct answer HOT 1
- Ex 6_8 HOT 1
- Ex 6_9 (added #include "fact.cpp")
- Ex 6_10 (added "return 0;")
- Exercise 13.37
- Answer to Exercises 3.37 is not entirely true.
- Ex16.49
- The new soulution of Exercise 5.12
- Ex.7.53 will not compile under the C++14 standard
- #include<exception>
- ex7_22.h 'class' instead of 'struct'
- Bug report exercise 16.12
- An error in exercise 16.28, in file <unique_pointer.h>
- Small modification for answer of Exercise 3.9 - mention `s` is a empty string
- Exercise 9.28(The loop may not stop)
- terminate called after throwing an instance of 'std::bad_alloc
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cppprimer.