2015年8月21日 星期五

Cross Initialization

今天和朋友聊到了會發生在Switch statement與goto statement的Cross Initialization問題。
switch (ch) {
    case 1:
        int jval = 1;
        break;
    case 2:
        jval = 2;
}
compile時會出現

error: crosses initialization of 'int jval'

然而只要改成
switch (ch) {
    case 1:
        int jval;
        break;
    case 2:
        jval = 2;
}
就不會出現error了
或者是使用block來限制變數的scope也可解決這問題。
switch (ch) {
    case 1: {
        int jval;
        break;
    }
    case 2: {
        jval = 2;
    }
}
去google了一下,stackoverflow有許多篇在介紹這個問題,似乎是compiler不允許在同一個scope中jump over initialization.

ISO C++ ‘03 6.7/3 有規範
It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps from a point where a local variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has POD type (3.9) and is declared without an initializer (8.5)

還需要弄懂的是為什麼只有initialization被禁止(不只有explicit的使用initializer來init,default init和value init也都不行),但是卻可以做definition與assignment。

5 則留言:

  1. casino online - Xn--o80b910a26eepc81il5g
    casino online 우리카지노 - Xn--o80b910a26eepc81il5g.online.online. Xn -o80b910a26eepc81il5g.online. Xn クイーンカジノ -o80b910a26eepc81il5g.online. Xn 메리트 카지노 쿠폰 -o40b910a26eepc81il5g.Online.

    回覆刪除
  2. MGM Grand Hotel & Casino, Las Vegas, NV - Mapyro
    Find the cheapest and quickest way to get from MGM 경상북도 출장안마 Grand Hotel & Casino, Las 익산 출장마사지 Vegas to MGM 안산 출장마사지 Grand 계룡 출장샵 Hotel & Casino, Las Vegas, 문경 출장마사지 NV, 89109.

    回覆刪除
  3. Mr Green's app, which is available for Android and iOS, additionally be|can be} an choice if you prefer to play in your smartphone. The app options intuitive and easy-to-use navigation with tons of options that will ensure you've have} an outstanding gaming expertise. With the app, have the ability to|you probably can} wager in your favourite slots, live on line casino tables, Keno, and sports events (live and pre-event). It 먹튀검증 abc-1111 is secure and simple to deposit and withdraw cash from the app, and in case you may have|you could have|you would possibly have} any queries, the client assist staff is all the time willing to assist. This distinctive and useful software permits to quickly and easily propose limits for deposits or losses to make sure your expertise is as enjoyable and safe as attainable.

    回覆刪除
  4. If your phone/tablet blocks the obtain, allow “Downloads from Unknown Sources” in your security settings. This is a 3-reel pokies sport that has a ninety six.8% RTP and quantity of} probabilities of winning. The best fee methodology is the method that is the most accessible to you, nicely as|in addition to} the one you are feeling the most comfortable utilizing. Some of the providers referred to on our web site could also be} considered to be prohibited interactive gambling providers underneath the interactive Gambling 1xbet korea Act 2001 . I am over 18 and have learn and accepted the terms of use and privateness coverage. By ticking this box I affirm I am pleased to obtain newsletters and advertising from PokerNews.

    回覆刪除