用其他东西替换宏中的goto

问题描述 投票:4回答:1

我试图删除Mach7中的goto语句,因为goto is not allowed in constexpr function

#define MatchQ(s) {                                                            \
        XTL_MATCH_PREAMBULA(s)                                                 \
        enum { __base_counter = XTL_COUNTER };                                 \
        typedef mch::unified_switch<source_type> switch_traits;                \
        XTL_PRELOADABLE_LOCAL_STATIC(XTL_CPP0X_TYPENAME switch_traits::static_data_type,static_data,match_uid_type,XTL_EMPTY()); \
        XTL_CPP0X_TYPENAME switch_traits::local_data_type  local_data;         \
        bool processed = false;                                       \
        size_t jump_target = switch_traits::choose(subject_ptr,static_data,local_data); \
        XTL_CONCAT(ReMatch,__LINE__):                                          \
        switch (jump_target)                                                   \
        {                                                                      \
            XTL_NON_REDUNDANCY_ONLY(default:)                                  \
            { XTL_REDUNDANCY_ONLY(try){{                                       \
            if (switch_traits::on_default(jump_target,local_data,static_data)) \
                goto XTL_CONCAT(ReMatch,__LINE__);                             \
            XTL_SUBCLAUSE_FIRST

上面的代码使用goto:goto XTL_CONCAT(ReMatch,__LINE__);,可以跳转到switch语句的上部。

如何用其他东西替换goto?

c++ macros constexpr goto
1个回答
0
投票

在@ozlsn和gcc -E的帮助下,替换完成了。说服代码:

while(true)
{
    bool continue_flag = false;
    switch(var)
    {
        default:
            if(something)
                continue_flag = true;
                break;
        // do something
        OtherCases:
            // do something
    }
    if (!contine_flag)
        break;
}

完全承诺:https://github.com/FirstLoveLife/Mach7/commit/3db24a337a7643018ed9e12ac95f53f9a036251c

这是一个相关的质量保证:Using continue in a switch statement

© www.soinside.com 2019 - 2024. All rights reserved.