设为首页 加入收藏

TOP

2.2 Literal Constants (6)
2013-10-07 15:25:09 来源: 作者: 【 】 浏览:80
Tags:2.2 Literal Constants

ADVICE: DON’T RELY ON UNDEFINED BEHAVIOR

Programs that use undefined behavior are in error. If they work, it is only by coincidence. Undefined behavior results from a program error that the compiler cannot detect or from an error that would be too much trouble to detect.

Unfortunately, programs that contain undefined behavior can appear to execute correctly in some circumstances and/or on one compiler. There is no guarantee that the same program, compiled under a different compiler or even a subsequent release of the current compiler, will continue to run correctly. Nor is there any guarantee that what works with one set of inputs will work with another.

C++(www.cppentry.com) 程序一定要避免“未定义的行为”,所以C++(www.cppentry.com) 程序员一定要知道哪些是“未定义的行为”。

Programs should not (knowingly) rely on undefined behavior.

Similarly, programs usually should not rely on machine-dependent behavior, such as assuming that the size of an int is a fixed and known value. Such programs are said to be nonportable. When the programismoved to anothermachine, any code that  relies on machine-dependent behavior may have to be found and corrected. Tracking down these sorts of problems in previously working programs is, mildly put, a profoundly unpleasant task.

Multi-Line Literals

There is a more primitive (and less useful) way to handle long strings that depends on an infrequently used program formatting feature: Putting a backslash as the last character on a line causes that line and the next to be treated as a single line.

As noted on page 14, C++(www.cppentry.com) programs are largely free-format. In particular, there are only a few places that we may not insert whitespace. One of these is in the middle of a word. In particular, we may not break a line in the middle of a word. We can circumvent this rule by using a backslash:

  1. // ok: A \ before a newline ignores the line break  
  2. std::cou\  
  3. t << "Hi" << st\  
  4. d::endl; 

is equivalent to

  1. std::cout << "Hi" << std::endl; 

We could use this feature to write a long string literal:

  1. // multiline string literal  
  2. std::cout << "a multi-line \  
  3. string literal \  
  4. using a backslash"  
  5. << std::endl;  
  6. return 0;  

Note that the backslash must be the last thing on the line—no comments or trailing blanks are allowed. Also, any leading spaces or tabs on the subsequent lines are part of the literal. For this reason, the continuation lines of the long literal do not have the normal indentation.

根据勘误,这里四个选项中10 前面的负号都应该去掉,因为“ 10”等是表达式,不是字面量。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇2.2 Literal Constants (7) 下一篇2.2 Literal Constants (5)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Java 实现多个大文件 (2025-12-24 23:22:00)
·Java多线程编程在工 (2025-12-24 23:21:56)
·请问微信4.0版本xwec (2025-12-24 22:48:42)
·电脑NVIDIA的文件夹 (2025-12-24 22:48:40)
·如何看待微信新版本 (2025-12-24 22:48:37)