小编Equ*_*uod的帖子

如何在 constexpr 上下文中使用 if-constexpt 比较 string_view

是否可以在 constexpr 上下文中使用“if constexpr”来比较 std::string_view ?以及为什么 is_hello_2 和 is_hello_4 无法编译显示错误:“'s' 不是常量表达式”

static constexpr bool is_hello_1(auto s) {
  return s == "hello";
}

static constexpr bool is_hello_2(auto s) {
    if constexpr (s == "hello") {
        return true;
    }
    return false;
}

static constexpr auto is_hello_3 = [](auto s) {
    return s == "hello";
};

static constexpr auto is_hello_4 = [](auto s) {
    if constexpr (s == "hello") {
        return true;
    }
    return false;
};
Run Code Online (Sandbox Code Playgroud)

考虑到主要功能(https://godbolt.org/z/zEcnb8):

int main(int argc, char …
Run Code Online (Sandbox Code Playgroud)

c++ string-view c++20 if-constexpr

4
推荐指数
1
解决办法
453
查看次数

标签 统计

c++ ×1

c++20 ×1

if-constexpr ×1

string-view ×1