名稱空間
變體
操作

C++ 關鍵字: goto

來自 cppreference.com
< cpp‎ | 關鍵字
 
 
C++ 語言
 
 

[編輯] 用法

  • goto 語句:作為語句的宣告

[編輯] 示例

#include <cassert>
#include <string>
 
[[nodiscard]] auto get_first_line(const std::string& string)
{
    std::string first_line{};
    for (const auto character : string)
        switch (character)
        {
            case '\n':
                goto past_for; // breaks from 'range-for loop'
            default:
                first_line += character;
                break;
        }
past_for:
    return first_line;
}
 
int main()
{
    assert(get_first_line("Hello\nworld!") == "Hello");
}

[編輯] 參閱

(C++17 起)
(C++23 起)
(C++20 起)