亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? sutter.htm

?? 教你如何編寫有效簡潔的出C++程序
?? HTM
?? 第 1 頁 / 共 4 頁
字號:
    T* v_new = NewCopy( v_, vsize_, vsize_new );    delete[] v_;                    // this can't throw    v_ = v_new;                      // take ownership    vsize_ = vsize_new;  }  v_[vused_] = t;  ++vused_;}If we have no more space, we first pick a new size for the buffer and make a larger copy using NewCopy. Again, if NewCopy throws then our own Stack's state is unchanged and the exception propagates through cleanly. Deleting the original buffer and taking ownership of the new one involves only operations that are known not to throw, so the entire if block is exception-safe.After any required grow operation, we attempt to copy the new value before incrementing our vused_ count. This way, if the assignment throws, the increment is not performed and our Stack's state is unchanged. If the assignment succeeds, the Stack's state is changed to recognize the presence of the new value, and all is well.Only one function left... that wasn't so hard, was it? Well, don't get too happy just yet, because it turns out that Pop is the most problematic of these functions to write with complete exception safety. Our initial attempt might look something like this: template<class T>T Stack<T>::Pop() {  if( vused_ == 0) {    throw "pop from empty stack";  } else {    T result = v_[vused_-1];    --vused_;    return result;  }}If the stack is empty, we throw an appropriate exception. Otherwise, we create a copy of the T object to be returned, update our state, and return the T object. If the initial copy from v_[vused_-1] fails, the exception is propagated and the state of the Stack is unchanged, which is what we want. If the initial copy succeeds, our state is updated and the Stack is in its new consistent state, which is also what we want.So this works, right? Well, kind of. There is a subtle flaw here that's completely outside the purview of Stack::Pop. Consider the following client code: int i(s.Pop());int j;j = s.Pop();Note that above we talked about "the initial copy" (from v_[vused_-1]). That's because there is another copy to worry about in either of the above cases, namely the copy of the returned temporary into the destination. (For you experienced readers, yes, it's actually "zero or one copies" because the compiler is free to optimize away the second copy if the return value optimization applies (see Item M20). The point is that there can be a copy, so you have to be ready for it.) If that copy construction or copy assignment fails, then the Stack has completed its side effect (the top element has been popped off) but the popped value is now lost forever because it never reached its destination (oops). This is bad news. In effect, it means that any version of Pop that is written to return a temporary like this cannot be made completely exception-safe, because even though the function's implementation itself may look technically exception-safe, it forces clients of Stack to write exception-unsafe code. More generally, mutator functions should not return T objects by value.The bottom line and it's significant is this: Exception safety affects your class's design! In other words, you must design for exception safety from the outset, and exception safety is never "just an implementation detail." One alternative is to respecify Pop as follows: template<class T>void Stack<T>::Pop( Tresult ) {  if( vused_ == 0) {    throw "pop from empty stack";  } else {    result = v_[vused_-1];    --vused_;  }}A potentially tempting alternative is to simply change the original version to return T instead of T (this would be a reference to the popped T object, since for the time being the popped object happens to still physically exist in your internal representation) and then the caller could still write exception-safe code. But this business of returning references to "I no longer consider it there" resources is just purely evil (see Item E23). If you change your implementation in the future, this may no longer be possible! Don't go there.The modified Pop ensures that the Stack's state is not changed unless the copy safely arrives in the caller's hands. Another option (and preferable, in my opinion) is to separate the functions of "querying the topmost value" and "popping the topmost value off the stack." We do this by having one function for each: template<class T>TStack<T>::Top() {  if( vused_ == 0) {    throw "empty stack";  }  return v_[vused_-1];}template<class T>void Stack<T>::Pop() {  if( vused_ == 0) {    throw "pop from empty stack";  } else {    --vused_;  }}Incidentally, have you ever grumbled at the way the standard library containers' (see Item E49) pop functions (e.g., list::pop_back, stack::pop, etc.) don't return the popped value? Well, here's one reason to do this: It avoids weakening exception safety. In fact, you've probably noticed that the above separated Top and Pop now match the signatures of the top and pop members of the standard library's stack<> adapter. That's no coincidence! We're actually only two public member functions away from the stack<> adapter's full public interface, namely: template<class T>const TStack<T>::Top() const {  if( vused_ == 0) {    throw "empty stack";  } else {    return v_[vused_-1];  }}to provide Top for const Stack objects, and: template<class T>  bool Stack<T>::Empty() const {  return( vused_ == 0 );}Of course, the standard stack<> is actually a container adapter that's implemented in terms of another container, but the public interface is the same and the rest is just an implementation detail.Levels of Safety: The Basic and Strong GuaranteesJust as there's more than one way to skin a cat (somehow I have a feeling I'm going to get enraged email from animal lovers), there's more than one way to write exception-safe code. In fact, there are two main alternatives we can choose from when it comes to guaranteeing exception safety. These guarantees were first set out in this form by Dave Abrahams: Basic Guarantee: Even in the presence of T or other exceptions, Stack objects don't leak resources. Note that this also implies that the container will be destructible and usable even if an exception is thrown while performing some container operation. However, if an exception is thrown, the container will be in a consistent but not necessarily predictable state. Containers that support the basic guarantee can work safely in some settings. (This is similar to every Stack member function leaving the object in what Jack Reeves terms a good but never a bad or an undefined state. For details, consult Reeves' article, Coping with Exceptions.)Strong Guarantee: If an operation terminates because of an exception, program state will remain unchanged. This always implies commit-or-rollback semantics, including that no references or iterators into the container be invalidated if an operation fails. For example, if a Stack client calls Top and then attempts a Push which fails because of an exception, then the state of the Stack object must be unchanged, and the reference returned from the prior call to Top must still be valid. For more information on these guarantees, see Dave Abrahams' documentation of the SGI exception-safe standard library adaptation.Probably the most interesting point here is that when you implement the basic guarantee, the strong guarantee often comes along for free. (Note that I said "often," not "always." In the standard library, for example, vector is a well-known counterexample where satisfying the basic guarantee does not cause the strong guarantee to come along for free.) For example, in our Stack implementation, almost everything we did was needed to satisfy just the basic guarantee... and what's presented above very nearly satisfies the strong guarantee, with little or no extra work. Not half bad, considering all the trouble we went to.(There is one subtle way in which this version of Stack still falls short of the strong guarantee: If Push() is called and has to grow its internal buffer, but then its final v_[vused_] = t; assignment throws, the Stack is still in a consistent state and all, but its internal memory buffer has moved which invalidates any previously valid references returned from Top(). This last flaw in Stack::Push() can be fixed fairly easily by moving some code and adding a try block. For a better solution, however, see the Stack presented below that Stack does not have this problem, and it does satisfy the strong commit-or-rollback guarantee.)Points to PonderNote that we've been able to implement Stack to be not only exception-safe but fully exception-neutral, yet we've used only a single try/catch. As we'll see below, using better encapsulation techniques can get rid of even this try block. That means we can write a fully exception-safe and exception-neutral generic container without using try or catch... which really is pretty cool.As originally defined, Stack requires its instantiation type to have a: default constructor (to construct the v_ buffers) copy constructor (if Pop returns by value) nonthrowing destructor (to be able to guarantee exception safety) exception-safe copy assignment (to set the values in v_, and if the copy assignment throws then it must guarantee that the target object is unchanged; note that this is the only T member function which must be exception-safe in order for our Stack to be exception-safe)Next, we'll see how to reduce even these requirements without compromising exception safety, and along the way we'll get an even more detailed look at the standard operation of the statement delete[] x;.Delving DeeperNow I'll delve a little deeper into the Stack example, and write not just one but two new-and-improved versions of the template. Not only is it possible to write exception-safe generic containers, but between the last approach and this one I'll have demonstrated no less than three different complete solutions to the exception-safe Stack problem.Along the way, I'll also answer several more interesting questions: How can we use more advanced techniques to simplify the way we manage resources, and get rid of the last try/catch into the bargain? How can we improve Stack by reducing the requirements on T, the contained type? Should generic containers use exception specifications? What do new[] and delete[] really do?The answer to the last may be quite different than you expect. Writing exception-safe containers in C++ isn't rocket science; it just requires significant care and a good understanding of how the language works. In particular, it helps to develop a habit of eyeing with mild suspicion anything that might turn out to be a function call including user-defined operators, user-defined conversions (see Item M5), and silent temporary objects (see Item M19) among the more subtle culprits because any function call might throw (except for functions declared with an exception specification of throw(), or certain functions in the standard library that are documented to never throw).An Improved StackOne way to greatly simplify an exception-safe container like Stack is to use better encapsulation. Specifically, we'd like to encapsulate the basic memory management work. Most of the care we had to take while writing our original exception-safe Stack was needed just to get the basic memory allocation right, so let's introduce a simple helper class to put all of that work in one place: template <class T> class StackImpl {/*????*/:  StackImpl(size_t size=0)  : v_( static_cast<T*>               // see Item M2 for info on static_cast    ( size == 0    ? 0    : ::operator new(sizeof(T)*size) ) ),    vsize_(size),    vused_(0)  { }  ~StackImpl() {    destroy( v_, v_+vused_ );         // this can't throw    ::operator delete( v_ );  }  void Swap(StackImplother) throw() {    swap(v_, other.v_);    swap(vsize_, other.vsize_);    swap(vused_, other.vused_);  }  T*     v_;                          // ptr to a memory area big  size_t vsize_;                      //  enough for `vsize_' T's  size_t vused_;                      // # of T's actually in use};There's nothing magical going on here: StackImpl is responsible for simple raw memory management and final cleanup, so any class that uses it won't have to worry about those details. We won't spend much time analyzing why this class is fully exception-safe (works properly in the presence of exceptions) and exception- neutral (propagates all exceptions to the caller), because the reasons are pretty much the same as those we dissected in detail above.

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
欧美电影免费观看完整版| 色视频成人在线观看免| 性做久久久久久免费观看 | 亚洲精品福利视频网站| 中文在线资源观看网站视频免费不卡| 欧美精品一区二区三区蜜桃视频| 宅男噜噜噜66一区二区66| 欧美日韩视频不卡| 欧美日韩国产另类不卡| 欧美日韩激情在线| 在线观看网站黄不卡| 处破女av一区二区| 成人美女视频在线观看18| 成人高清视频在线观看| 成人免费高清在线| 91丨porny丨在线| 欧美综合色免费| 欧美色电影在线| 日韩亚洲欧美综合| 精品国产91亚洲一区二区三区婷婷| 欧美电影免费观看完整版| 欧美激情资源网| 一区二区三区四区亚洲| 日韩电影免费一区| 国产成人午夜高潮毛片| 91丝袜美女网| 欧美撒尿777hd撒尿| 欧美一区二区日韩| 国产人成亚洲第一网站在线播放| 国产精品美女一区二区| 亚洲成人综合视频| 国产麻豆日韩欧美久久| 97久久超碰国产精品| 88在线观看91蜜桃国自产| 国产午夜精品一区二区三区视频| 亚洲精品一二三| 久久精品久久综合| 日本道色综合久久| 欧美电影免费观看高清完整版在| 中文字幕乱码久久午夜不卡 | 日韩丝袜美女视频| 中文成人av在线| 亚洲成人av电影| 国产精品99久久久久久久女警 | 午夜视频在线观看一区| 国产91富婆露脸刺激对白| 91老司机福利 在线| 日韩欧美一区二区久久婷婷| 亚洲人吸女人奶水| 国产综合色在线| 欧美性猛片aaaaaaa做受| 国产日韩精品一区二区浪潮av| 亚洲欧美视频一区| 国产成人在线视频网址| 欧美一级电影网站| 亚洲国产日产av| 99国产精品久| 中文字幕不卡在线| 狠狠色狠狠色合久久伊人| 欧美人狂配大交3d怪物一区| 中文字幕中文乱码欧美一区二区| 奇米影视在线99精品| 在线欧美日韩精品| 综合分类小说区另类春色亚洲小说欧美 | 3d动漫精品啪啪一区二区竹菊| 国产人妖乱国产精品人妖| 日本欧美韩国一区三区| 91久久一区二区| 亚洲美女在线一区| 99精品视频中文字幕| 国产亚洲欧美色| 国产一区二区三区久久久| 日韩你懂的在线播放| 日本成人在线电影网| 欧美美女黄视频| 亚洲国产一二三| 欧美影视一区在线| 亚洲国产综合人成综合网站| 色婷婷av久久久久久久| 夜夜嗨av一区二区三区网页| 91国偷自产一区二区开放时间 | 亚洲午夜av在线| 91成人在线精品| 亚洲伦理在线免费看| 91丨porny丨最新| 亚洲一区二区三区不卡国产欧美| 色视频成人在线观看免| 亚洲成av人综合在线观看| 欧美高清dvd| 久久精品噜噜噜成人88aⅴ| 精品国产乱码久久久久久闺蜜| 精品一二三四区| 中文字幕欧美激情| 色诱视频网站一区| 亚洲第一成人在线| 日韩一卡二卡三卡四卡| 国产一区 二区 三区一级| 欧美高清在线精品一区| 色噜噜狠狠成人网p站| 亚洲一区二区不卡免费| 欧美理论片在线| 国产一区二区不卡在线| 亚洲人成网站精品片在线观看| 91久久人澡人人添人人爽欧美| 午夜精品免费在线| 久久久精品欧美丰满| 色欧美88888久久久久久影院| 天天av天天翘天天综合网| 2020国产精品自拍| 色88888久久久久久影院野外| 天堂久久一区二区三区| 国产日韩视频一区二区三区| 在线观看成人小视频| 免费高清在线一区| 亚洲少妇30p| 精品国产乱子伦一区| av男人天堂一区| 精品在线你懂的| 亚洲一二三专区| 欧美国产日韩精品免费观看| 欧美色国产精品| fc2成人免费人成在线观看播放| 亚洲二区在线观看| 中日韩免费视频中文字幕| 91精品啪在线观看国产60岁| 成人av午夜影院| 久久国产精品区| 丝袜美腿亚洲色图| 亚洲色图欧美激情| 久久日韩精品一区二区五区| 色吊一区二区三区| 国产传媒欧美日韩成人| 蜜臀av国产精品久久久久| 亚洲精品免费看| 国产精品久久毛片av大全日韩| 日韩一区二区电影在线| 欧美性生活影院| 欧洲一区二区三区在线| 不卡影院免费观看| 国产精品一区二区三区四区| 午夜不卡av免费| 一区二区三区精密机械公司| 国产亚洲一区二区在线观看| 欧美另类z0zxhd电影| 精品视频在线看| 色婷婷综合中文久久一本| 99精品久久免费看蜜臀剧情介绍| 国产a久久麻豆| 国产精华液一区二区三区| 免费精品视频在线| 日本不卡一二三| 日本色综合中文字幕| 丝瓜av网站精品一区二区| 日韩综合一区二区| 香港成人在线视频| 亚洲一区二区三区小说| 亚洲美女淫视频| 亚洲成av人片在线| 亚洲国产一区二区三区| 亚洲成人自拍偷拍| 亚洲高清免费在线| 亚洲成人免费在线观看| 亚洲国产va精品久久久不卡综合| 亚洲国产sm捆绑调教视频| 亚洲mv大片欧洲mv大片精品| 亚洲成人综合在线| 秋霞电影网一区二区| 免费人成精品欧美精品| 狠狠色狠狠色综合| 国产黄人亚洲片| 成人免费毛片aaaaa**| av在线不卡电影| 欧美在线看片a免费观看| 欧美日韩高清一区二区三区| 欧美日韩精品一区二区三区 | 99视频有精品| 欧美三区在线观看| 日韩免费在线观看| 国产精品久久久久aaaa| 亚洲精品国产无套在线观| 香蕉成人伊视频在线观看| 精品午夜一区二区三区在线观看| 国产精品一区二区免费不卡 | 日韩视频中午一区| 国产欧美日韩亚州综合 | 日本在线播放一区二区三区| 精品在线你懂的| 91在线视频播放地址| 欧美福利视频导航| 久久久亚洲欧洲日产国码αv| 国产精品国产三级国产普通话99| 亚洲少妇中出一区| 久草在线在线精品观看| 不卡视频在线观看| 欧美一区二区三区思思人| 中文久久乱码一区二区| 三级成人在线视频| 成人app网站| 26uuu国产日韩综合| 一区二区日韩电影|